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) – 192.168.1.1/24
WAN Port IP (eth1) – 172.16.1.1/24

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 192.168.1.2, 192.168.1.3;

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:

authoritative;

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 192.168.1.0 netmask 255.255.255.0 {
 ## dhcp start and end IP range ##
  range 192.168.1.100 192.168.1.200;
  option subnet-mask 255.255.255.0; ## subnet 
  option broadcast-address 192.168.1.255; ## broadcast
  option routers 192.168.1.254; ## 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

net.ipv4.conf.default.rp_filter=1
net.ipv4.ip_forward=1

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

Install kernel 4.12 in centos 7

How to Install Kernel 4.12 in CentOS 7

Back in May, Linus Torvalds released Linux kernel 4.11, which was codenamed Fearless Coyote. It came …

Leave a Reply

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

%d bloggers like this: