How to configure static IP address on Ubuntu 18.04
List All Active Network Interfaces on Ubuntu
First, you need to identify the network interface you are going to configure. You can list all attached network interfaces on your system using the command as shown below:
From the output of the above command, we have 3 interfaces attached to the Ubuntu system: 2 ethernet interfaces (one of them is docker) and the loopback interface. The ens33 ethernet interface has been configured to obtain IP address from DHCP. And we need to change it to static IP.
Set Static IP Address in Ubuntu 18.04
In this example, we will configure a static IP for the ens33 ethernet network interface. To make this happen we will use the netplan utility.
Netplan is a new command-line network configuration utility introduced in Ubuntu 17.10 to manage and configure network settings easily in Ubuntu systems. It allows you to configure a network interface using YAML abstraction. It works in conjunction with the NetworkManager and systemd-networkd networking daemons (referred to as renderers, you can choose which one of these to use) as interfaces to the kernel.
It reads network configuration described in /etc/netplan/*.yaml and you can store configurations for all your network interfaces in these files.
So the first thing to do is to check what configuration files do we have in /etc/netplan/
We can check this out by using the command:
As we can see there is a configuration file called 50-cloud-init.yaml. We need to edit it to change configuration.
If there’s no configuration files in /etc/netplan/ you can use command as shown below to generate it:
sudo netplan generate
In addition, auto generated files may have different filenames on desktop, servers, cloud instantiations etc (for example 01-network-manager-all.yaml or 01-netcfg.yaml, 50-cloud-init.yaml, etc.), but all files under /etc/netplan/*.yaml will be read by netplan.
Open the netplan configuration file using your text editor as shown:
sudo nano /etc/netplan/50-cloud-init.yaml
Find your network interface name in there. It’s ens33 in this example.
As you can see there’s dhcp4: true = dchp for ipv4 is enabled.
To change that simply change the config like that:
- ens33 – network interface name.
- dhcp4and dhcp6 – dhcp properties of an interface for IPv4 and IPv6 receptively.
- addresses– sequence of static addresses to the interface. (with subnet mask)
- gateway4– IPv4 address for default gateway.
- nameservers– sequence of IP addresses for nameserver.
The addresses property of an interface expects a sequence entry for example [192.168.14.2/24, “2001:1::1/64”]
or [192.168.56.110/24, ] (see netplan man page for more information).
Important: don’t use tabs in this configuration file.
Save the file and exit. (to save file press Ctrl+O and then choose the filename and press Enter. To exit nano press Ctrl+X)
Then apply the recent network changes using the following command:
sudo netplan apply
If everything is ok you’ll not get any error messages.
Just check your ip configuration again with the command:
As you can see now the ens33 have an IP addresses as we configured before in yaml file.
Congratulations! You’ve successfully configured a network static IP addresses to your Ubuntu server.