I suppose this can be followed to set a static IP on your local network in Ubuntu Desktop as well, but this is explained with the expectation that you’re using terminal on an Ubuntu Server setup.
There are plenty of explanations of how to configure the server to use a set static IP instead of getting assigned one automatically by a DHCP server, but it seems that most tend to be incomplete. For example, it’s easy to find an explanation of editing the /etc/network/interfaces configuration to change a particular interface from using DHCP to a static address, but that seems to be where most end – which many will find leaves them unable to resolve any addresses outside the LAN after a system reboot. So I’m going to explain what I do that gets around this problem.
For every command you see here, I am executing them as root – which in some cases may require you to have sudo preceding it if you’re not. You can use the following command to login as root.
First off, you need to open /etc/resolv.conf and note the listed IP addresses for the nameservers your server is automatically using with the DHCP settings. The reason you need to record these values is because when the machine is rebooted, the /etc/resolv.conf configuration gets overwritten – and the overwritten values will be empty since there is going to be no DHCP server providing them anymore. This is done by the DHCP client that is running on the server. Another workaround is to disable or remove the DHCP client, which will then stop the configuration from being overwritten – allowing the sustained values in /etc/resolv.conf to do the job. But I didn’t want to do that just in case I ever want to resume using the DHCP client later on, or in case I wanted to still use it for a different interface. I used nano to open the configuration.
I simply copied the values for each nameserver in an open text editor, but you could write them down by hand on a notepad if that’s what you prefer.
The next step is the same as most other explanations: opening the network configuration and setting the static IP address.
You’ll typically see configurations for just two interfaces here: the lo (or loopback interface) and eth0 (or the primary network interface). The settings for the primary network interface are the ones we want to edit. If your machine has multiple ethernet ports, you may see more than one, but in most cases you will just see the one. By default, it should look something like this.
# The primary network interface auto eth0 iface eth0 inet dhcp
Instead of deleting the settings, I prefer to simply comment them out by placing a hash in front of them.
# The primary network interface #auto eth0 #iface eth0 inet dhcp
The only reason I do this is so that if I decide to revert back to using DHCP for that interface, I only have to uncomment the lines and either remove or comment out the lines that I added to configure it for a static address.
The next step varies depending on the network. If you’re doing this for a home network running on a common residential router, it’s likely that the values for netmask should be the same (255.255.255.0). However, the other values could be slightly different, but hopefully you are aware of what they are if you are configuring any network settings in your home. For me, the network used the address range of 192.168.0.0 to 192.168.0.255 (including the network and broadcast addresses). The gateway address is almost always the first address in the network and assigned to the router itself, which for me is 192.168.0.1. I chose to assign the address of 192.168.0.100 to the Ubuntu server (similar to most other examples). Lastly, I included the values for the dns-nameservers since we will not be able to get them from the /etc/resolv.conf location after the network service has been restarted with our changes.
# The primary network interface auto eth0 iface eth0 inet static address 192.168.0.100 netmask 255.255.255.0 gateway 192.168.0.1 network 192.168.0.0 broadcast 192.168.0.255 dns-nameservers 0.0.0.0 184.108.40.206 220.127.116.11
You should replace with the values you see in my example on the line for dns-nameservers with those you recorded from your /etc/resolv.conf configuration. Each entry for the dns-nameservers should be separated by a space as shown. Do not create a new line with dns-nameservers <address> for each entry or else the network service will not accept it.
After doing this, you can simply restart the network service to apply the new settings, which apparently must be done as root – gives permission errors for me when trying to do it using sudo.
Once this is done, you can check the active network settings to make sure the server is using the new address for the interface.
If everything looks good now, you should restart the server – place sudo in front of the command if not executing as root.
After the server has rebooted, test that the nameserver configuration did work and you can resolve outside addresses by pinging an address such as Google.
If you do not get an error, everything should be good to go. 🙂