0

I have an old printer with only Ethernet and USB, no Wi-Fi connection. I therefore want to know if I can use my Raspberry Pi 3 B+ for that purpose, so that the printer is shown up as a network printer. The USB cable cannot be used, as it still has to be possible to print using that cable. Neither can I use the Ethernet cable, as I have no plugs nearby the printer and I do not want to cable between floors.

If I share the Internet connection with the printer using NAT on the Pi, will it then still show up as a network printer (it then has to have its own subnet mask)? Or do I then have to install the printer on the Pi and then share it using e.g. Cups?

EDIT It is not identical with the possible duplicate, as my question is concerning the connection with the printer.

  • Samba and CUPS is possibly what you want to research – Jaromanda X Sep 4 '18 at 10:37
  • If the printer is plugged into the network and gets an IP it will be available for all network devices. – Andy Anderson Sep 4 '18 at 11:32
  • Andy Anderson It will be plugged into the Raspberry Pi's RJ45 port, and the Wi-Fi is connected to the network – Lasse Sep 4 '18 at 11:46
  • You misunderstood, I think, the comment by @AndyAnderson. If you plug your printer into the router via Ethernet, you should be good to go. If you are trying to force it to work via WiFi, then I suggest that you update your question with more details. But if by WiFi, then Samba and CUPS are probably what you want. – Brick Sep 4 '18 at 13:00
  • Brick I have edited my question now. So if I bridge wlan to ethernet, then Cups and Samba is the way to go? – Lasse Sep 4 '18 at 14:01
0

You can make the Raspberry Pi a router that routes ethernet packages between ethernet and wifi interface. The best solution would be a bridge so the printer will get part of your network. But bridging on a wifi client connection is not supported by the on board wifi chip (1). Also using network address translation (NAT) with the printer on the RasPi is not an option because other devices want to connect to the printer and not the other way. But it is the nature of NAT that it only works in on direction, from the printer into the cloud. You cannot connect to the printer with NAT. That is the same reason why you cannot connect to computer on your local network from the internet. Your internet router also uses NAT.

I will show you how you can make the RasPi a router. I will use systemd-networkd for reasons. For routing we need two subnets. For my example I assume your wifi network has the subnet 192.168.50.0/24 and the subnet the printer is using is 192.168.1.0/24. The printer accepts an ip address from a DHCP server.

Tested with
Raspberry Pi OS (32-bit) Lite 2020-08-20 on a Raspberry Pi 4B updated at 2020-09-18.
Updates done with sudo apt update && sudo apt full-upgrade && sudo reboot.
Here you can find the last tested revision for previous Raspbian versions.

For reference I use Raspbian Stretch Lite 2018-06-27 updated with sudo apt update && sudo apt full-upgrade && sudo reboot.

Setup systemd-networkd

Just follow to Use systemd-networkd for general networking. You can use section "♦ Quick Step". Then come back here.

Setup wifi client connection

Setup wpa_supplicant with this file and your settings for country=, ssid=, psk= and enable it. You can just copy and paste this in one block to your command line beginning with cat and including EOF (delimiter EOF will not get part of the file):

root@raspberrypi:~ $ sudo -Es   # if not already done
root@raspberrypi:~ # cat >/etc/wpa_supplicant/wpa_supplicant-wlan0.conf <<EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=DE

network={
    ssid="wlan@hoeft-online.de"
    psk="verySecretPwassword"
}
EOF

root@raspberrypi:~ # chmod 600 /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
root@raspberrypi:~ # systemctl disable wpa_supplicant.service
root@raspberrypi:~ # systemctl enable wpa_supplicant@wlan0.service
root@raspberrypi:~ # rfkill unblock wlan

Configure interfaces

Create these files for interfaces eth0 and wlan0 with your settings. For wlan0 don't use an ip address which is in the pool of the DHCP server from that subnet. You can just copy and paste this in one block to your command line beginning with cat and including EOF (delimiter EOF will not get part of the file):

root@raspberrypi:~ # cat > /etc/systemd/network/04-eth0.network <<EOF
[Match]
Name=eth0
[Network]
Address=192.168.1.1/24
DHCPServer=yes
IPForward=yes
[DHCPServer]
PoolSize=2
EOF

root@raspberrypi: ~# cat > /etc/systemd/network/08-wlan0.network <<EOF
[Match]
Name=wlan0
[Network]
Address=192.168.50.60/24
#Gateway = ip from your internet router
Gateway=192.168.50.1
DNS=84.200.69.80 1.1.1.1
EOF

reboot.

You should now be able to connect to the printer from your wifi network with ip address 192.168.1.2. This is why we define PoolSize=2 and 192.168.1.2 is the only left over ip address after the routers ip address 192.168.1.1. The only problem is that the internet router does not know where the device with ip address 192.168.1.2 is. We have to set a static route.

Set static route

This has the advantage that you can use any service the printer provided. You are not restricted to one port. But if you have no access to the internet router or cannot set a static route there then you can use port forwarding (see next section).

To get routing complete working you have to set a static route in your internet router so it can find the route to the printer. On most internet router you can set a static route but how to do that varies from model to model. It's up to you to find it out. For example your RasPi wlan0 interface has the static ip address 192.168.50.60. Then on your router the gateway (next hop) is 192.168.50.60, destination network is 192.168.1.0/24 (or 192.168.1.0 netmask 255.255.255.0).

That means for the internet router: "send all packets belonging to subnet 192.168.1.0/24 (destination network from AP) to the next router on my subnet, the RasPi 192.168.50.60 (gateway). It knows where to go on."

