Configure Static IP on Ubuntu Linux

ubuntu-logo

By default most Linux distributions including Ubuntu are configured with DHCP. This means that an IP address and some other parameters are configured automatically. The only issue with dynamic IP is that it can change making remote access a game of chance. Setting up static IP allows a user defined IP addresses that will remain the same unless changed manually.

 

Gather Information

You will need to gather some information prior to editing the configuration file. Specifically you will need to find your network interface, gateway and netmask.

 

Find the required network information.

ifconfig

Network interface is the physical device that connects to the network. This used to be generally eth0 or wlan0 for the first ethernet port and wireless adapter. With the new naming convention you may see different names. In my case there is only a single ethernet port named p3p1.

 

Netmask is displayed as Mask. In my case it was 255.255.255.0.

 

Gateway is the IP of the router that connects to the internet. This is the same IP address you use to log in to your router configuration page. I had my router configured to use 192.168.0.1. If you don’t know your gateway you can easily find out from command line.

route -n

Look for the IP address under Gateway column in a row with the value of UG under the Flags column.

 

For DNS you can use Google Public DNS primary and secondary addresses which are 8.8.8.8 and 8.8.4.4 respectively. Alternatively you can have your router provide the DNS service by supplying a gateway address.

 

Configure Static IP

Edit the network configuration file.

nano /etc/network/interfaces

 

By default interfaces file should have configuration required for DHCP. We don’t want DHCP so we comment out the last two lines. You may need to revert back to DHCP so do not delete them. The first two lines are required for static and dynamic configurations alike.

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
#auto p3p1
#iface p3p1 inet dhcp

 

At the end of the file add the static IP configuration settings. Change the network interface, gateway, netmask with previously noted details. Change address with the IP address of your choice making sure to stay away from low and high numbers to avoid clashes with DHCP addresses.

auto p3p1
iface p3p1 inet static
address 192.168.0.100
gateway 192.168.0.1
netmask 255.255.255.0
dns-nameservers 8.8.8.8 8.8.4.4

 

Restart network services for the changes to take effect.

service network-manager restart

 

Verify the new static IP has been applied.

ifconfig

 

Quick reference

ifconfig
route -n
nano /etc/network/interfaces
service network-manager restart
ifconfig