Setting up a static IP dddress on Ubuntu Server 20.04 / 22.04 / 24.04

When setting up a virtual machine in a local network/homelab, it is recommended to set up a static IP for the machine to ensure that services are running smoothly and without interruption because the IP is changed after each reboot.

In this article, I will share how to set up a static IP address on a computer with Ubuntu Server 20.04 / 22.04 / 24.04

Setting up a static IP on Ubuntu Server

First check the current IP and network port of the computer using the command

ip addr show
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:00:64:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.132/24 brd 192.168.0.255 scope global dynamic eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fe00:6400/64 scope link
       valid_lft forever preferred_lft forever

The current IP of the eth0 network port is being set to dynamic IP mode from the Router’s DHCP Server.

Check gateway and DNS parameters with the command

networkctl status
 Interfaces: 1, 2
       State: routable
Online state: online
     Address: 192.168.0.132 on eth0
              fe80::215:5dff:fe00:6400 on eth0
     Gateway: 192.168.0.1 on ens18
         DNS: 1.1.1.1

We need to record the 2 parameters of Gateway and DNS to configure in the next step

As of Ubuntu 20.04, network configurations are set up and managed by the netplan engine. To change to a static IP, we need to edit the file in the 00-installer-config.yaml/etc/netplan

sudo nano /etc/netplan/00-installer-config.yaml

For Ubuntu 22.04/24.04, edit the file contents to the following save (Ctrl+O) and exit (Ctrl+X)

# This is the network config written by 'subiquity'
network:
  ethernets:
    eth0:
       dhcp4: no
       addresses: [192.168.0.5/24]
      routes:
        - to: 0.0.0.0/0
          via: 192.168.0.1
       nameservers:
             addresses: [1.1.1.1, 1.0.0.1, 8.8.8.8, 8.8.4.4]

  version: 2

For Ubuntu 20.04, edit the file contents to the following save (Ctrl + O) and exit (Ctrl + X)

# This is the network config written by 'subiquity'
network:
  ethernets:
    eth0:
       dhcp4: no
       addresses: [192.168.0.5/24]
       gateway4: 192.168.0.1
       nameservers:
             addresses: [1.1.1.1, 1.0.0.1, 8.8.8.8, 8.8.4.4]

  version: 2

Depending on the subnet of the intranet you are using, you will adjust the addresses and gateways4 accordingly.
In the nameservers address line, you can use CloudFlare’s DNS (1.1.1.1 and 1.0.0.1) or Google (8.8.8.8 and 8.8.4.4) or both.

Next, run the command to apply the parameter you just changednetplan apply

sudo netplan apply

Double-check the IP of the machine, which is now set to the same as set in the netplan configuration file.192.168.0.5

ip addr show
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:00:64:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.5/24 brd 192.168.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fe00:6400/64 scope link
       valid_lft forever preferred_lft forever

That’s it. Your server already has a static IP address, ready to install services such as AdGuard Home, WireGuard, Nginx Proxy Manager,…

Leave a Reply

Your email address will not be published. Required fields are marked *