how do I attach devices to connections using nmcli?

34,858

Solution 1

The easiest would be

nmcli device wifi connect <name ssid> password <the password>

For a solution with the password as variable: https://github.com/aurelien-git/bash/blob/master/wifi.sh

This script offer you the choice of selection and hidden passord

Solution 2

The short answer is:

# nmcli con modify my-bridge connection.interface-name ens7
# nmcli con up my-bridge

However, it's never that simple - read on...

Three things are needed for the connection to be attached to the device:

  1. A valid network device
  2. The connection.autoconnect property to be set to yes
  3. The connection.interface-name property is set to the name of the interface

Make sure that you have a working NIC (virtual in a VM). This is outside the scope of this answer as there are so many options.

Check the state of the connection.autoconnect property with:

# nmcli con show my-bridge | grep connection.autoconnect:
connection.autoconnect:                  no

and if need be, change it with:

# nmcli con modify my-bridge connection.autoconnect yes

Check the state of the connection.interface-name with:

# nmcli con show my-bridge | grep connection.interface-name
connection.interface-name           --

and if need be, set it with:

# nmcli con modify my-bridge connection.interface-name ens7

Solution 3

If you have a device with two wifi adapters, and you want to use the second one, then you should do the following commands. I used this with my AWS DeepLens, since the internal WiFi antenna is quite basic, but my USB WiFi adapter is very very good:

# Find the device name (such as wlan0) of your good adapter
ip a
ifconfig

# Ensure that NetworkManager sees the device
nmcli d

# Ensure that the device is detected as a Wifi adapter:
# NOTE: Replace "wlan0" with the name of your device
nmcli d show wlan0

# Ensure that the device can look around and do a scan of nearby networks:
nmcli d wifi list ifname wlan0

# Connect to the Wifi with the given password
# NOTE: Replace "MyHomeNetwork" with your Wifi network SSID
#       and replace "Sup3r-secret-password" with your password,
#       If your password contains any special characters, such as the $ below
#         I highly recommend surrounding it in single quotes, this is a shell thing,
#         not a nmcli thing.
nmcli d wifi connect MyHomeNetwork password 'Sup3r-$ecret-password' ifname wlan0

# Make sure it worked!
nmcli d
ip a
ifconfig

# PROFIT!
Share:
34,858

Related videos on Youtube

RabT
Author by

RabT

Updated on September 18, 2022

Comments

  • RabT
    RabT over 1 year

    An installation of CentOS 7 has two connections and three devices. How can I attach the device ens7 to the connection my-bridge? And how can I attach the device eth0 to the connection my-eth1?

    Here is the relevant data from the terminal:

    [root@localhost ~]# nmcli con show
    NAME       UUID          TYPE            DEVICE 
    my-bridge  some.uuid     802-3-ethernet  --     
    my-eth1    another.uuid  802-3-ethernet  --     
    
    [root@localhost ~]# nmcli device status
    DEVICE  TYPE      STATE         CONNECTION 
    ens7    ethernet  disconnected  --         
    eth0    ethernet  disconnected  --         
    lo      loopback  unmanaged     --         
    [root@localhost ~]# ping 8.8.8.8
    connect: Network is unreachable
    

    I think it is something like nmcli connection modify id my-bridge ens7 but I am not sure of the exact syntax.

    Also, the problem may have to do with the fact that the my-bridge connection (for example) was created in this way:

    # nmcli con add con-name my-bridge ifname eth1 type ethernet ip4 10.1.1.2/24
    # nmcli connection modify my-bridge ipv4.method manual ipv4.addresses 10.1.1.1/24
    

    An answer would either show the exact syntax to type in the terminal for attaching the devices to the connections, or alternatively, the syntax for creating new connections that are automatically attached to devices from the start.

  • RabT
    RabT over 6 years
    Thank you and +1 for documenting another approach. But leaving passwords exposed is a security risk. How would you secure the password?
  • aurelien
    aurelien over 6 years
    Not sure nmcli offer solution for that, but I will have a look ...
  • RabT
    RabT over 6 years
    Maybe an environmental variable.
  • aurelien
    aurelien over 6 years
    @CodeMed I add a script with password environnemental variable.
  • aurelien
    aurelien over 6 years
    And for information it works like a charm in Emacs shell :-)
  • aurelien
    aurelien over 6 years
    I update information on how to configure it to works on your system. By that way as simple user when you type wifi in your terminal you will have the choice ;-)