How to Set Up a Wireless Ubuntu Server on Raspberry Pi

Sergejs Kozlovičs
6 min readMar 21, 2021

Works also with Ubuntu 21.04 Hirsute Hippo un 21.10 Impish Indri!

No LAN cable is required. Use any power outlet within the radius of your WiFi router! The 5GHz ac standard for Raspberry Pi 4 also covered.

Prerequisites

I suppose that you have a wireless router and know how to configure it. Besides, you will need some storage medium for Raspberry Pi.

If you are looking for an elegant solution, I suggest investing in a capacious microSD card. A microSD card is needed to boot Raspberry Pi. A capacious microSD card allows you to go without an external hard drive. A microSD card does not need much power to operate; it occupies less physical space, it is fast and noiseless.

For a reasonable quality/price ratio, I would recommend the Sandisk Ultra series. Notice, however, that disks are measured in gigabytes, GB (not gibibytes, GiB). 1 GB=10⁹ bytes.

If you still wish to plug in an external 2.5-inch HDD with just a USB cable, be ready that non-original Raspberry Pi power adapters may have not enough power during the initial boot process to spin up your external HDD. Thus, you will need a powered USB hub for that. 3.5-inch disks always require an external power source.

32-bit or 64-bit?

Both Raspberry Pi 3 and 4 have 64-bit processors. Thus, I recommend installing the 64-bit Ubuntu there. Since the Raspberry Pi 3 comes with just 1 GiB of RAM, you may think that 64-bit pointers are not suitable for such a small amount of memory. Nevertheless, the 64-bit architecture is the future, and you will have fewer compatibility issues in the long term. For Raspberry Pi 4, I recommend getting at least the 4 GiB model.

Raspberry Pi processors implement the ARM architecture. In *NIX systems, 32-bit ARM is usually denoted as “armhf” but the 64-bit ARM is denoted “aarch64” (sometimes “arm64”). Although the ARM processors out-of-the-box cannot execute code compiled for the x86 (Intel/AMD) architecture, Linux has the qemu-based compatibility layer that can execute x86 code on ARM systems.

Raspberry Pi Imager

The easiest way to install an OS to a Raspberry Pi is by means of the Raspberry Pi Imager tool. Get it for your current PC OS. It is able to download an OS image and write it to the SD card.

Click “CHOOSE OS,” then Other general purpose OS→Ubuntu→Ubuntu Server 20.04.x LTS (64-bit server OS).

Afterward, “CHOOSE SD CARD” and select your SD card. Finally, click “WRITE.” You may be prompted for administrator privileges.

Edit Configuration Before the First Boot

After the microSD card is written, eject it and re-insert; thus, your PC OS will be able to mount it. The SD card will have two partitions named system-boot and writable.

Without third-party drivers, Windows and macOS will be able to mount only the system-boot partition, which we actually need.

