Skip to main content
1 of 3
Ingo
  • 42.7k
  • 20
  • 85
  • 205

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.10.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.

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
country=DE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
    ssid="[email protected]"
    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 [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
[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.10.60/24
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

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.10.60. Then on your router the gateway (next hop) is 192.168.10.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.10.60 (gateway). It knows where to go on."

That's it.

(Work in progress. Will try a better solution)


**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)
Ingo
  • 42.7k
  • 20
  • 85
  • 205