Creating a keyfile Connection Profile Using nmcli

To illustrate the different uses of the nmcli command, this procedure describes an example of adding and configuring a new Ethernet connection for the enp0s2 device. For more information about the command, see the nmcli(1) manual page.

Tip:

Before adding the connection, prepare the information you would need for the configuration, such as the following:
  • Connection name, for example, My Work Connection. The nmcli command works by referring to the connection name rather than the device name. If you don't set a connection name, then the device's name is used as the connection name.
  • IP addresses (IPv4 and, if needed, IPv6)
  • Gateway addresses
  • Other relevant data you want to set for the connection
  1. Display the network devices on the system.
    sudo nmcli device status
    DEVICE  TYPE      STATE          CONNECTION
    enp0s1  ethernet  connected      enp0s1   
    enp0s2  ethernet  disconnected    --   
    lo      loopback  unmanaged
    

    The command shows whether a device is connected or disconnected, and whether it is managed or unmanaged.

  2. Display the connection information about the network devices.
    sudo nmcli con show --active
    NAME     UUID                                TYPE      DEVICE
    enp0s1   nn-nn-nn-nn-nn  ethernet  enp0s1
    virbr0   nn-nn-nn-nn-nn  bridge    virbr0
    mybond   nn-nn-nn-nn-nn  bond      bond0

    The con subcommand is the short form of connection, and can be further shortened to c. Specifying the --active option displays only active devices.

    Note:

    In the output, NAME represents the connection ID.
  3. Add a new connection.
    sudo nmcli con add type connection type {properties} [IP-info] [gateway-info
    type
    (Required) Specifies a known NetworkManager connection type.

    For a list of allowed type values, see the nmcli connection add section in the nmcli(1) manual page.

    properties
    The connection name as specified by the con-name argument, and the interface name as specified by the ifname argument.
    IP-info
    The IPv4 or IPv6 address as specified by either the ip4 or ip6 argument. The address must be in the format address/netmask. The IPv4 address can be in CIDR form, for example, 1.2.3.4/24.
    gateway-info
    The gateway IPv4 or IPv6 address as specified by either the gw4 or gw6 argument.

    For example, to add the connection with the information at the beginning of this procedure, run the following command:

    sudo nmcli con add type ethernet ifname enp0s2 con-name "My Work Connection" ip4 192.168.5.10/24 gw4 192.168.5.2

    The output would acknowledge that the connection is successfully completed.

  4. Activate the interface.
    sudo nmcli con up "My Work Connection"
  5. Display the configuration properties of the new connection.
    sudo nmcli -o con show "My Work Connection"
    connection.id:               My Work Connection
    connection.uuid:             nn-nn-nn-nn-nn
    connection.type:             802-3-ethernet
    connection.interface-name:   enp0s2
    ...
    IP4.ADDRESS[1]:              192.168.5.10
    IP4.GATEWAY:                 192.168.5.2
    ...

    Specifying the -o option displays only properties that have configured values.

After you have created the connection, a corresponding profile is created. For more information on connection profiles, see NetworkManager Connection Profiles.

ls -lrt /etc/NetworkManager/system-connections/
...
-rw-r--r--. 1 root root 266 Aug  6 11:03 /etc/sysconfig/network-scripts/ifcfg-My_Work_Connection