That's it.

Port forwarding

Thanks to @RalfFriedl for the suggestion in his answer. I will show in detail how I would it use in this setup.

To use port forwarding requires that the printer supports the forwared port. We use port 9100 for raw printing. You can check it with nmap 192.168.1.2 on the RasPi. You must get at least the open port:

9100/tcp open  jetdirect

This is almost suported by all printer and mostly named jetdirect. For example you can select this connection on the unix printing system CUPS with AppSocket/HP JetDirect when you install the printer driver on a computer and set it for this example to:

socket://192.168.50.60:9100

On MS Windows machines it should go similar but I'm not familiar with Windows. Look where and how to set connections to network printer in the printer driver with jetdirect (I hope) to printer 192.168.50.60 and port 9100.

There is a unit to start and stop wpa_supplicant for the wifi connection. We simply add to it the iptables rule so it will be set and deleted with the wifi connection. Create a drop in file (3):

rpi ~$ sudo systemctl edit wpa_supplicant@wlan0.service

In the empty editor insert these statements, save them and quit the editor:

[Service]
ExecStartPost=/sbin/iptables -t nat -A PREROUTING -i wlan0 -p tcp --dport 9100 -j DNAT --to-destination 192.168.1.2
ExecStopPost=/sbin/iptables -t nat -D PREROUTING -i wlan0 -p tcp --dport 9100 -j DNAT --to-destination 192.168.1.2

Check with

rpi ~$ systemctl cat wpa_supplicant@wlan0.service

Restart the service and check if port forwarding is set:

rpi ~$ sudo systemctl daemon-reload
rpi ~$ sudo systemctl restart wpa_supplicant@wlan0.service
rpi ~$ sudo iptables -v -t nat --list

That's it.


**refefences:**

[1] Raspberry Pi WiFi to Ethernet Bridge for a server?
[2] Howto migrate from networking to systemd-networkd with dynamic failover
[3] man systemd.unit - overriding vendor settings

| improve this answer | |
  • It was something like this, I was looking for. But I only have the router from my Internet Provider (Icotera 5800), and they have limited the access for setting up anything. But your solution lead me into finding Proxy Arp, but as you see it, will Proxy Arp then be a solution? – Lasse Sep 6 '18 at 8:05
  • @Lasse Yes, proxy arp is the "better solution" I have pointed to in my last sentence. I have already tried it but run into problems with a RasPi. Proxy arp has only worked if the interface was set to promiscous mode. I don't understand this and it is not documented. Maybe it is my local area network environment? I'm just fighting with this. – Ingo Sep 6 '18 at 9:04
  • I will wait and see, if you can figure it out. Otherwise the solution must be, that I setup a DHCP server on Ethernet and create a printserver using Cups – Lasse Sep 6 '18 at 12:08
  • @Lasse I have just noticed that @RalfFriedl has updated his answer with port forwarding using destination NAT. Haven't thought of this. That is a good suggestion to avoid routing. Just set it instead of a static route in the internet router. Then you can use 192.168.10.60:9100 to connect the printer drivers to the printer. – Ingo Sep 7 '18 at 0:47
  • I cannot see the answer is updated :s So that mean I shall create a iptables rule to forward traffic in port 9100 or? Is that also enough to set it up automatically as a network printer on Windows? – Lasse Sep 7 '18 at 7:01
2

You have two options to connect the printer to the Raspberry, via LAN or USB. Either should work, you don't need to install drivers if you generate the print jobs elsewhere.

There are many options to make the printer available.

  • Samba and CUPS is one option, but it might be overkill for a single printer. With Samba, you can also install Windows drivers on the Raspberry and have Windows clients download them automatically.
  • You can use just CUPS. CUPS can accept jobs with LPD, you configure the client to transfer the jobs to the CUPS LPD server that will send them to the printer.
  • You can just have a process listen on port 9100 and send the data to the printer, without queuing.
  • You can set up the Raspberry as a bridge between LAN and WLAN.
  • If you already have a WLAN router or WLAN access point with LAN you can connect your printer to that LAN, the router or access point will act as a bridge between LAN and WLAN.
  • Use NAT to forward the printer connection from the Raspberry to the printer.

    iptables -t nat -A PREROUTING -i wlan0 -p tcp --dport 9100 -j DNAT --to-destination printer
    
| improve this answer | |
  • So you think I shall go for a bridge (wlan to ethernet) and then use Cups? If I use Cups and Samba will the printer then show up as a network printer on Windows 10? – Lasse Sep 4 '18 at 22:11
  • A bridge is of course the easiest way, especially as you likely already have a WLAN router with LAN. Connect the printer to the router, access it over WLAN, nothing else needed. – RalfFriedl Sep 5 '18 at 5:59
  • As I wrote in the question. It is not possible to cable directly to the router, that is why I will setup the Pi as a bridge – Lasse Sep 5 '18 at 6:46
  • Sorry, but a bridge between the ethernet interface (eth0) and the wifi interface (wlan0) as client isn't possible. This is the issue with 4addr and WDS and that is not supported by the wifi chip on the RasPi. You can only bridge an access point with ethernet. For further information look at Raspberry Pi WiFi to Ethernet Bridge for a server?. – Ingo Sep 5 '18 at 16:34
  • Ingo I think I will go for the Proxy Arp solution then, but I had many problems setting that up using DHCPCD, så the tutorial is based upon the old Linux way – Lasse Sep 5 '18 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.