Today we’ll look over configuring various network settings with nmcli, the command line tool for NetworkManager.
The first step is to understand how network configuration settings look. Inside the /etc/sysconfig/network-scripts/ directory, you can find config files for your network interfaces, under the format of ifcfg-name. On my system, it looks like this:
You can find a more complete overview of the options at https://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-networkscripts-interfaces.html , but from a glance at this file, we see the interface is an Ethernet device, it uses DHCP, it’s set as the default route for both IPv4 and IPv6 traffic, it’s activated at boot time, and also some other details like its MAC address and name.
nmcli is a very powerful tool, and you can press Tab to see options for its use at every stage.
123456789101112131415161718192021222324
nmcli help
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }
OPTIONS
-t[erse] terse output
-p[retty] pretty output
-m[ode] tabular|multiline output mode
-c[olors] auto|yes|no whether to use colors in output
-f[ields] <field1,field2,...>|all|common specify fields to output
-e[scape] yes|no escape columns separators in values
-a[sk] ask for missing parameters
-s[how-secrets] allow displaying passwords
-w[ait] <seconds> set timeout waiting for finishing operations
-v[ersion] show program version
-h[elp] print this help
OBJECT
g[eneral] NetworkManager's general status and operations
n[etworking] overall networking control
r[adio] NetworkManager radio switches
c[onnection] NetworkManager's connections
d[evice] devices managed by NetworkManager
a[gent] NetworkManager secret agent or polkit agent
m[onitor] monitor NetworkManager changes
nmcli dev status
DEVICE TYPE STATE CONNECTION
virbr0 bridge connected virbr0
enp0s3 ethernet connected Wired connection 1
lo loopback unmanaged --
virbr0-nic tun unmanaged --
look at the available connections and list only the active ones
123456789
nmcli con show
NAME UUID TYPE DEVICE
Wired connection 1 8d922966-5be4-4633-a99d-71e836e2b31a 802-3-ethernet enp0s3
virbr0 321062a9-495a-4095-b4b5-d1af9c0cc65c bridge virbr0
enp0s3 b4c32cc7-c5ff-451c-8af2-9b9124050f99 802-3-ethernet --
nmcli con show --active
NAME UUID TYPE DEVICE
Wired connection 1 8d922966-5be4-4633-a99d-71e836e2b31a 802-3-ethernet enp0s3
virbr0 321062a9-495a-4095-b4b5-d1af9c0cc65c bridge virbr0
list the settings of a particular connection (the double quotes aren’t mandatory)
nmcli con add con-name newdhcp type ethernet ifname enp0s3
Connection 'newdhcp' (8c90fcd7-5e40-43c6-819b-47cab3051b5e) successfully added.
nmcli con up newdhcp
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
nmcli con show --active
NAME UUID TYPE DEVICE
newdhcp 8c90fcd7-5e40-43c6-819b-47cab3051b5e 802-3-ethernet enp0s3
virbr0 321062a9-495a-4095-b4b5-d1af9c0cc65c bridge virbr0
add a static connection
12
nmcli con add con-name static-con ifname enp0s3 autoconnect no type ethernet ip4 192.168.241.120/24 gw4 192.168.241.2
Connection 'static-con' (258c2964-ac43-4ed8-bffa-7ad09967b748) successfully added.
nmcli con up static-con
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
nmcli con show --active
NAME UUID TYPE DEVICE
static-con 258c2964-ac43-4ed8-bffa-7ad09967b748 802-3-ethernet enp0s3
virbr0 321062a9-495a-4095-b4b5-d1af9c0cc65c bridge virbr0
ifconfig | grep inet
inet 192.168.241.120 netmask 255.255.255.0 broadcast 192.168.241.255
modifying connections
12345678910111213141516171819
nmcli con mod help
Usage: nmcli connection modify { ARGUMENTS | help }
ARGUMENTS := [id | uuid | path] <ID> ([+|-]<setting>.<property> <value>)+
Modify one or more properties of the connection profile.
The profile is identified by its name, UUID or D-Bus path. For multi-valued
properties you can use optional '+' or '-' prefix to the property name.
The '+' sign allows appending items instead of overwriting the whole value.
The '-' sign allows removing selected items instead of the whole value.
Examples:
nmcli con mod home-wifi wifi.ssid rakosnicek
nmcli con mod em1-1 ipv4.method manual ipv4.addr "192.168.1.2/24, 10.10.1.5/8"
nmcli con mod em1-1 +ipv4.dns 8.8.4.4
nmcli con mod em1-1 -ipv4.dns 1
nmcli con mod em1-1 -ipv6.addr "abbe::cafe/56"
nmcli con mod bond0 +bond.options mii=500
nmcli con mod bond0 -bond.options downdelay
add a DNS server to an existing connection
123456789101112
nmcli con show static-con | grep ipv4.dns
ipv4.dns:
ipv4.dns-search:
ipv4.dns-options: (default)
ipv4.dns-priority: 0
nmcli con mod static-con ipv4.dns 8.8.8.8
nmcli con up static-con
nmcli con show static-con | grep ipv4.dns
ipv4.dns: 8.8.8.8
ipv4.dns-search:
ipv4.dns-options: (default)
ipv4.dns-priority: 0