DEV Community

Cover image for Configuring Ubuntu Server as a basic router
Sergio Peris
Sergio Peris

Posted on • Originally published at sertxu.dev

Configuring Ubuntu Server as a basic router

First, we're going to configure the networking, lets assume we have two different network interfaces available, eth0 and eth1, the first one will be the WAN interface (internet), and the second one will be our local network.

Configure the network interfaces

To configure the network, we should edit the Netplan configuration file. For eth0 we'll be using DHCP, and for eth1 we'll configure a static IP address.

vi /etc/netplan/50-cloud-init.yaml
Enter fullscreen mode Exit fullscreen mode
network:
   version: 2
   ethernets:
     eth0:
       dhcp4: true
       optional: true

     eth1:
       addresses:
         - 192.168.50.1/24
       optional: true
       ignore-carrier: true
Enter fullscreen mode Exit fullscreen mode

Once we've configured our network, we should apply the changes executing the following command:

netplan apply
Enter fullscreen mode Exit fullscreen mode

As this network configuration file was created by Cloud-init, we should disable it's network capabilities so it doesn't get rewrited when the system reboots.

vi /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
Enter fullscreen mode Exit fullscreen mode
network: {config: disabled}
Enter fullscreen mode Exit fullscreen mode

Enable IP forwarding

In order to allow Ubuntu to pass network packets from one network to another, we should enable IP forwarding.

We can enable it right away running this command:

sysctl set net.ipv4.ip_forward=1
Enter fullscreen mode Exit fullscreen mode

But in order to make it persistent between reboots, we should find and uncomment net.ipv4.ip_forward=1 in the following file.

vi /etc/sysctl.conf
Enter fullscreen mode Exit fullscreen mode
# Uncomment the next line to enable packet forwarding for IPv4
- #net.ipv4.ip_forward=1
+ net.ipv4.ip_forward=1
Enter fullscreen mode Exit fullscreen mode

Configure the network rules

We will use iptables to configure the NAT at the interface we will be using for our eth0 WAN connection.

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
Enter fullscreen mode Exit fullscreen mode

The iptables configuration is not persistent, in order to make it persist after a reboot we should install the package iptables-persistent.

apt install iptables-persistent
Enter fullscreen mode Exit fullscreen mode

During the installation, it will ask us if we want to save the current iptables configuration, we should make sure we save them.

Configure DHCP server

Our network is almost ready to work, but it might be useful to add a DHCP server to our local network at eth1.

First, we download the package.

apt install isc-dhcp-server
Enter fullscreen mode Exit fullscreen mode

Next, we shoud edit the configuration file.

vi /etc/dhcp/dhcpd.conf
Enter fullscreen mode Exit fullscreen mode

We will add the desired configuration, in my case I will setup a very basic subnet declaration:

subnet 192.168.50.0 netmask 255.255.255.0 {
  range 192.168.50.100 192.168.50.199;
  option routers 192.168.50.1;
  option domain-name-servers 1.1.1.1, 1.0.0.1;
}
Enter fullscreen mode Exit fullscreen mode

Now, we will specify the interface the DHCP server should listen to.

vi /etc/default/isc-dhcp-server
Enter fullscreen mode Exit fullscreen mode
INTERFACESv4="eth1"
INTERFACESv6=""
Enter fullscreen mode Exit fullscreen mode

Finally, we restart the service and check it's running.

systemctl restart isc-dhcp-server
systemctl status isc-dhcp-server
Enter fullscreen mode Exit fullscreen mode

Top comments (0)