RealMuhko
Posts: 3
Joined: Sat Dec 16, 2017 12:20 pm

Raspbian Stretch Version as Router

Sat Dec 16, 2017 12:38 pm

Dear Forum!

I have been searching the fourm for information about how to set up my Raspberry Pi 3 with latest Raspbian Stretch OS installed as a simple 3G/4G router device.

It seems like the way of configuring the various network settings has changed in Stretch comparted to previous versions and the tuturials and guides I have seen has all refered to previous verisons - This is why I am creating this post.

My goal is to set up my Raspberry Pi 3 as a Router where the WAN device is my USB 3G/4G modem and LAN is the onboard ethernet port.

I am running a clean install of latest Stretch version and all files and setting are default.

Here is a copy of my ifconfig:

[email protected]:~ $ ifconfig
eth0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether b8:27:eb:e7:ad:17 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.8.100 netmask 255.255.255.0 broadcast 192.168.8.255
inet6 fe80::8c0d:f7bc:d498:cc8b prefixlen 64 scopeid 0x20<link>
ether 0c:5b:8f:27:9a:64 txqueuelen 1000 (Ethernet)
RX packets 99356 bytes 119500284 (113.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 54242 bytes 47232537 (45.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

wlan0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
ether b8:27:eb:b2:f8:42 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

ETH0 is the onboard LAN connector
ETH1 is the USB Modem
WLAN0 is the onboard WLAN adapter.

What I wanna do is to have the rpi act as a router for my local network and share the USB modem with all clients on the network.

As far as I understand from the guides found for the earlier versions of Raspbian, I need to do the following:

- Set a static IP address for the ETH0 interface
- Enable IP Forwarding
- Create IP Tables Rules
- Set up DHCP server for the ETH0 interface

I tried editing /etc/network/interfaces with this:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.1.111
netmask 255.255.255.0

auto eth1
iface eth1 inet dhcp


When doing so, the eth0 gets the static ip address assigned but eth1 disappears from ifconfig.

Can someone point me in direction of which files to edit in the current stretch version to be able to configure the above ?

Thank you and have a great weekend ! :)

SurferTim
Posts: 1648
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Raspbian Stretch Version as Router

Sat Dec 16, 2017 12:57 pm

Network settings are done with /etc/dhcpcd.conf.

RealMuhko
Posts: 3
Joined: Sat Dec 16, 2017 12:20 pm

Re: Raspbian Stretch Version as Router

Sat Dec 16, 2017 6:15 pm

Hello SurferTIm.

Thanks for your reply!

I managed to configure the static IP address of eth0 by editing the file /etc/dhcpcd.conf as explained by you.

I have the following set:

interface eth0
static ip_address=192.168.1.1/24
static routers=192.168.1.1
static domain_name_servers=8.8.8.8 8.8.4.4

To enable NAT I edited /etc/sysctl.conf and added this line:

net.ipv4.ip_forward=1

I then made a manual configuration to one of my windows clients:

IP: 192.168.1.2
Subnet: 255.255.255.0
Gateway: 192.168.1.1

DNS: 8.8.8.8
DNS: 8.8.4.4

Do I need to add some rules to IPTABLES on the rpi for the client to be able to use the rpi as a router ?

As of now, the internet connection is working fine on the rpi but not from the client. However I can ping the rpi 192.168.1.1 from the client 192.168.1.2 and will get a positive ping reply.

Should I build some kind of route between my LAN ETH interface eth0 192.168.1.1 and my WAN interface eth1 192.168.8.100 or is it enough to make the route in iptables like fx:

sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
sudo iptables -A FORWARD -i eth1 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT

SurferTim
Posts: 1648
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: Raspbian Stretch Version as Router

Sat Dec 16, 2017 6:24 pm

This is all that is required if eth1 is the WAN interface.

Code: Select all

sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
I use dnsmasq for DHCP service on the LAN interface. The LAN interfaces should be assigned ip addresses only, no "routers" or "domain_name_servers" in /etc/dhcpcd.conf.

RealMuhko
Posts: 3
Joined: Sat Dec 16, 2017 12:20 pm

Re: Raspbian Stretch Version as Router

Sat Dec 16, 2017 6:32 pm

Hello again!

Your help is much appreciated !

I edited /etc/dhcpcd.conf to only contain:

I now have the following set:

interface eth0
static ip_address=192.168.1.1/24

Then added this rule to IPTABLES:

sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE

Everything is running SMOOTH now !

Thank you so much for your help SurferTIm - again it is much appreciated. :)

MagpiesN5
Posts: 4
Joined: Wed Feb 14, 2018 8:43 am

Re: Raspbian Stretch Version as Router

Wed Apr 18, 2018 12:43 pm

Hi Guys I think this is what I am looking for. Wondering if it is still working ok and if you have had to make any tweaks etc.
would you mind sharing what you ended up with

Return to “Troubleshooting”

Who is online

Users browsing this forum: stevepiproject and 33 guests