2 of 3
Added section for port forwarding
  • 42.1k
  • 20
  • 85
  • 198

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 and the subnet the printer is using is The printer accepts an ip address from a DHCP server.

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

Setup systemd-networkd

For detailed information look at (2). Here only in short. Execute these commands:

pi@raspberrypi: ~$ sudo -Es
root@raspberrypi: ~# mkdir -p /var/log/journal
root@raspberrypi: ~# systemd-tmpfiles --create --prefix /var/log/journal #ignore warnings (*)

root@raspberrypi: ~# systemctl mask networking.service
root@raspberrypi: ~# systemctl mask dhcpcd.service
root@raspberrypi: ~# sudo mv /etc/network/interfaces /etc/network/interfaces~
root@raspberrypi: ~# sed -i '1i resolvconf=NO' /etc/resolvconf.conf

root@raspberrypi: ~# systemctl enable systemd-networkd.service
root@raspberrypi: ~# systemctl enable systemd-resolved.service
root@raspberrypi: ~# ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

(*) You will get one or two confusing warnings "...Cannot set file attribute..." This are not errors and doesn't matter in this case.

Setup wifi client connection

Setup wpa_supplicant with this file and your settings 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:~ # cat >/etc/wpa_supplicant/wpa_supplicant-wlan0.conf <<EOF
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

    ssid="[email protected]"

root@raspberrypi:~ # chmod 600 /etc/wpa_supplicant/wpa_supplicant-wlan0.conf
root@raspberrypi:~ # systemctl disable wpa_supplicant.service
root@raspberrypi:~ # systemctl enable [email protected]

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

root@raspberrypi: ~# cat > /etc/systemd/network/08-wlan0.network <<EOF
#Gateway = ip from your internet router


You should now be able to connect to the printer from your wifi network with ip address This is why we define PoolSize=2 and is the only left over ip address after the routers ip address The only problem is that the internet router does not know where the device with ip address 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 Then on your router the gateway (next hop) is, destination network is (or netmask

That means for the internet router: "send all packets belonging to subnet (destination network from AP) to the next router on my subnet, the RasPi (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 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:


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 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 [email protected]

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

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

Check with

rpi ~$ systemctl cat [email protected]

Restart the service and check if port forwarding is set:

rpi ~$ sudo systemctl daemon-reload
rpi ~$ sudo systemctl restart [email protected]
rpi ~$ sudo iptables -v -t nat --list

That's it.

**refefences:** [1] [Raspberry Pi WiFi to Ethernet Bridge for a server?](https://raspberrypi.stackexchange.com/a/81518/79866) [2] [Howto migrate from networking to systemd-networkd with dynamic failover](https://raspberrypi.stackexchange.com/a/78788/79866) [3] man systemd.unit - overriding vendor settings
  • 42.1k
  • 20
  • 85
  • 198