Table of Contents
Typically, in most network configurations, the IP address is assigned dynamically by the router DHCP server. Setting a static IP address may be required in different situations, such as configuring port forwarding or running a web server behind a NAT.
This post explains how to set up a static IP address on Ubuntu Server 20.04.
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 Ubuntu Desktop machines, while the Systemd-networkd is used on servers without a GUI.
Configuring Static IP address on Ubuntu 20 Server
On Ubuntu Server 20.04, the system identifies network interfaces using predictable network interface names.
ip link
The command prints a list of all the available network interfaces. In this example, the name of the interface is ens33.
# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state ...
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc...
2. 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.
# ls /etc/netplan
00-installer-config.yaml
3. To assign a static IP address on the network interface, open the yaml configuration file with your text editor.
sudo nano /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
ens33:
dhcp4: true
version: 2
4. To assign a static IP address to ens33 interface, edit the file as follows:
- Set DHCP to dhcp4: 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 gateway4.
- Under nameservers, set the IP addresses of the nameservers.
# This is the network config written by 'subiquity'
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: no
addresses:
- 192.168.0.10/24
gateway4: 192.168.0.1
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
5. Once done, save the file and apply the changes by running the following command:
sudo netplan apply
6. Verify the changes by typing:
ifconfig
# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.10 netmask 255.255.255.0 broadcast 192.168.255.255
inet6 fe80::20c:29ff:fe47:751e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:47:75:1e txqueuelen 1000 (Ethernet)
RX packets 51777 bytes 62997759 (62.9 MB)
RX errors 0 dropped 22 overruns 0 frame 0
TX packets 4511 bytes 359631 (359.6 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
That’s it! You have assigned a static IP to your Ubuntu server.