How to build Linux Router with Ubuntu Server 20.04 LTS

Post a Comment
Linux router configuration is simple, you just need to make sure the router have two interfaces that connected to the network that will be routed, enable packet forwarding and iptables for NAT if needed. There are more advanced configuration, but in this post we will build Linux router on Ubuntu Server 20.04 to allow LAN traffic through to internet on WAN interface with NAT.

How to build Linux Router with Ubuntu Server 20.04 LTS


How to build Linux Router with Ubuntu Server 20.04 LTS
Topology and IP addresses

Ubuntu Server 20.04 comes with netplan as default network interface configuration tool.If you are not familiar with netplan please read the previous post about Ubuntu Server 20.04 LTS - Basic Network Configuration with netplan.

Find out the available network interfaces
ip add
How to build Linux Router with Ubuntu Server 20.04 LTS
Find out the available network interfaces
  • enp0s3 is the WAN interface, it will be configured to obtain IP address from DHCP server.
  • enp0s8 is the LAN interface, it will be configured to use 10.10.10.1/24 IP address.
1. Edit network configuration
nano /etc/netplan/anything.yaml
This configuration below addedd interface enp0s3 to obtain dhcp and enp0s8 to use 10.10.10.1/24 IP address. Just add lines that red colored to your configuration file, if it is a newly installed Ubuntu Server.
network:
  version: 2
  renderer: networkd
  ethernets:
    enpos3:
      dhcp4: yes
    enp0s8
      addresses:
        - 10.10.10.1/24
- Save then exit the configuration file and apply the configuration
netplan apply
- Make sure all the configuration have applied
ip addr
ping google.com
2. Enable Packet forwarding for IPv4
nano /etc/sysctl.conf
Find and uncomment net.ipv4.ip_forward=1 line
How to build Linux Router with Ubuntu Server 20.04 LTS
Enable Packet forwarding for IPv4
sysctl -p
to apply sysctl configuration.
3. Enable NAT with iptables
iptables -t nat -A POSTROUTING -j MASQUERADE
This command will enable NAT on all traffic.
iptables command line will not be saved after the system reboot, there are many ways to make it persistent after reboot. This time we will use the easiest way to install iptables-persistent package to the Ubuntu server.
apt update -y && apt install iptables-persistent
It will asks you to save the current ipv4 and ipv6 iptables rules, answer yes to save it or no to save it later.
iptables-save > /etc/iptables/rules.v4
This command will save the iptables rules to /etc/iptables/rules.v4, where later it will be restored by iptables-persistent on every reboot.
4. Configure the clients
If all going well, we have finished building Linux Router with Ubuntu Server 20.04 LTS. According to the topology, the clients use 10.10.10.0/24 network address. So configure the clients accordingly.
How to build Linux Router with Ubuntu Server 20.04 LTS
Configure the clients
Try to ping google or just open your browser to start browsing the internet to see if it is works.

You can watch all the activity above on building Linux Router with Ubuntu Server 20.04 LTS from this youtube video below. If you still have any problems, please put your comments.

Related Posts

    Post a Comment

    Subscribe Our Newsletter