Update October 2024: Added instructions for Ubuntu 24.04
When setting up a virtual machine in the local network / homelab, you should set up a static IP for the machine to ensure services operate smoothly, 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 installed.
Set up static IP on Ubuntu Server
First check the current IP and network port of the computer with the command
ip addr show
Code language: Nginx (nginx)
2: eth0: 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
Code language: YAML (yaml)
The current IP of the eth0 network port is being set in dynamic IP mode from the Router’s DHCP Server.
Check gateway and DNS parameters with command
networkctl status
Code language: Nginx (nginx)
● 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
Code language: YAML (yaml)
We need to record 2 parameters Gateway and DNS to configure in the next step
Since Ubuntu 20.04, network configuration is set up and managed by the netplan tool. To change to a static IP, we need to edit the file 00-installer-config.yaml
in folder /etc/netplan
sudo nano /etc/netplan/00-installer-config.yaml
Code language: Nginx (nginx)
For Ubuntu 22.04/24.04edit the file Content as follows, 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
Code language: YAML (yaml)
For Ubuntu 20.04edit the file content as follows, 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
Code language: YAML (yaml)
Depending on the subnet of the internal network you are using, you will adjust the addresses and gateway4 accordingly.
In the nameservers address line, you can use CloudFlare’s DNS (1.1.1.1 and 1.0.0.1) or Google’s (8.8.8.8 and 8.8.4.4) or both. Or if you have AdGuard Home set up on your local network, you can replace it with the IP of the device where AdGuard Home is installed.
Next run the command netplan apply
to apply the newly changed parameters
sudo netplan apply
Code language: Nginx (nginx)
Check the machine’s IP again, it has now been converted to 192.168.0.5
as set in the netplan configuration file.
ip addr show
Code language: Nginx (nginx)
2: eth0: 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
Code language: YAML (yaml)
That’s it. Your server already has a static IP address, ready to install services such as AdGuard Home, WireGuard, Nginx Proxy Manager,…
If you are using a Raspberry Pi, refer to the article below on how to set up a static IP
Comment Policy: We truly value your comments and appreciate the time you take to share your thoughts and feedback with us.
Note: Comments that are identified as spam or purely promotional will be removed.
To enhance your commenting experience, consider creating a Gravatar account. By adding an avatar and using the same e-mail here, your comments will feature a unique and recognizable avatar, making it easier for other members to identify you.
Please use a valid e-mail address so you can receive notifications when your comments receive replies.