How to Use nmcli to manage networks in RedHat/CentOS 7.x
A new feature of Red Hat Enterprise Linux 7 and CentOS 7 is that the default network service is provided by NetworkManager, which is a dynamic network control and configuration daemon process, it maintains a normal connection between the network device and the connection. It also provides a typical ifcfg configuration file. NetworkManager can be used for the following connections: Ethernet, VLANs, bridging, Bonds, Teams, Wi-Fi, mobile broadband (such as 3G), and IP-over-InfiniBand (IPoIB ).
NetworkManager can be controlled by the command line tool nmcli.
Common usage of nmcli
The common syntax of nmcli is:
- # Nmcli [OPTIONS] OBJECT {COMMAND | help}
One cool thing is that you can use the tab key to complete the operation, so that when you forget the syntax, you can press tab to see the available options.
Examples of common usage of nmcli:
- # Nmcli general status
The overall status of NetworkManager is displayed.
- # Nmcli connection show
All connections are displayed.
- # Nmcli connection show-
Show active connections only
- # Nmcli device status
Displays the list of devices recognized by NetworkManager and their current status.
Start/stop a network device
You can use nmcli to start or stop network devices from the command line. This is equivalent to up and down in ifconfig.
Stop a network device and use the following syntax:
- # Nmcli device disconnect eno16777736
To start it, use the following syntax:
- # Nmcli device connect eno16777736
Add an Ethernet connection using a static IP Address
To add an Ethernet connection to a static IP address, run the following command:
- # Nmcli connection add type ethernet con-name NAME_OF_CONNECTION ifname INTERFACE-NAME ip4 IP_ADDRESS gw4 GW_ADDRESS
Set NAMEOFReplace CONNECTION with the new connection name (LCTT: This NAME can be used to operate on it later, you can use any simple and clear NAME), INTERFACE-NAME with your interface name, IPReplace ADDRESS with the ip address you want, GWReplace ADDRESS with the gateway ADDRESS you are using (if you are not using the gateway, you can ignore this part ).
- # Nmcli connection add type ethernet con-name NEW_STATIC ifname eno167736 ip4 192.168.1.141 gw4 192.168.1.1
Use the following command to set the DNS server used for this connection:
- # Nmcli connection modify NEW_STATIC ipv4.dns "8.8.8.8 8.4.4"
To enable a new Ethernet connection, run the following command:
- # Nmcli connection up NEW_STATIC ifname eno16777736
To view the detailed information of the newly configured connection, run the following command:
- # Nmcli-p connection show NEW_STATIC
Add a connection using DHCP
If you want to add a new connection using DHCP to configure the interface ip address, gateway address, and dns server address, all you need to do is to ignore the ip/gw part in the preceding command, networkManager automatically uses DHCP to obtain configuration details.
For example, to create a new DHCP connection named NEW_DHCP, you can use the following command on the eno16777736 device:
- # Nmcli connection add type ethernet con-name NEW_DHCP ifname eno16777736