15

There are multiple wireless networks available with same SSID. I am looking for a solution to connect to a specific one (by BSSID).

nmcli con up ifname ap seems good, but, unfortunately, it's not working. It just connects back to the original network.

5 Answers 5

29

You can use Network Manager's cli interface, nmcli.

I'm sure you already have the BSSID. You can check and verify it with:
(the $ is the command prompt. The lines following the command is the output.)

$ nmcli -f in-use,ssid,bssid,signal,bars  dev wifi
*  SSID               BSSID              SIGNAL  BARS 
*  Apollo III (TWC)   XX:XX:XX:XX:XX:XX  98      ▂▄▆█ 
   Chromecast8481     XX:XX:XX:XX:XX:XX  97      ▂▄▆█ 
   --                 XX:XX:XX:XX:XX:XX  94      ▂▄▆█ 
   Apollo III (1)     XX:XX:XX:XX:XX:XX  87      ▂▄▆█ 
   TWCWiFi            XX:XX:XX:XX:XX:XX  80      ▂▄▆_ 
   CableWiFi          XX:XX:XX:XX:XX:XX  80      ▂▄▆_ 
   TWCWiFi-Passpoint  XX:XX:XX:XX:XX:XX  80      ▂▄▆_ 
   Apollo III (1)     XX:XX:XX:XX:XX:XX  70      ▂▄▆_

The cli for the connection to the BSSID is:

$ nmcli d wifi connect XX:XX:XX:XX:XX:XX

That command will give you a GUI prompt for the password. You could enter the password on the commanline (may be a security concern):

$ nmcli d wifi connect XX:XX:XX:XX:XX:XX password "mypassword"

The latter won't prompt for a password but will connect to the specified network by the BSSID in the command. If you were already previously connected to a different network, it will be replaced with the one specified in the command.

The command will also add the connection info to the /etc/NetworkManager/system-connections location. The location is protected.

Subsequent connections can be made via the network icon in the notification area by the name created.

3
  • 1
    uh... why do i have to provide a password? i am trying it on the BSSID I am already connected to, and it still asks for a password. can't i tell it to use the saved password it already has somewhere?
    – Michael
    Jul 16, 2020 at 21:09
  • 1
    Why in the world is nmcli showing ONLY 2.4Ghz connections? nmcli -f in-use,ssid,device,chan,freq,bssid,signal,bars dev wifi. The whole reason I'm on this page is to try to force my computer to connect to my mesh network on 5Ghz instead! Sep 1, 2020 at 7:13
  • 2
    This answer may be outdated or never worked exactly as intended. If the connection is known to your device, specifying the BSSID seems to just connect to the SSID as a whole, not the exact BSSID. See issue gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/….
    – sazerac
    Jan 14, 2022 at 21:51
7

The easy way to do this is to use the GUI provided by Network Manager.

Go to the network icon in the top panel, choose Edit Connections, locate the wireless connection profile for the desired SSID, open for EDIT, and go to the BSSID field and either 1)pop up the desired BSSID, or 2)manually enter the desired BSSID.

See below for an example wireless connection showing its BSSID field.

enter image description here

0
6

This answer was first posted by MariusMatutiae on SuperUser. Please consider voting there if this helps you.

You can do it by connecting to the AP manually.

First, it is easiest to turn off network manager, if you are running one:

  sudo service network-manager stop

Then you need to identify the BSSID of the AP you wish to join: the command

 sudo iw dev wlan0 scan

(if you are using wlan0 as your wireless interface) will produce a lot of output, among which you will find something like:

  BSS f8:1a:67:aa:7f:b9 (on wlan0) -- associated
    TSF: 629432841083 usec (7d, 06:50:32)
    freq: 2417
    beacon interval: 100
    capability: ESS Privacy ShortPreamble SpectrumMgmt ShortSlotTime (0x0531)
    signal: -70.00 dBm
    last seen: 0 ms ago
    Information elements from Probe Response frame:
    SSID: MySSID_NAME

(the output is longer than this). The relevant part is of course BSS f8:1a:67:aa:7f:b9.

Next, you will have to free your interface of any previous IP addresses, just in case:

  sudo ip link set wlan0 down
  sudo ip addr flush dev wlan0
  sudo ip link set wlan0 up

