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.1050.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
.
For detailed information look atJust follow to (2)Use systemd-networkd for general networking. Here only in shortYou can use section "♦ Quick Step". 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.Then come back here.
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
country=DE
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=DE
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]
root@raspberrypi:~ # rfkill unblock wlan
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.1050.60/24
#Gateway = ip from your internet router
Gateway=192.168.1050.1
DNS=84.200.69.80 1.1.1.1
EOF
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.1050.60. Then on your router the gateway (next hop) is 192.168.1050.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.1050.60
(gateway). It knows where to go on."
socket://192.168.1050.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.1050.60 and port 9100.
[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