Set Static IP via CLI (command line)

The majority of Ubuntu’s network interfaces are managed by NetworkManager. It is very important to use nmcli instead of ifconfig to set a static IP.

  1. Check if the current OS is running any NetworkManager service:

ps aux | grep -E 'dhclient|NetworkManager|systemd-networkd'
# root         740  0.0  0.2 336976 20660 ?        Ssl   十 15   0:15 /usr/sbin/NetworkManager --no-daemon

  1. Using enp5s0 as an example, to set a static IP of 7.7.7.5 with a netmask of 255.255.255.0, run:

sudo nmcli connection modify enp5s0 ipv4.method manual ipv4.addresses 7.7.7.5/24
sudo nmcli connection down enp5s0
sudo nmcli connection up enp5s0

ifconfig enp5s0
# enp5s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
#         inet 7.7.7.55  netmask 255.255.255.0  broadcast 7.7.7.255
#         ether 78:d0:04:36:fe:62  txqueuelen 1000  (Ethernet)
#         RX packets 232  bytes 21965 (21.9 KB)
#         RX errors 0  dropped 0  overruns 0  frame 0
#         TX packets 294  bytes 28992 (28.9 KB)
#         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Last updated