Setting IP Address on Linux
Table of Contents
Problem
I have a 22.04LTS Ubuntu machine and I want to change the IP address on it. There are several methods to check the current IP address:
ip a
(shorthand, ip addr
, ip address
or ip addr show
. shows info for all interfaces)
ifconfig
(needed installation)
hostname -I
(concise output)
nmcli device show
(similar to #1)
Solution
the routine to change the IP address includes disabling the interface, changing IP and enabling it again:
ip link set <interface> down
ip addr add <ip>/<subnet> dev <interface>
ip link set <interface> up
however I came across a method using netplan
, by modifying a .yaml
config file. this is how it goes:
- locate config
netplan
files: ls /etc/netplan
- the contents in that folder depends on the machine:
- user-defined configs are typically named
01-netcfg.yaml
- config created by
NetworkManager
is often named 01-network-manager-all.yaml
00-installer-config.yaml
is a common name for configs that are generated while installing, often on a server, and can contain network defaults
- filenames are important, since
netplan
processes files alphabetically (so 00-*
files are processed before 01-*
files, etc.)
- the renderer also matters, and determines how the settings are applied (e.g., using
NetworkManager
or networkd
)
- backup the original config (in case you mess something up like I did)
- edit the proper
.yaml
file
netplan apply
ping
to verify
this was my initial config :
# Let NetworkManager manage all devices on this system
network:
version: 2
renderer: NetworkManager
an example config looks like below:
network:
version: 2
renderer: networkd # or NetworkManager
ethernets:
enp0s3: # Replace with your interface name
dhcp4: no
addresses:
- 192.168.1.100/24 # Replace with your desired static IP
gateway4: 192.168.1.1 # Replace with your gateway IP
nameservers:
addresses:
- 8.8.8.8 # Primary DNS
- 8.8.4.4 # Secondary DNS
⚠️ YAML files
human-readable markup language
highly sensitive to whitespaces, indentations, case/caps, etc.
take extra care
Sources
- https://pimylifeup.com/ubuntu-static-ip-netplan/
- https://ostechnix.com/configure-static-ip-address-ubuntu/
- https://www.serverlab.ca/tutorials/linux/administration-linux/how-to-configure-networking-in-ubuntu-20-04-with-netplan/