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

How to configure WiFi on Ubuntu Server with Raspberry Pi 4, and prefer 5 GHz over 2.4 GHz when both use the same SSID.

· 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
/etc/netplan/50-cloud-init.yaml
network:
  ethernets:
    eth0:
      dhcp4: true
      optional: true
  version: 2
  wifis:
    wlan0:
      optional: true
      dhcp4: true
      access-points:
        '{{WIFI NAME}}':
          password: '{{WIFI PASSWORD}}'

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

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}}"

The thing after Address: is the BSSID.

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

/etc/netplan/50-cloud-init.yaml
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'

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

Thanks for reading! You can check out my projects on GitHub or follow me on Twitter.