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
  • You listed a tutorial, but have NOT followed it! NOTE the arp instructions are WRONG for a Pi4, but there are better methods of determining IP. How do you know "my RPi 4 is still unable to connect to WiFi"? – Milliways Apr 25 '20 at 06:53
  • @Milliways I've updated my post to better reflect the steps I did. – Drake P Apr 25 '20 at 09:17
  • I am having this exact same issue. sudo iwlist wlan0 scan is even picking up the network I want to connect to, but the system dialog for Wifi says that I don't have a wifi adapter installed. – Brian Tracy Apr 25 '20 at 22:49

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
    I am going to try this tomorrow, because I am in the exact same situation. Could you explain what the network: {config: disabled} line does? In addition, when I run sudo netplan apply, I get Warning: The unit file, source configuration file or drop-ins of netplan-wpa-wlan0.service changed on disk. Run 'systemctl daemon-reload' to reload units. – Brian Tracy Apr 27 '20 at 06:19
  • the config: disabled part turns off the "cloud config" networking stuff that's configurable in the 50-something file in cloud.cfg.d. I've had a couple of occasions where I encountered warnings when running apply too, but after reboot, it always works :) – Andbdrew Apr 30 '20 at 21:59
  • Thank you for this great answer. I owe you a beer :) – ndreisg May 27 '20 at 17:25
  • Is there any way to do this without connecting a keyboard and monitor to the Pi? I'm trying to manually tamper with them as little as possible so that they are easily re-imagable... thanks! – Mark Stickley May 29 '20 at 20:38
  • As of Jun3 15,2020, slight update. I also got the Run 'systemctl daemon-reload'. Tried it. It exited with non-zero error, I think it was 5. I tried the reboot. No good. No wifi. Repeated the process. Same. No good. Then I did a full shutdown and poweroff instead of reboot. Powered back up and after giving the login prompt, it also then gave some status and versions info for several lines, where I was supposed type in the login user. I went ahead with user and password, as if those messages had not stomped on the login area and had not repositioned the cursor. Successfully loggen in. – always_learning Jun 15 '20 at 19:18
  • The system then came up, and the wifi was connected. So, small hiccup, but now working. Following up with sudo apt update and sudo apt upgrade did not solve the quirkiness during login, but again, ignoring the obtrusive status messages, and proceeding with entering the user name and then password does get me into the system. With wifi connected. Thank you for these posts. Also got a message that apt was unstable... interesting. – always_learning Jun 15 '20 at 19:23
  • Additionally, although this solution works in a non-GUI system, as soon as I installed the official repository available kubuntu or budgie GUI, the GUIs were unstable, and wifi connectivity was lost, and the utilities to re-establish it either were not available or problematic, that is, with kubuntu and budgie. I will try building and configuring kubuntu and budgie systems, from the base 20.04 server image, using connectivity on the wire (ethernet) first, then switch to a wifi config setup using the GUI tools, after the installs and updates are complete regarding the GUI. On Pi4B 4GB. – always_learning Jun 16 '20 at 13:44
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.
  • 171
  • 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

  • I believe this would only be a temporary solution, since 50-cloud-init.yaml says Changes to it will not persist across an instance reboot. – Drake P May 11 '20 at 14:49
  • @DrakeP I wonder why https://raspberrypi.stackexchange.com/questions/98598/how-to-setup-the-raspberry-pi-3-onboard-wifi-for-ubuntu-server-18-04-with-netpla is suggesting to update that very same file. This is confusing. – piepi Jul 28 '20 at 11:19