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 network: version: 2 ethernets: eth0: dhcp4: true optional: true eth1: addresses: - 192.168.50.1/24 optional: true ignore-carrier: true Once we've configured our network, we should apply the changes executing the following command: netplan apply 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 network: {config: disabled} 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 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 # Uncomment the next line to enable packet forwarding for IPv4 - #net.ipv4.ip_forward=1 + net.ipv4.ip_forward=1 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 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 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 Next, we shoud edit the configuration file. vi /etc/dhcp/dhcpd.conf 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; } Now, we will specify the interface the DHCP server should listen to. vi /etc/default/isc-dhcp-server INTERFACESv4="eth1" INTERFACESv6="" Finally, we restart the service and check it's running. systemctl restart isc-dhcp-server systemctl status isc-dhcp-server
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
network:
version: 2
ethernets:
eth0:
dhcp4: true
optional: true
eth1:
addresses:
- 192.168.50.1/24
optional: true
ignore-carrier: true
Once we've configured our network, we should apply the changes executing the following command:
netplan apply
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
network: {config: disabled}
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
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
# Uncomment the next line to enable packet forwarding for IPv4
- #net.ipv4.ip_forward=1
+ net.ipv4.ip_forward=1
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
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
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
Next, we shoud edit the configuration file.
vi /etc/dhcp/dhcpd.conf
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;
}
Now, we will specify the interface the DHCP server should listen to.
vi /etc/default/isc-dhcp-server
INTERFACESv4="eth1"
INTERFACESv6=""
Finally, we restart the service and check it's running.
systemctl restart isc-dhcp-server
systemctl status isc-dhcp-server