Use the nmcli command in RedHat/CentOS 7.x to manage the network
The default network service in Red Hat Enterprise Linux 7 and CentOS 7 is provided by NetworkManager, which dynamically controls and configures the network daemon. It is used to keep the current network device and connection in the working state, it also supports configuration files of the traditional ifcfg type.
NetworkManager can be used for the following types of connections: Ethernet, VLANS, Bridges, Bonds, Teams, Wi-Fi, mobile boradband (such as mobile 3G) and IP-over-InfiniBand. For these network types, NetworkManager can configure their network aliases, IP addresses, static routes, DNS, VPN connections, and many other special parameters.
You can use the command line tool nmcli to control NetworkManager.
Nmcli usage
# nmcli [ OPTIONS ] OBJECT { COMMAND | help }
You can use the TAB key to complete the command. When you forget the syntax of this command, you only need to press TAB to view the Option List.
Some examples of using nmcli:
# nmcli general status
This command prints out all the statuses of NetworkManager.
# nmcli connection show
Show all connections.
# nmcli connection show -a
Only active connections are displayed.
# nmcli device status
Lists devices identified by NetworkManager and their statuses.
Start/stop a network interface
Use the nmcli tool to start or stop the network interface, which is the same as the up/down of ifconfig.
Run the following command to stop an interface:
# nmcli device disconnect eno16777736
Run the following command to start the interface:
# nmcli device connect eno16777736
Add static IP Ethernet connection
Run the following command to add an Ethernet connection with a static IP Address:
# nmcli connection add type ethernet con-name NAME_OF_CONNECTION ifname interface-name ip4 IP_ADDRESS gw4 GW_ADDRESS
Modify the NAME_OF_CONNECTION, IP_ADDRESS, and GW_ADDRESS parameters according to the configuration you need (if you do not need a gateway, You can omit the last part ).
# nmcli connection add type ethernet con-name NEW ifname eno16777736 ip4 192.168.1.141 gw4 192.168.1.1
Run the following command to set the DNS server:
# nmcli connection modify NEW ipv4.dns "8.8.8.8 8.8.4.4"
Run the following command to start a new Ethernet connection:
# nmcli connection up NEW ifname eno16777736
View the configuration of the new connection:
# nmcli -p connection show NEW
Add a new connection using DHCP
Add a new connection and use DHCP to automatically allocate ip addresses, gateways, DNS, etc. All you need to do is remove the ip address/gw address after the command line, DHCP automatically assigns these parameters.
For example, configure a DHCP connection named NEW_DHCP on the eno 16777736 device.
# nmcli connection add type ethernet con-name NEW_DHCP ifname eno16777736