Using Raspberry Pi as Router

Juniarto Samsudin
3 min readMar 22, 2020

--

We have a situation. We need to connect a network printer that comes with LAN Ethernet interface [ but, alas there is no WIFI connection]. The printer is quite a distance from our router, and no body is going to help you the lay the LAN cable from the router to the printer.

You can use raspberry pi, as a router. Raspberry has 2 interfaces, the ethernet [eth0] and wifi [wlan0]. We can route the traffic from the ethernet side [ where our printer is connected] to the wifi side [which connected to the router].

  1. Enable ip_forward in the kernel
sudo vi /etc/sysctl.conf

add the following line:

net.ipv4.ip_forward = 1

reboot, or reload sysctl:

sudo sysctl -p

2. Install DHCP

Configure eth0 with ip address 192.168.13.1 with /24 subnet. This setting is still not persistent. We need to make it persistent in the following steps below.

sudo ifconfig eth0 192.168.13.1 netmask 255.255.255.0 broadcast 192.168.13.255

Install DHCP

$ sudo apt-get install isc-dhcp-server
$ sudo service isc-dhcp-server stop

Edit /etc/default/isc-dhcp-server. Add the following line:

INTERFACESv4="eth0"

Edit /etc/dhcp/dhcpd.conf, to include the followings:

authoritative; # I will be the single DHCP server on this network, trust me authoritatively
subnet 192.168.13.0 netmask 255.255.255.0{
interface eth0;
range 192.168.13.10 192.168.13.250;
option broadcast-address 192.168.13.255;
option routers 192.168.13.1;
option domain-name "local";
option domain-name-servers 8.8.8.8, 8.8.4.4;
default-lease-time 600;
max-lease-time 7200;
}

Edit /etc/dhcpcd.conf: Here is where we really set the ip address of eth0. It will be persistent after reboot.

# Persist interface configuration when dhcpcd exits. 
persistent

# Rapid commit support.
# Safe to enable by default because it requires the equivalent option set
# on the server to actually work.
option rapid_commit

# A list of options to request from the DHCP server.
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
# Respect the network MTU. This is applied to DHCP routes.
option interface_mtu

# Most distributions have NTP support.
#option ntp_servers

# A ServerID is required by RFC2131.
require dhcp_server_identifier

# Generate SLAAC address using the Hardware Address of the interface
#slaac hwaddr
# OR generate Stable Private IPv6 Addresses based from the DUID
slaac private

# Example static IP configuration:
#interface eth0
#static ip_address=192.168.0.10/24
#static ip6_address=fd51:42f8:caae:d92e::ff/64
#static routers=192.168.0.1
#static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1
interface eth0
static ip_address=192.168.13.1/24


# It is possible to fall back to a static IP if DHCP fails:
# define static profile
#profile static_eth0
#static ip_address=192.168.1.23/24
#static routers=192.168.1.1
#static domain_name_servers=192.168.1.1
profile static_eth0
static ip_address=192.168.13.1/24


# fallback to static profile on eth0
#interface eth0
#fallback static_eth0
interface eth0
fallback static_eth0

Restart DHCP

sudo service isc-dhcp-server start

3. Routing, NAT and persistence after reboot

Install iptables-persistent

sudo apt-get install iptables-persistent

Set NAT and masquerade. This simply mean to route traffic from source (-s 192.168.13.0/24), output (-o) it to wlan0, and masquerade it, as if it is from wlan0.

sudo iptables -t nat -A POSTROUTING -s 192.168.13.0/24 -o wlan0 -j MASQUERADE

Now the printer should work. However, we’ve got make it persistence after reboot.

sudo dpkg-reconfigure iptables-persistent

At this stage, everything should work. If it does not work, you can check the routing table.

pi@pirouter:/etc $ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default Singtel-ACPlus 0.0.0.0 UG 303 0 0 wlan0
192.168.1.0 0.0.0.0 255.255.255.0 U 303 0 0 wlan0
192.168.13.0 0.0.0.0 255.255.255.0 U 202 0 0 eth0

It should show only one Gateway (UG). If it shows 2 or more gateways, you have to delete it. Eth0 SHOULD NOT be the gateway.

To delete Gateway: [ here we delete eth0 gw with ip address 192.168.13.1]

sudo route del default gw 192.168.13.1

--

--

No responses yet