Now you specify you want to connect to the specific AP:

  sudo iwconfig wlan0 essid MySSID_NAME ap f8:1a:67:aa:7f:b9

where of course ap precedes the BSSID you just identified. Now you need to start wpa_supplicant,

 sudo wpa_supplicant -Dnl80211 -i wlan0 -B -c FILE_with_WPA_Secrets

(if you do not know how to set up the file with your WPA credentials, you may look it up here for instance; just be careful, where it says network= {, it should be network={ without a space). Lastly,

 sudo dhclient -v wlan0

(the -v flag does not work on all Linux distros, I like it because I can monitor what is happening).

EDIT

The instructions above work for a network with WPA security. Fore WEP security, replace the wpa_supplicant command with:

  sudo iwconfig wlan0 key s:Your_WEP_password

Remember that the two characters s: before your password are necessary. After this, once again

  sudo dhclient -v wlan0

Hope this helps.

3
  • ap f8:1a:67:aa:7f:b9 not working to me,I need re-run the steps multiple time since it connect random bssid. I check associated bssid with sudo iw dev wlan0 scan |& grep -P '(^BSS.*associated$)' -A 10 | grep -E 'associated|SSID' (run few time if no output)
    – 林果皞
    Nov 17, 2020 at 10:38
  • You should provide a link to the original answer so we can upvote it. I can't find it on SuperUser.
    – Dan
    Dec 3, 2020 at 18:21
  • @Dan I've updated my answer with the link to the referenced post.
    – TheOdd
    Dec 4, 2020 at 1:57
3

This is more of an expansion off TheOdd's answer. Since iwconfig is depricated in many distros, iw can be used to associate with a specific access point instead. iw works slightly differently than iwconfig in that you specify specific access points by frequency rather than BSSID. Also, I found it necessary to release the DHCP lease before requesting another one from a different access point. Assuming you're already connected to an access point on the same network, you don't need to re-submit authentication with wpa_supplicant. I'm using wlan0, but your WNIC might have a different name. Here's the complete process of switching between access points with the same ESSID:

show connection info:

iw wlan0 link

take the wlan0 down:

sudo ip link set wlan0 down

free WNIC of any previous IP addresses:

sudo ip addr flush dev wlan0

remove old dhcp lease:

sudo dhclient -r wlan0

bring WNIC back up:

sudo ip link set wlan0 up

scan, take note of freq:

sudo iw dev wlan0 scan

specify ESSID and frequency with iw (not iwconfig) to assoate with specific acces point:

sudo iw wlan0 connect <ESSID> <freq>

for example:

sudo iw wlan connect fly-for-wifi 2462

run dhcp:

sudo dhclient -v wlan0

check:

iw wlan0 link

2

This answer is equivalent to the answer that explains how to use NetworkManager's GUI, but use the CLI.

  1. (optional, used to list all the available Wi-Fi and their BSSID)
    nmcli device wifi list
    
  2. Edit the BSSID field:
    nmcli connection edit ConnectionName
    set 802-11-wireless.bssid 12:34:56:AB:CD:EF
    save
    quit
    
    or:
    nmcli connection modify ConnectionName 802-11-wireless.bssid 12:34:56:AB:CD:EF
    
    where ConnectionName is usually identical to the ESSID if it's automatically created (you can list all stored connection names with nmcli connection), replace 12:34:56:AB:CD:EF with the connection ID.
  3. Use the connection profile:
    nmcli connection up ConnectionName
    

Note that this way when that BSSID is not available, it will not automatically connect to any other BSSID.

There are some possible fixes.

  • Manually revert the change.

    Do the same thing but in step 2 you need to do:

    nmcli connection edit ConnectionName
    remove 802-11-wireless.bssid
    save
    quit
    

    or

    nmcli connection modify ConnectionName 802-11-wireless.bssid ''
    
  • Create a new connection, cloning the existing one.

    nmcli connection delete ConnectionName-cloned  # delete all existing connections named "ConnectionName-cloned"
    nmcli connection clone ConnectionName ConnectionName-cloned  # create a new connection with name "ConnectionName-cloned" copying from the connection named "ConnectionName". Note that if there is an existing one it will create a new one with the same name
    nmcli connection modify ConnectionName-cloned 802-11-wireless.bssid 12:34:56:AB:CD:EF  # as above
    nmcli connection up ConnectionName-cloned  # as above
    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .