How to set Static IP on Ubuntu


Static IP is essentially used to configure the system as a web server, or file sharing. On Linux, the IP assignment is typically done by DHCP, which stands for Dynamic Host Configuration Protocol. However, dynamic IP is prone to change and may not be suitable for services like web and media sharing. In such a situation, the system administrator configures the static IP.

In this guide, I will walk you through the process of getting a usable IP address and setting a static IP on Ubuntu.

Set Static IP on Ubuntu

To set the static IP on Ubuntu, there are a few steps that are listed below:

  • Finding the Usable IP address
  • Modifying Configuration Files
  • Applying the Changes and Testing

Let’s proceed with each step mentioned above.

Finding the Usable IP Address

First, check the IP address of the server using ip command.

ip a

Note the IP address, which is in my case 192,168.178.190/24. Here, 24 is the subnet mask in CIDR notation. The 24 means the first 24 bits are used for the network address, while the remaining 8 are for the host address.

The network address indicates the network to which the host is connected, and the host could be a computer or a network device. The host address is the address that indicates the host’s identity within the network.

To get a clear interface name, use the ip command with the show link option.

ip link show

You may get the following interface names.

  • eth0 or enp0
  • wlan
  • bridge

Another command to list interface names is tcpdump.

tcpdump --list-interfaces

In my case, it is enp0s1 which is the same as eth0, it varies depending on how distribution represents them.

Now, note the IP address and interface name.

To proceed further, we must find the subnet mask; which is required to configure the network settings accurately.

The subnet mask can be displayed using the ifconfig -a command.

ifconfig -a 

Here the netmask is 255.255.255.0.

IP Netmask
192.168.64.11 255.255.255.0
11000000.10101000.1000000.00001011 11111111.11111111.11111111.0000000

 

To get the number of the usable number of IP, the formula is 2(32-n). In our case, the n is 24, 32-24=8, which we have 256 IP addresses out of which 254 will be usable.

Let’s get the first IP address and the last IP address.

To get the first usable IP address, perform an AND operation between the binaries of the IP address and the subnet mask.

Subnet Mask(10) 255 255 255 0
Subnet Mask(2) 11111111 11111111 11111111 00000000
IP Address(10) 192 168 64 11
IP Address(2) 11000000 10101000 10000000 00001011
First IP(2) 11000000 10101000 10000000 00000000
First IP(10) 192 168 64 0

And to get the last IP address, the OR operation will be performed between the inverse of the subnet mask (0.0.0.255) and the first IP address (192.168.64.0).

Subnet Mask(10) 255 255 255 0
Subnet Mask(2) 11111111 11111111 11111111 00000000
First IP(10) 192 168 64 0
First IP(2) 11000000 10101000 10000000 00000000
Subnet Mask Inverse(2) 00000000 00000000 00000000 11111111
Last IP(2) 11000000 10101000 10000000 11111111
Last IP(10) 192 168 64 255

So now we have the first IP 192.168.64.0 and the last IP 192.168.64.255. The first and the last IP addresses of the usable addresses are generally not used, so we have a range of 192.168.64.1 to 192.168.64.254 of IP addresses to be used for static IP.

Finally, find the gateway4 address using the ip route command.

ip route

Now, we have all the information to set the static IP, let’s proceed to the next step.

Modifying the Configuration Files

To set a static IP on the Ubuntu server, Netplan file needs to be modified. The Netplan is a utility by Canonical that lets you easily change the network settings. It is an abstraction over two background processes, networkd, and NetworkManager. The Netplan files are located in the /etc/netplan directory and written in YAML.

If you do not see such a file in the directory, you can create it by following a specific naming convention. The file’s name should begin with 01-[name] and have the .yaml extension.

I suggest creating a backup of the configuration file before modifying it.

sudo cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak

Now, open the configuration file in the nano editor.

First, type the network with a colon at the end, on the next line add an indentation and mention the renderer as networkd.

network:

version: 2

 renderer: networkd

There are two renderers, NetworkManager and networkd, if you are accessing the Ubuntu server through GUI then use the NetworkManager otherwise use networkd as renderer.

In my case, I am accessing Ubuntu through the CLI interface so, I will use networkd.

Now, we will add the ethernets with a colon and on the next line, type the interface name, which is, in my case, enp0s1. To stop the dynamic allocation of the IP address, set dhcp4 to no.

Underneath it, we will add addresses where we will insert the static IP address and then gateway4 which is 192.168.64.1. To access your gateway4 address, use the ip route command.

network:

version: 2

 renderer: networkd

 ethernets:

 enp0s1:

 dhcp4: no

addresses: [192.168.64.10/24]

 gateway4: 192.168.64.1

Lastly, we need to set the DNS using the nameserver option in the file. The primary and secondary DNS can be changed depending on the requirement, in my case, I am setting the primary DNS as 8.8.8.8 and the secondary as 8.8.4.4 which are Google’s DNS addresses.

network:

version: 2

 renderer: networkd

 ethernets:

 enp0s1:

 dhcp4: no

addresses: [192.168.64.10/24]

 gateway4: 192.168.64.1

nameservers:

 addresses: [8.8.8.8,8.8.4.4]

Note: Be careful with the indentations while editing the Netplan yaml file. The file will throw an error in case of bad indentation.

The Netplan file is properly modified, in the next step, we will verify and apply the changes.

Applying the Changes and Testing

Now, apply the changes using the netplan command with the try option.

sudo netplan try

If there are any errors, then it will show up, otherwise you will be asked to press the ENTER key to accept the changes.

To check whether the static IP has been configured, use the ip a command.

Set Static IP on Ubuntu through GUI

The process of setting the static IP on the Ubuntu server with a graphical user interface becomes easier.

Open the settings, then go to the Network or Wi-Fi settings.

Next, click on the gear icon.

A window will appear, open the IPv4 tab, check Manual, and set the static IP address information in the following fields.

Click on the Apply button to apply the changes. Next, toggle off and on the connection.

Go to the Details tab to find out the IP address.

Conclusion

The static IP is useful when you want to create a web server or a server through which you want to share files. To set the static IP on Ubuntu, first identify the network interface name, usable IP address, and gateway address. Then make modifications to the Netplan file.

The static IP can also be set using the graphical user interface. Open the Settings, then go to Wi-Fi or Network; click on the IPv4 tab and insert the static IP details.

Print Friendly, PDF & Email
Categories