How to use nmcli to add new Ethernet connection?

Solution Verified - Updated -

Environment

  • Red Hat Enterprise Linux 7 and Above
  • NetworkManager

Issue

  • How to use nmcli to add an Ethernet connection with manual or via DHCP configuration?

Resolution

  • 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
  • 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

# nmcli connection add type ethernet ifname <interface-name> con-name <interface-name> ipv4.method auto

Static IP configuration

# nmcli connection add type ethernet ifname <interface-name> con-name <interface-name> ipv4.method manual ipv4.address <IP>/<prefix> ipv4.gateway <GW-address>
  • DNS nameservers can only be added after the connection has already been created.
# nmcli connection modify <ethernet-name> ipv4.dns "DNS1-IP DNS2-IP"
  • Bring UP or the load the configuration.
# nmcli connection up <interface-name>

Root Cause

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.

Comments