How to Configure Static IP Address on Ubuntu Server.
In this tutorial, you will learn how to configure networking in Ubuntu 20.04 with Netplan. You will learn how to set static IP addresses, DHCP addresses, as well as how to configure DNS.
Netplan
Ubuntu 17.10 and later uses Netplan as the default network management tool. The previous Ubuntu versions were using ifconfig
and its configuration file /etc/network/interfaces
to configure the network.
Netplan configuration files are written in YAML syntax with a .yaml
file extension. To configure a network interface with Netplan, you need to create a YAML description for the interface, and Netplan will generate the required configuration files for the chosen renderer tool.
Netplan supports two renderers, NetworkManager and Systemd-networkd. NetworkManager is mostly used on Desktop machines, while the Systemd-networkd is used on servers without a GUI.
Configuring Static IP address on Ubuntu Server
On Ubuntu 20.04, the system identifies network interfaces using ‘predictable network interface names’.
The first step toward setting up a static IP address is identifying the name of the ethernet interface you want to configure. To do so, use the ip link
command, as shown below:
$ ip link
The command prints a list of all the available network interfaces. In this example, the name of the interface is eth0
:
Output
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: etho: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether dc:##:##:##:##:#7 brd ff:ff:ff:ff:ff:ff
Netplan configuration files are stored in the /etc/netplan
directory. You’ll probably find one or more YAML files in this directory. The name of the file may differ from setup to setup. Usually, the file is named either 01-netcfg.yaml
, 50-cloud-init.yaml
, or NN_interfaceName.yaml
, but in your system it may be different.
*If your Ubuntu cloud instance is provisioned with cloud-init, you’ll need to disable it. To do so create the following file:
sudo nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
network: {config: disabled}
To assign a static IP address on the network interface, open the YAML configuration file with your text editor:
sudo nano /etc/netplan/50-cloud-init.yaml
/etc/netplan/50-cloud-init.yaml
You should see something like that:
network:
version: 2
renderer: networkd
ethernets:
ens3:
dhcp4: yes
Before changing the configuration, let’s explain the code in a short.
Each Netplan Yaml file starts with the network
key that has at least two required elements. The first required element is the version of the network configuration format, and the second one is the device type. The device type can be ethernets
, bonds
, bridges
, or vlans
.
The configuration above also has a line that shows the renderer
type. Out of the box, if you installed Ubuntu in server mode, the renderer is configured to use networkd
as the back end.
Under the device’s type (ethernets
), you can specify one or more network interfaces. In this example, we have only one interface eth0
that is configured to obtain IP addressing from a DHCP server dhcp4: yes
.
To assign a static IP address to etho
interface, edit the file as follows:
- Set DHCP to
dhcp4: no
. - Set DHCP to
dhcp6: no
. - Specify the static IP address. Under
addresses:
you can add one or more IPv4 or IPv6 IP addresses that will be assigned to the network interface. - Specify the gateway.
- Under
nameservers
, set the IP addresses of the nameservers. - addresses is a list of IPv4 or IPv6 ip addresses for the DNS name servers. IPv6 must be quoted.
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: no
dhcp6: no
addresses: [192.168.1.137/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, "FEDC::1"]
Now, careful, uhen editing Yaml files, make sure you follow the YAML code indent standards. If the syntax is not correct, the changes will not be applied. Can't stress this enough, it will not run.
Run the following command to save the file and apply the changes:
sudo netplan apply
Verify the changes by typing:
ip addr show dev eth0
Ouput
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether dc:##:##:##:##:#7 brd ff:ff:ff:ff:ff:ff
inet 192.168.121.137/24 brd 192.168.1.255 scope global dynamic eth0
valid_lft 3575sec preferred_lft 3575sec
inet6 fe80::5054:ff:feb0:f500/64 scope link
valid_lft forever preferred_lft forever
That’s it!
You have assigned a static IP to your Ubuntu server. 🤝