Setup Ubuntu Static IP
To configure a static IP address on Ubuntu, you will need to edit the network (netplan) configuration files.
Step 1: Find your network interface
ip addrLook for the interface that is currently connected to the network. For my cased, it is wlo1.
kc@kchomelab:~$ ip addr
3: wlo1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether a0:29:42:00:3c:78 brd ff:ff:ff:ff:ff:ff
altname wlp0s20f3
inet 192.168.0.20/24 brd 192.168.0.255 scope global dynamic noprefixroute wlo1
valid_lft 86009sec preferred_lft 86009sec
inet6 2001:e68:545b:1e09:331:a36c:d781:b2ad/64 scope global temporary dynamic
valid_lft 209331sec preferred_lft 85891sec
inet6 2001:e68:545b:1e09:20ce:6713:d2e1:733a/64 scope global dynamic mngtmpaddr noprefixroute
valid_lft 209331sec preferred_lft 122931sec
inet6 fe80::c7b2:63f6:791c:7ef9/64 scope link noprefixroute
valid_lft forever preferred_lft foreverStep 2: Create or Edit the netplan configuration file
Let’s first list the files in the /etc/netplan/ directory. You should see a lists of YAML files. You can either create a new file or edit an existing one.
kc@kchomelab:~$ ls /etc/netplan
01-network-manager-all.yaml 90-NM-904e969d-0545-4cb6-98f5-3b150b90657e.yaml
50-cloud-init.yamlFor my case, I will create a new file called 99-static-ip.yaml, there’s because by using a higher prefix like 99-, it ensures that this configuration will take priority over the earlier files like 01- or 50-.
To get the gateway address (route), you can use the following command:
kc@kchomelab:~$ ip route | grep default
default via 192.168.0.1 dev wlo1 proto dhcp src 192.168.0.20 metric 600network:
version: 2
renderer: NetworkManager
# ethernets: # for wired connection, use 'ethernets'
wifis: # for wireless connection, use 'wifis'
wlo1: # use the actual interface name
dhcp4: no # this overrides the 'yes' in the 50-cloud-init file
dhcp6: no # this overrides the 'yes' in the 50-cloud-init file
addresses: # static IP
- 192.168.0.20/24
routes:
- to: default
via: 192.168.0.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4, 1.1.1.1]
access-points: # for wireless connection only
"kc_5G":
password: <wireless-password>- nameservers must be set as computer only speak in IP address format, but we use domain names to access websites, so in order to translate domain names to IP addresses, we need to use nameservers.
8.8.8.8= Google Public DNS8.8.4.4= Google Secondary Public DNS1.1.1.1= Cloudflare Public DNS
Step 3: Apply the netplan configuration
Try the configuration first to se if there are any errors:
kc@kchomelab:~$ sudo netplan try
** (process:13133): WARNING **: 14:01:57.841: Permissions for /etc/netplan/01-network-manager-all.yaml are too open. Netplan configuration should NOT be accessible by others.
** (process:13133): WARNING **: 14:01:57.842: Permissions for /etc/netplan/99-static-ip.yaml are too open. Netplan configuration should NOT be accessible by others.Fix the “Permissions are too open” warning by changing the file permissions.
sudo chmod 600 /etc/netplan/01-network-manager-all.yaml
sudo chmod 600 /etc/netplan/99-static-ip.yamlIf everything looks good, apply the configuration:
kc@kchomelab:~$ sudo netplan try
Do you want to keep these settings?
Press ENTER before the timeout to accept the new configuration
Changes will revert in 106 seconds
Configuration accepted.
kc@kchomelab:~$ sudo netplan applyStep 4: Check Static IP setup successfully?
If you still see dynamic keyword from ip addr or proto dhcp keyword from ip route command. You might need to do some configuration.
First, disable cloud-init network management. This is dealing with 50-cloud-init.yaml to avoid conflicts.
echo "network: {config: disabled}" | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfgNext, if you have multiple files in /etc/netplan, delete or rename the ones you aren’t using, for my case, I create a backup folder and move those files into backup folder to avoid merging conflicts.
mkdir backup
mv 50-cloud-init.yaml 90-NM-*.yaml backup/Remove the old dynamic lease and apply the netplan configuration.
sudo ip addr flush dev <interface-name>
sudo netplan applyYou can check whether the static IP is setup successfully by using two commands:
ip addrip route
For ip addr command, you will notice the dynamic keyword is gone.
kc@kchomelab:/etc/netplan$ ip addr show wlo1
3: wlo1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether a0:29:42:00:3c:78 brd ff:ff:ff:ff:ff:ff
altname wlp0s20f3
inet 192.168.0.20/24 brd 192.168.0.255 scope global noprefixroute wlo1
valid_lft forever preferred_lft forever
inet6 2001:e68:545b:1e09:435:c1ff:b38f:d374/64 scope global temporary dynamic
valid_lft 206237sec preferred_lft 86014sec
inet6 2001:e68:545b:1e09:a229:42ff:fe00:3c78/64 scope global dynamic mngtmpaddr
valid_lft 206237sec preferred_lft 119837sec
inet6 fe80::a229:42ff:fe00:3c78/64 scope link
valid_lft forever preferred_lft foreverFor ip route command, you will notice proto static.
kc@kchomelab:/etc/netplan$ ip route | grep default
default via 192.168.0.1 dev wlo1 proto static metric 600One final test to ensure the internet routing and DNS are working correctly:
kc@kchomelab:~$ ping -c 3 google.com
PING google.com (2404:6800:4001:804::200e) 56 data bytes
64 bytes from pnkula-ah-in-x0e.1e100.net (2404:6800:4001:804::200e): icmp_seq=1 ttl=120 time=10.9 ms
64 bytes from pnkula-ah-in-x0e.1e100.net (2404:6800:4001:804::200e): icmp_seq=2 ttl=120 time=13.4 ms
64 bytes from pnkula-ah-in-x0e.1e100.net (2404:6800:4001:804::200e): icmp_seq=3 ttl=120 time=12.2 ms
--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 10.868/12.135/13.378/1.024 ms