Home » Linux » How To » Setup Ubuntu as Router

Setup Ubuntu as Router

Your old P4 PC can be used as a full featured router. Install Ubuntu / Debian & follow this step to setup ubuntu as router.

You can take control over every bit of data flow through the router.

Ok , lets start . Setup router on ubuntu debian, To configure router you must need two network card

Setup ubuntu as router

Test Environment

OS – Ubuntu 14.04 LTS
No of NIC – 2
LAN Port IP (eth0) –
WAN Port IP (eth1) –

1. Install requited tools

bivas@router:~$  sudo apt-get install iptables isc-dhcp-server

iptables is used to manage firewall, routing. You can read details here.

isc-dhcp-server is used to serve ip address automatically to the client connected to LAN interfaces.

bivas@router:~$ sudo nano -w /etc/default/isc-dhcp-server
 bivas@router:~$ sudo nano -w /etc/dhcp/dhcpd.conf

You must prevent the DHCP server from receiving DNS information from clients, set the following global option (this is a security feature):

ddns-update-style none;

You need to set your domain name and name server:

## Set a domain name for your LAN ##
option domain-name "unixhops.local";
## Set DNS server IP address, you can set to your ISP's dns server too or use Google DNS server##
option domain-name-servers,;

Increase the lease time. The time is set in seconds:

### Set the length in seconds that will be assigned to a lease if the client requesting the lease does not ask for a specific expiration time. ##
 ### This is used for both DHCPv4 and DHCPv6 leases (it is also known as the "valid lifetime" in DHCPv6). ###
default-lease-time 86400;
## Set the maximum length in seconds that will be assigned to a lease ##
max-lease-time 604800;

The authoritative directive should be uncommented:


The authoritative directive indicate that the DHCP server should send DHCPNAK messages to misconfigured clients. If this is not done, clients will be unable to get a correct IP address after changing subnets until their old lease has expired, which could take quite a long time. Finally, update the configuration file with your subnet as follows:

subnet netmask {
 ## dhcp start and end IP range ##
  option subnet-mask; ## subnet 
  option broadcast-address; ## broadcast
  option routers; ## router IP

Test DHCP Configuration file if there any error.

bivas@router:~$ sudo dhcpd -t /etc/dhcp/dhcpd.conf

Now restart the DHCP server

bivas@router:~$ sudo service isc-dhcp-server restart

Now here is the most important part.

Need to enable karnel to forward traffic through it

Edit /etc/sysctl.conf & change the both line


Now add the Iptable rule to route data

bivas@router:~$ iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Save the iptable rule

bivas@router:~$sudo iptables save

Check Also

Password less SSH authentication

Password less SSH authentication is one of the best security practices to avoid any password …

Leave a Reply

Your email address will not be published. Required fields are marked *