Go to the system-boot partition, and edit the network-config file (you can uncomment lines starting with dashes #; however, please ensure exactly 2-space indent):

ethernets:
eth0:
dhcp4: true
# keep other lines as is...
wifis:
wlan0:
dhcp4: true
addresses: [192.168.0.99/24]
optional: true
access-points:
"Your WiFi Name":
password: "YourSecret"

Some clarifications:

  • Replace 192.168.0.99/24 with the desired IP address that must match the local network of your WiFi router. This IP address must not be assigned to other devices in your WiFi network.
  • dhcp4: true ensures that your Raspberry Pi will ask your WiFi router for network settings, including DNS servers and an IP address. Likely, your router will assign another IP address in addition to the one you specified above. That is not a problem for now — it can be corrected later by configuring the router to assign a fixed IP address to your Raspberry Pi.
  • Keep the optional: true setting. Since the WiFi adapter will not be initialized on the first boot, this setting will ensure that the boot process won't fail. We will configure our Raspberry Pi for reboot just after the first boot.
  • In the access-points setting, enter your WiFi network name and password.
  • Pay attention to indents! Each next inner level must have exactly 2 additional spaces.

Next, edit the user-data file, and append the following lines:

runcmd:
- [ sed, -i, s/REGDOMAIN=/REGDOMAIN=US/g, /etc/default/crda ]
- [ netplan, apply ]
# Reboot after cloud-init completes
power_state:
mode: reboot

The runcmd part that invokes sed is essential if you wish to connect to the 5GHz WiFi network (Raspberry Pi 4 supports the ac standard). It adds the country code to the /etc/default/crda file. The 5GHz standard has different restrictions depending on the country where it is used; thus, it won't work if the country is not specified. You can replace “US” with your country code in the code snippet above.

The runcmd part that invokes netplan apply may be omitted on Ubuntu 20.04 but is required on newer Ubuntu versions.

The power_state part instructs our Raspberry Pi to reboot after the initialization. Thus, your WiFi network can be reached on the second boot.

I recommend disabling the password login and enforcing the public key-based authentication. Otherwise, other devices on your WiFi network can intercept your password. To generate a private+public key pair on *NIX systems, launch the command:

ssh-keygen -t rsa

For Windows, download the putty-gen app (search for it here). Keep your private key in secret. Copy your public key to the clipboard; you will need to paste it into the user-data file.

In the user-data file, find the ssh_pwauth setting and change it to false. Next, add the ssh_authorized_key (with underscores!) setting as follows:

ssh_pwauth: false
ssh_authorized_keys:
- ssh-rsa AAAA...your-public-key-ending-with-the-equals-sign=

To specify the public key for the default user “ubuntu” use the ssh_authorized_keys setting name with underscores. For other users (in the users section of the users_data file) specify the ssh-authorized-keys setting (with dashes!).

Now you can umount your microSD card (do it correctly, e.g., choose “Eject”) and insert it into the corresponding slot of Raspberry Pi. Pay attention to the connectors!

If you are on Linux or macOS, store the generated private key in your home directory as ~/.ssh/id_rsa. The .ssh folder and the id_rsa file must be owned only by you and have the 700 and 600 permissions, respectively.

If you are on Windows, I suggest using Putty as an SSH client. Specify the private key file by clicking the Browse button in the Connection→SSH→Auth category (the Auth node must be selected).

The First Boot

Just plug your Raspberry Pi into a power outlet and wait for several minutes. During that time, your Ubuntu image will perform the initialization scripts and expand the root file system to occupy the whole microSD card. Raspberry Pi will also reboot once it finished executing the init script (recall the reboot setting).

Try to connect to Raspberry Pi by the IP address specified above. Use “ubuntu” as the login name.

ssh ubuntu@192.168.0.99

If you cannot connect, go to the router dashboard and look for the DHCP server leases. (On MikroTik routers, go to WebFig→IP→DHCP Server→Leases.) There you can find the IP address assigned to your Raspberry Pi by your router. It should be possible to associate the MAC (EUI-48) address of the Raspberry Pi with the desired IP.

When connecting to a new server, the SSH client will ask whether to trust the server certificate. Type “yes” to the question:

Are you sure you want to continue connecting (yes/no/[fingerprint])?

When asked for the password, enter ubuntu but you will have to change it right away. Then the connection will be terminated, and you will need to re-connect.

Unattended Upgrades

By default, Ubuntu is configured to install unattended upgrades. Thus, after the first boot you can get the error message regarding the cache log when trying to install apt packages. Just be patient and wait until Ubuntu upgrades.

However, if you wish to disable unattended upgrades, edit the /etc/apt/apt.conf.d/20auto-upgrades file as sudo, and set these values:

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "0";
APT::Periodic::AutocleanInterval "0";
APT::Periodic::Unattended-Upgrade "0";

--

--

Sergejs Kozlovičs

Sergejs is an Associate Professor of Mathematics and has Ph.d. in Computer Science. He is the main developer of webAppOS.