During a default install of Ubuntu it will try and automatically obtain an IP address using DHCP. While that may be fine for most users, if you are wanting to set up a server, the chances are you will be wanting it to always have the same IP address.
There are two options for ensuring you get/use the same IP address at every boot. You could set up a IP address reservation on the DHCP server based on the hardware/mac address of the network interface, or alternativly, set the operating system to use a static IP address. If for whatever reason you dont want to go down the path of setting a DHCP reservation (you may not have access to the DHCP server; unsure how to set it up; etc), the only option is to set a static IP address on the PC itself.
(Note: For Ubuntu 18.04 and higher, this is now done using netplan configuration. Please see “Setting a static IP address on Ubuntu 18.04 and higher using netplan“ for more info)
The following steps will guide you through setting up your computer to use a static IP address on Ubuntu.
Open up the /etc/network/interfaces file in your favorite editor. This guide will be using the “nano” editor.
sudo nano /etc/network/interfaces
You should see the lines similar to the following in the editor.
# The primary network interface auto eth0 iface eth0 inet dhcp
The line containing “iface eth0 inet dhcp” needs to either be deleted, or commented out. I choose to comment it out, and add the configuration lines to set up a static IP directly below it. This allows you to change it back easily at a later date if you want to. The # character at the beginning of the line indicates that it is commented out, and will be ignored.
Add the following lines to set up the interface with a static IP. Make sure you change the relevant settings to suit your network.
iface eth0 inet static address 192.168.0.10 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1
Depending on how many network interfaces your machine has, the output file may vary, but below is a full example of the file after being configured with a static IP.
# This file describes the network interfaces available on your system # and how to activate them. For more information, see interfaces(5). # The loopback network interface auto lo iface lo inet loopback # The primary network interface auto eth0 #iface eth0 inet dhcp iface eth0 inet static address 192.168.0.10 netmask 255.255.255.0 network 192.168.0.0 broadcast 192.168.0.255 gateway 192.168.0.1
If you are unsure what settings should go in this file, you can always run the ‘ifconfig’ command, which will output the current IP address settings obtained via DHCP. The default gateway address can be obtained by using the ‘route’ command.
Once you have finished with the changes to the file, save and quit the editor.
You will also need to set up the DNS nameserver settings manually. These are stored in the /etc/resolv.conf file.
Open this file in your editor.
sudo nano /etc/resolv.conf
you will want at least 1 nameserver set up in this file. If one already exists, and is valid, great. If not, add one or more nameserver records in the format below.
Depending on your setup, this could be your router’s IP, your ISP’s DNS server IP, or a 3rd party’s DNS server, such as Google’s (188.8.131.52 and 184.108.40.206), or OpenDNS.
Save and exit the editor.
To activate the changes you will need to reboot the PC, or simply restart networking (recommended).
sudo /etc/init.d/networking restart
$ sudo /etc/init.d/networking restart * Reconfiguring network interfaces... ssh stop/waiting ssh start/running, process 4552 ssh stop/waiting ssh start/running, process 4584 [ OK ]
Your PC should now retain its IP address across reboots, or even when a DHCP server is not working. Use the ‘ifconfig’ command to confirm the IP address has been updated successfully.