Skip to content

Instantly share code, notes, and snippets.

@plembo
Last active March 29, 2024 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plembo/486cccdebc824337506441987223d42b to your computer and use it in GitHub Desktop.
Save plembo/486cccdebc824337506441987223d42b to your computer and use it in GitHub Desktop.
nmcli commands for static networking

nmcli commands for static networking

This is for a common use case: setting up static networking from the command line on a Linux system that uses NetworkManager.

For all its faults, I like NetworkManager because of its versatility. Here are the commands to use on a new Linux machine to take it from DHCP to static networking. On some systems you may need to execute these sudo root.

Do all this at the host console if possible, not over the network. Connectivity will be lost during these changes.

I like to start with renaming the connection after the device it controls. Connection names are case-sensitive.

The hardware device in these examples has been assigned "ens3" as its device name. This will almost certainly be different on different machines. Use ip addr to retrieve the name for yours.

nmcli con mod 'Wired connection 1' con-name ens3

Now on to the main event. The example network is 10.0.0.0/24, with its gateway at 10.0.0.1 and internal name server (DNS) at the same address.

nmcli con mod ens3 ipv4.addresses 10.0.0.15/24
nmcli con mod ens3 ipv4.gateway 10.0.0.1
nmcli con mod ens3 ipv4.dns '10.0.0.1,8.8.8.8'
nmcli con mod ens3 ipv4.dns-search 'example.com'

Switch from DHCP to static.

nmcli con mod ens3 ipv4.method manual

Finally, bring the interface back up.

nmcli con up ens3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment