27

I'm trying to run my Raspberry Pi 4 on Ubuntu Server 20.04 (64bit) following this tutorial. When I reached 4. Boot Ubuntu Server, my RPi had no IP address (I switched to using a monitor when doing a headless setup failed, and calling hostname -I returns a blank line), which I believe is the result of having no internet connection, further demonstrated by sudo apt update failing with every line saying temporary failure resolving 'ports.ubuntu.com'. I also get a blank line when calling arp -a | findstr b8-27-eb from the Windows command prompt.

The tutorial had me modify the network-config file before inserting the card into the RPi which I did, and through some exploring, this had the following effect on /etc/netplan/50-cloud-init.yaml (with WIFI-NAME and WIFI-PASSWORD being what they should be):

# this file generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init’s
# network configuration capabilities, write a file 
# /etc/cloud/cloud.cfg.d/99-disable-network-config-cfg with the following:
# network:{config: disabled}
network:
    ethernets:
        etho:
            dhcp4: true
            optional: true
    version: 2
    wifis:
        wlan0:
            access-points:
                WIFI-NAME:
                    password: WIFI-PASSWORD
            dhcp4: true
            optional: true

In spite of this, my RPi 4 is still unable to connect to WiFi. If it's relevant, the first time I booted the RPi I didn't actually get the IP address since I disliked the angle of the cord so I unplugged it after a minute or two, plugged it back in, and on the second boot was when I attempted to get the IP.

What am I doing wrong? Or is the issue perhaps with Ubuntu 20.04 and I should use Ubuntu 18.04.4 until 20.04 stabilizes?

Drake P
  • 395
  • 1
  • 4
  • 7

5 Answers5

27

Using Andbdrew's tips above I was able to make this work headless (and with a head) consistently without disabling Cloud-Init. Thanks to @enconn, for brainstorming and a link to https://cloudinit.readthedocs.io/en/latest/topics/examples.html#reboot-poweroff-when-finished.

  1. Image the SD Card using Raspberry Pi Imager v 1.3 selecting the 'UNBUNTU 20.04 LTS (RASBERRY PI 3/4)(64-bit) image.
  2. When imaging is completed, remove the SD Card and then reinsert it so that it is mounted.
  3. Modify 'network-config' found on the SD Card so that it contains only the items in the example below. Comment out with #, or remove, all other settings including the LAN ones. Enter any missing items. Be certain to maintain only the indentations shown. Use two spaces for each indentation. Remove all tab characters. Be especially careful if using Notepad++ as it will enter tabs wherever enter is used to start a new line. Replace the SSID with your wireless SSID and the PassPhrase with your wireless passphrase. When done, those two values should be wrapped in quotes. Save the modified file to the SD Card.
  4. Edit 'user-data' appending the additional lines shown below. Again, use spaces, not tabs and mind the indentation.
  5. Eject the SD Card, remove it from the computer used to image it, and then insert into the powered off Raspberry Pi. Then, power up the RPi.
  6. Allow Ubuntu to boot; DO NOT try to log into Ubuntu as soon as possible. Wait until Cloud-Init runs (although it appears to be doing nothing - in about two minutes it will show SSH info when done). If you don't wait you may not be able to logon with the default user and passwd. At the end of the cloud-init,Ubuntu will be rebooted. What a couple of minutes for the server to boot.
  7. Logon either at the console or remotely using ssh ubuntu@x.x.x.x (get the address from your router, etc.)

That's it: Headless, Wi-Fi'd Ubuntu 20.04 on Raspbery Pi 4b

network-config example:

# This file contains a netplan-compatible configuration which cloud-init
# will apply on first-boot. Please refer to the cloud-init documentation and
# the netplan reference for full details:
#
# https://cloudinit.readthedocs.io/
# https://netplan.io/reference
#

version: 2 renderer: networkd wifis: wlan0: dhcp4: true dhcp6: true optional: true access-points: "SSID": password: "PassPhrase"

Append this to the end of 'user-data':

##Reboot after cloud-init completes
power_state:
  mode: reboot
David Taylor
  • 371
  • 3
  • 4
22

I've had similar adventures over the last few days :) My system is now:

  • burn image of ubuntu server 20.04 onto sd card
  • boot rpi
  • write network: {config: disabled} to /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
  • write the following to /etc/netplan/01-netcfg.yaml:
network:
  version: 2
  renderer: networkd
  wifis:
    wlan0:
      dhcp4: true
      dhcp6: true
      optional: true
      access-points: 
        "******":
          password: "**********"
  • then do:
sudo netplan generate
sudo netplan apply
reboot

wifi now works for me, and then you can do stuff like apt update and install a desktop and stuff

For more info on netplan, check out https://netplan.io/examples

Andbdrew
  • 336
  • 2
  • 5
2

Issue is because of this bug https://bugs.launchpad.net/ubuntu/+source/netplan.io/+bug/1874377

Currently Ubuntu 20.04.1 needs reboot even though the issue is supposed to be fixed. Cable internet connects immediately. Ubuntu 20.10 connects to wifi even without reboot for now.

Margus Pala
  • 121
  • 2
2

Description

This script named first_run.sh asks you for your wifi ssid and wifi password and automatically connects your Raspberry Pi 4 to your wifi:

Code

# Verify internet access is available
while [ $(ping -q -w1 -c1 google.com &>/dev/null && echo online || echo offline) == offline ]
do
sleep 1
# get wifi module
module=$(ls /sys/class/net)
module_list=($module)
wlan_module=${module_list[-1]}
echo $wlan_module

# get wifi configuration file
config_filename=$(ls /etc/netplan)
echo $config_filename
config_filepath="/etc/netplan/$config_filename"

# check if the wifi is already added.
if ! grep -q "$wlan_module" "$config_filepath"; then
    echo "have to add wifi"

    # ask  wifi pwd ssid
    read -p "What is the wifi ssid you want to connect to(Case sensitive)?" ssid

    # ask wifi pwd
    read -p "What is the wifi pwd?" pwd

    # append content
    echo "    wifis:" | sudo tee -a $config_filepath 
    echo "        wlan0:" | sudo tee -a $config_filepath
    echo "            dhcp4: true" | sudo tee -a $config_filepath
    echo "            optional: true" | sudo tee -a $config_filepath
    echo "            access-points:" | sudo tee -a $config_filepath
    echo "                \"$ssid\":" | sudo tee -a $config_filepath
    echo "                    password: \"$pwd\"" | sudo tee -a $config_filepath

    # generate a config file
    sudo netplan generate

    # apply the config file
    sudo netplan apply

fi

done

The script is verified on a new Ubuntu Server 20.04.2 LTS 32-bit server OS for armhf architectures on the RaspberryPi with 4gb ram that was written to an sd card using the Raspberry Pi Imager.

Usage

You can copy first_run.sh to a usb stick, then plug it into your Raspberry Pi 4 and run it with:

sudo mount /dev/sda1 /media/
sudo chmod +x /media/first_run.sh
/media/.first_run.sh

It will loop until the internet connection is established.

a.t.
  • 183
  • 2
  • 10
0

I used your method but on a different file. sudoedit /etc/netplan/50-cloud-init.yaml

you don't have to do the sudo netplan generate or reboot. It will work straight away after the sudo netplan apply