Question: How to use nmcli to add an Ethernet connection with manual or via DHCP configuration?
The NetworkManager command line tool, nmcli, provides those who prefer to use the command line way to configure networking by controlling NetworkManager. It is installed along with NetworkManager.
Use nmcli with the object connection and command add. (This can be abbreviated to con add). The first step should be to have the latest version of NetworkManager package, thus upgrade the same if its not latest;
# yum update NetworkManager
Note: Another suggested package is to have the latest version of bash-completion for auto-completion of nmcli command when tab key is pressed. This helps in displaying available nmcli command options on screen and reduces the chances of an administrator manually adding wrong options.
To setup ethernet, configure following command options:
- con-name – The connection name is the name of a connection profile and should not be confused with the interface name that denotes a device.
- ifname – Interface name.
- type – Allowed values are: ethernet, wifi, wimax, gsm, cdma, infiniband, bluetooth, vlan, bond, bond-slave.
- ipv4.method – To specific if interface configuration is via dhcp (auto) or static (manual).
- ipv4.address – IPv4 address in CIDR representation.
- ipv4.gateway – IPv4 Gateway address.
- ipv6.address – IPv6 address(optional if IPv6 enabled)
IP configuration via DHCP
To configure the ethernet connection to get an IP address assigned automatically, use the below command syntax:
# nmcli connection add type ethernet ifname [interface-name] con-name [interface-name] ipv4.method auto
Static IP configuraiton
1. For a static IP address configuration for the new ethernet connection, use the below command syntax:
# nmcli connection add type ethernet ifname [interface-name] con-name [interface-name] ipv4.method manual ipv4.address [IP]/[prefix] ipv4.gateway [GW-address]
2. DNS nameservers can only be added after the connection has already been created.
# nmcli connection modify [ethernet-name] ipv4.dns "DNS1-IP DNS2-IP"
3. Bring UP or the load the configuration.
# nmcli connection up [interface-name]