Raspberry pi - Set up DHCP server and share internet from WiFi to Ethernet port

One day, your laptop WiFi interface is suddenly died or you got a new device that has only ethernet interface, but your home router is too far from your table and you cannot or even don't want to wire them from your home router (Who like the long long lan cable, right 🙄).

You have one raspberry pi in your hand with both WiFi and ethernet interface working. Why don't we make your raspberry pi become a bridge between your home router and your spoiled WiFi devices, and even you can hide your laptop behind your raspberry pi on the network, sound cool, sound cool🤔

Let's do it. 🐱‍💻

Sketches

Overall, what we're gonna do is to connect Raspberry pi to the internet router through WiFi, and then connect our Spoiled WiFi Laptop to Raspberry pi through Lan (Ethernet) port. Eventually, we expect the laptop will be assigned IP address from Raspberry pi and have Internet connection through Lan port.


A bit more detail about configuration inside Rasbperry pi.

WiFi and Ethernet interface would be bridge by one more interface (Bridge). The DHCP server will assign IP address to the bridge and Ethernet interface will get the IP address and Internet from Bridge interface.

Get started

Connect Raspberry pi to the home router

This step I will not describe the detail, there are thousand of guides over internet and you can search for
"how to connect a Rasbperry pi to WiFi by cli or even gui".

Config Bridge Interface

Add interface

Try below command on Raspberry pi:

pi@raspberrypi:~ $ sudo ip link add br0 type bridge

Confirm again:

pi@raspberrypi:~ $ ifconfig br0
br0: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 0a:35:6d:96:9e:98  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

Config the interface

Edit file /etc/network/interface to include below contents:

pi@raspberrypi:~ $ cat /etc/network/interfaces
source-directory /etc/network/interfaces.d

auto eth0
iface eth0 inet manual

# The bridge network interface
auto br0
iface br0 inet static
        bridge_ports eth0
        bridge_stp off
        address 192.168.99.1
        netmask 255.255.255.0
        network 192.168.99.0
        dns-search "EmbedCoder"

Restart networking service

pi@raspberrypi:~ $ sudo service networking restart

Confirm again the IP address on br0 interface should be 192.168.99.1

pi@raspberrypi:~ $ ip a show dev br0
5: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UNKNOWN group default qlen 1000
    link/ether 0a:35:6d:96:9e:98 brd ff:ff:ff:ff:ff:ff
    inet 192.168.99.1/24 brd 192.168.99.255 scope global br0
       valid_lft forever preferred_lft forever
    inet6 fe80::835:6dff:fe96:9e98/64 scope link
       valid_lft forever preferred_lft forever

At this step, we config the bridge br0 interface to get the ip address 192.168.99.1, and bridge it to eth0.

Config DHCP

Now we config the dhcp server to allocate IP address in the range 192.168.99.1-254 for the bridge interface.

Install dhcp server

You need to check if you already have any dhcp server instance installed on your raspberry pi then you don't need to install any dhcp server else.

pi@raspberrypi:~ $ systemctl | grep dhcp
● dhcpcd.service                                                                               loaded failed     failed       dhcpcd on all interfaces

Like above checking result, I don't have any dhcp server running, the dhcpcd there is just a dhcp client instance.

Please follow and install below dhcp server.

pi@raspberrypi:~ $ sudo apt install -y isc-dhcp-server

Ofcourse, you can select to install the other dhcp server, in this case I choosed isc-dhcp-server, it's just the first thing I see, no special reason. 😅

Config dhcp server

Config the default interface for dhcp server.

Edit file /etc/default/isc-dhcp-server like below

pi@raspberrypi:~ $ cat /etc/default/isc-dhcp-server
INTERFACESv4="br0"
INTERFACESv6=""

Config the subnet for bridge interface.

Add below segment into file /etc/dhcp/dhcpd.conf

# Subnet for br0
ddns-update-style none;
log-facility local7;
lease-file-name "/var/lib/dhcp/dhcpd.leases";
subnet 192.168.99.0 netmask 255.255.255.0 {
    default-lease-time 600;
    max-lease-time 7200;
    option routers 192.168.99.1;
    option subnet-mask 255.255.255.0;
    option broadcast-address 192.168.99.255;
    option domain-name "EmbedCoder";
    option domain-name-servers 8.8.8.8,8.8.4.4;
    range 192.168.99.2 192.168.99.254;
}

Restart the dhcp server

pi@raspberrypi:~ $ sudo service isc-dhcp-server restart

At this step, we completed configuration for dhcp server. Now you can connect your Laptop to Raspberry pi through Lan port and can see an IP address (192.168.99.2 for ex) will be allocated to your Laptop (remember to set your laptop Lan port at the dhcp mode 😉).

You can check from Raspberry pi by

pi@raspberrypi:~ $ dhcp-lease-list
To get manufacturer names please download http://standards.ieee.org/regauth/oui/oui.txt to /usr/local/etc/oui.txt
Reading leases from /var/lib/dhcp/dhcpd.leases
MAC                IP              hostname       valid until         manufacturer
===============================================================================================
00:e0:4c:36:00:fb  192.168.99.2    LAPTOP-FIMV73Q 2021-05-01 08:25:01 -NA-

 

Or from your laptop

Route traffice by iptables

After we have the IP address for the Laptop connecting to Raspberry pi, we still have no internet for the Laptop yet.

We still need sone more steps to complete this course. Route the traffic from your WiFi to the bridge by using iptable.

Iptable is a tool on Linux to help config, route or block all the packages. It is installed as default on Raspberry pi and can work like a firewall.

Install iptables-persistent

But one thing need to note is, all the configuration by iptable will be lost every time Raspberry pi reboot. We need to persist the iptable configuration, we install iptables-persistent.

pi@raspberrypi:~ $ sudo apt install -y iptables-persistent

Select "Yes" when you are asked for persist IPv4 and IPv6.

Add iptable traffice route

pi@raspberrypi:~ $ sudo iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE

pi@raspberrypi:~ $ sudo iptables -A FORWARD -i wlan0 -o eth0 -m state --state RELATED,ESTABLISHED -j ACCEPT

pi@raspberrypi:~ $ sudo iptables -A FORWARD -i eth0 -o wlan0 -j ACCEPT

pi@raspberrypi:~ $ sudo netfilter-persistent save

pi@raspberrypi:~ $ sudo netfilter-persistent reload

You can check on the router if you have a access, your laptop is totally not appear on the router, that means it's on a separate subnet provide by raspberry pi.

That also means, your raspberry pi became a router somehow and created for you a private subnet with internet attached. if you have a switch, you can plugin the raspberry pi and then connect your devices into that switch, then you got your own network with IP range from 192.168.99.2 to
192.168.99.254.

Huraaaaa, 🥳🥳 This is already the last step, Open your Laptop and enjoy the internet from your raspberry pi bridge. How cool is it ha. 😋


Comments

Popular Posts