DEV Community

loading...

Connecting to a wireless network on Ubuntu Server 21.04 and Raspberry Pi 4

Raine Virta
・1 min read

Problems solved here:

  • Connecting to a wireless network on Ubuntu Server 21.04 and Raspberry Pi 4
  • Prefer 5 GHz WiFi when 2.4 GHz exists with the same SSID

Add the wifi network configuration to /etc/netplan/50-cloud-init.yaml. Looks like this file is generated by netplan somehow and there's probably a better way but this persists through restarts so it's good enough for now.

$ sudo vim /etc/netplan/50-cloud-init.yaml
Enter fullscreen mode Exit fullscreen mode
network:
    ethernets:
        eth0:
            dhcp4: true
            optional: true
    version: 2
    wifis:
        wlan0:
            optional: true
            dhcp4: true
            access-points:
                "{{WIFI NAME}}":
                    password: "{{WIFI PASSWORD}}"

Enter fullscreen mode Exit fullscreen mode

Raspberry Pi should connect to the network after restarting.

Next problem: It is preferring the 2.4 GHz wifi over 5 GHz. Or at least it was for me.

We need to hardcode the basic service set identifier (BSSID) of 5 GHz network in the above configuration.

Find out the BSSID for the 5 GHz network:

# Will not return results without sudo
$ sudo iwlist wlan0 scan
Enter fullscreen mode Exit fullscreen mode

In the output, look for an entry like this for the 5 Ghz network:

Cell 01 - Address: 2C:FD:A1:DD:D3:34
          Channel:36
          Frequency:5.18 GHz (Channel 36)
          Quality=56/70  Signal level=-54 dBm
          Encryption key:on
          ESSID:"{{WIFI NAME}}"
Enter fullscreen mode Exit fullscreen mode

The thing after Address:Β is the BSSID.

Add it to /etc/netplan/50-cloud-init.yaml on same indentation level as password.

network:
    ethernets:
        eth0:
            dhcp4: true
            optional: true
    version: 2
    wifis:
        wlan0:
            optional: true
            dhcp4: true
            access-points:
                "{{WIFI NAME}}":
                    password: "{{WIFI PASSWORD}}"
                    bssid: "2C:FD:A1:DD:D3:34"
Enter fullscreen mode Exit fullscreen mode

Done. Restart and Raspberry Pi should connect to the 5 Ghz network.

Discussion (0)