
Building your own bare-bones Linux router is educational, and in an age of cyber-security threats it is a good way to secure your home network. You may also see a significant boost in your internet speeds. Most commercial routers these days are low quality, so the desire to build a bare-bones one from scratch is completely understandable.
This tutorial turns an ordinary Linux PC into a router with two network ports. One port faces the internet and gets its address by DHCP. The other faces your home network, hands out addresses and DNS, and shares the internet connection through NAT. A firewall written in iptables protects the router and controls what is forwarded.
What you will build
What you need
You need a Linux PC with two network ports. I use a self-built machine with these components:
- A mini ATX motherboard with an integrated CPU
- 4 GB of RAM
- A 16 GB flash drive to run the Linux OS
- A 550 watt power supply
- A mini ATX case
In this setup the motherboard's own port (enp2s0) is the WAN interface and a PCI Express network card (enp1s0) is the LAN interface. Check your own interface names before you start, because the rest of the configuration refers to them.
Install Ubuntu first, then work through the steps below on the router.
Step 1: Install the packages
Update the system, then install the networking and routing packages. I use ifupdown to manage the interfaces, DHCP and DNS servers for the LAN, and the iptables persistence tools to keep the firewall across reboots.
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install netplan
$ sudo apt-get install ifupdown
$ sudo apt-get install isc-dhcp-server
$ sudo apt-get install iptables-persistent
$ sudo apt-get install netfilter-persistent
$ sudo apt-get install bind9
The original version of this post left out install on the last four lines. I have restored it to match the Ansible playbook in the repository linked at the end. That playbook also removes netplan rather than installing it, since the router is managed with ifupdown from here on.
Step 2: Turn on IP forwarding
A router has to pass packets from one interface to another. Open the sysctl configuration and add one line.
$ sudo nano /etc/sysctl.conf
Add the following line:
net.ipv4.ip_forward=1
Step 3: Configure the network interfaces
Replace the interfaces file. The WAN interface takes an address from your ISP by DHCP, and the LAN interface gets the fixed address 10.0.0.1.
$ sudo rm /etc/network/interfaces
$ sudo touch /etc/network/interfaces
$ sudo nano /etc/network/interfaces
Place the following lines in the file:
# The loopback network interface
auto lo
iface lo inet loopback
# The WAN interface, motherboard interface
auto enp2s0
iface enp2s0 inet dhcp
# The LAN interface, pci express interface
auto enp1s0
iface enp1s0 inet static
address 10.0.0.1
network 10.0.0.0
netmask 255.0.0.0
broadcast 10.255.255.255
Press control-x, y, enter to save the file.
Step 4: Load the firewall at boot
This small script restores the saved iptables rules whenever an interface comes up.
$ sudo rm /etc/network/if-pre-up.d/iptables
$ sudo touch /etc/network/if-pre-up.d/iptables
$ sudo nano /etc/network/if-pre-up.d/iptables
$ sudo chmod 775 /etc/network/if-pre-up.d/iptables
$ sudo nano /etc/network/if-pre-up.d/iptables
Place the following lines in the file, then save it with control-x, y, enter:
#!/bin/sh
/sbin/iptables-restore < /etc/iptables/rules.v4
Step 5: Set up the DHCP server
Choose the LAN interface
First tell the DHCP server which interface to serve.
$ sudo rm /etc/default/isc-dhcp-server
$ sudo touch /etc/default/isc-dhcp-server
$ sudo nano /etc/default/isc-dhcp-server
Place the following lines in the file and save it:
# Defaults for isc-dhcp-server (sourced by /etc/init.d/isc-dhcp-server)
# Path to dhcpd config file (default: /etc/dhcp/dhcpd.conf).
DHCPDv4_CONF=/etc/dhcp/dhcpd.conf
#DHCPDv6_CONF=/etc/dhcp/dhcpd6.conf
# Path to dhcpd's PID file (default: /var/run/dhcpd.pid).
#DHCPDv4_PID=/var/run/dhcpd.pid
#DHCPDv6_PID=/var/run/dhcpd6.pid
# Additional options to start dhcpd with.
# Don't use options -cf or -pf here; use DHCPD_CONF/ DHCPD_PID instead
#OPTIONS=""
# On what interfaces should the DHCP server (dhcpd) serve DHCP requests?
# Separate multiple interfaces with spaces, e.g. "eth0 eth1".
INTERFACESv4="enp1s0"
INTERFACESv6=""
The original post spelled the interface en1s0 here, which does not match the LAN interface from Step 3. I have written enp1s0.
Define the address pool and fixed leases
Now describe the LAN. Three machines get fixed addresses by MAC address: the wireless access point at 10.0.0.2, the VPN server at 10.0.0.3 and a desktop at 10.0.0.4. Replace each XX:XX:XX:XX:XX:XX with the real MAC address of that device. Every other client gets an address from the range.
$ sudo touch /etc/dhcp/dhcpd.conf
$ sudo nano /etc/dhcp/dhcpd.conf
Place the following lines in the file and save it:
default-lease-time 600;
max-lease-time 7200;
ddns-update-style none;
authoritative;
log-facility local7;
host WAP {
hardware ethernet XX:XX:XX:XX:XX:XX;
fixed-address 10.0.0.2;
}
host VPN {
hardware ethernet XX:XX:XX:XX:XX:XX;
fixed-address 10.0.0.3;
}
host DESKTOP {
hardware ethernet XX:XX:XX:XX:XX:XX;
fixed-address 10.0.0.4;
}
subnet 10.0.0.0 netmask 255.0.0.0 {
range 10.0.0.2 10.0.255.254;
option routers 10.0.0.1;
option domain-name-servers 10.0.0.1;
option domain-name "example.local";
option broadcast-address 10.255.255.255;
}
Step 6: Set up the DNS server
The router answers DNS queries from the LAN with BIND. Queries it cannot answer itself are forwarded to Google's resolvers, 8.8.8.8 and 8.8.4.4, and only the LAN and the router itself may ask.
$ sudo rm /etc/bind/named.conf.options
$ sudo touch /etc/bind/named.conf.options
$ sudo nano /etc/bind/named.conf.options
Place the following lines in the file and save it:
options {
directory "/var/cache/bind";
// If there is a firewall between you and nameservers you want
// to talk to, you may need to fix the firewall to allow multiple
// ports to talk. See http://www.kb.cert.org/vuls/id/800113
// If your ISP provided one or more IP addresses for stable
// nameservers, you probably want to use them as forwarders.
// Uncomment the following block, and insert the addresses replacing
// the all-0's placeholder.
forwarders {
8.8.8.8;
8.8.4.4;
};
allow-query {
10.0.0.0/8;
127.0.0.1;
};
allow-transfer {
10.0.0.0/8;
127.0.0.1;
};
//========================================================================
// If BIND logs error messages about the root key being expired,
// you will need to update your keys. See https://www.isc.org/bind-keys
//========================================================================
dnssec-validation auto;
auth-nxdomain no; # conform to RFC1035
listen-on-v6 { any; };
};
Step 7: Write the firewall and NAT rules
This file does three jobs. The nat table masquerades everything that leaves through the WAN interface, so LAN hosts share the router's public address. The INPUT rules decide what may talk to the router itself: loopback, ping, replies to its own connections, and DNS, SSH and DHCP from the LAN only, with everything else dropped. The FORWARD rules let the LAN reach the WAN and let replies back in, and drop all other forwarded traffic.
The example port-forward sends SSH arriving on the WAN interface to the host at 10.0.0.3. Add further port forwards in the two marked sections, one in the nat table and a matching rule in FORWARD, or delete the example if you do not want it.
$ sudo rm /etc/iptables/rules.v4
$ sudo touch /etc/iptables/rules.v4
$ sudo nano /etc/iptables/rules.v4
Place the following lines in the file and save it:
#####################
######## NAT ########
######################
*nat
:PREROUTING ACCEPT [0:0]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
#
# Enable NAT on WAN interface enp2s0
-A POSTROUTING -o enp2s0 -j MASQUERADE
##
#### Port Forwarding Section 1 - Add any port forwarding rules here and in Section 2 ####
# Example: Port forward SSH traffic from WAN to LAN client 10.0.0.3
-A PREROUTING -p tcp -m tcp -i enp2s0 --dport 22 -j DNAT --to-destination 10.0.0.3:22
COMMIT
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
####
########################
####### Services #######
#########################
#####
# ICMP/Loopback - Accept all traffic from the router to itself (loopback) and ICMP (e.g. ping) traffic
-A INPUT -s 127.0.0.0/8 -d 127.0.0.0/8 -i lo -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -m state --state ESTABLISHED -j ACCEPT
######
# Traceroute - Send a traceroute reject message instead of doing nothing when a client traceroute hits the router
-A INPUT -p udp -m udp --dport 33434:33523 -j REJECT --reject-with icmp-port-unreachable
#######
# DNS - Accept DNS requests traffic (port 53) from the private LAN interface
-A INPUT -i enp1s0 -p tcp --dport 53 -j ACCEPT
-A INPUT -i enp1s0 -p udp --dport 53 -j ACCEPT
########
# SSH - Accept SSH traffic (port 22) from the private LAN so we can manage the router from any machine on the private network
-A INPUT -i enp1s0 -p tcp --dport 22 -j ACCEPT
#########
# DHCP - Accept DHCP requests from the private LAN so that clients can get IP addresses from the router
-A INPUT -i enp1s0 -p udp --dport 67:68 -j ACCEPT
##########
# Drop any other traffic that hits the router
-A INPUT -j DROP
###########
###########################
#### Forwarding rules #####
############################
############
# forward packets to related/established connections
-A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
#############
# Forward from LAN enp1s0 interface to WAN enp2s0 interface
-A FORWARD -i enp1s0 -o enp2s0 -j ACCEPT
##############
#### Port Forwarding Section 2 - Add any port forwarding rules here and in Section 1 ####
# Example: Allow SSH traffic from our NAT rule to client 10.0.0.3
-A FORWARD -p tcp -d 10.0.0.3 --dport 22 -j ACCEPT
###############
# drop all other forwarded traffic
-A FORWARD -j DROP
COMMIT
The file is whitespace sensitive. Each COMMIT line must have no spaces before or after it, with an empty line (no spaces) above and below, and no line should carry stray trailing whitespace. In the original post some comments named the WAN interface enp3s0 and the LAN enp2s0; I have corrected the comments to match the rules, which use enp2s0 for WAN and enp1s0 for LAN.
Step 8: Reboot and check
Reboot the router. It should come up with the LAN interface at 10.0.0.1, handing out addresses and DNS to anything you plug into the LAN port.
$ sudo reboot
These checks come from the notes in the repository. See which services are running (a + means running, a - means not), and look at the DHCP leases to confirm that clients are getting addresses:
$ sudo service --status-all
$ nano /var/lib/dhcp/dhcpd.leases
If the interfaces do not come up, the usual cause is an error in /etc/iptables/rules.v4. After fixing something you can restart a single service instead of rebooting:
$ sudo systemctl restart networking.service
$ sudo /etc/init.d/isc-dhcp-server restart
$ sudo systemctl restart bind9
$ sudo systemctl restart netfilter-persistent
Adding WiFi
This router does not include a wireless access point, but that is fine: nothing stops you from adding a commercial-grade one, such as a UniFi AC from Ubiquiti. Plug it into the LAN port (it is the WAP host in the DHCP configuration above) and it gives you good WiFi speeds behind your own router.
A note from 2026
This is a 2019 walkthrough, and some of it has aged. Ubuntu now configures networking with Netplan by default, which is the system this tutorial deliberately replaces with ifupdown. On a current Ubuntu the interface setup would normally be written as a Netplan file instead. Firewalls on Linux are also moving from iptables to nftables, which is the framework the newer tools build on. ISC has retired the ISC DHCP server used here, and its maintainers point people to Kea instead. The steps above are unchanged from the original, so treat them as a record of how I built it at the time.
Source
The configuration files are also automated as an Ansible playbook in the ubuntu-router repository.