Ubuntu Server on Raspberry Pi 4

In this tutorial, let’s learn how to set up an ubuntu server on Raspberry Pi 4

Pre-requisites

  • MicroSD card (8GB recommended)
  • PC/Laptop with a microSD card drive or a microSD card adapter to connect to the PC/Laptop
  • Raspberry Pi 4
  • Wi-Fi network or an ethernet cable to provide internet access to the Pi
  • Monitor with an HDMI cable (Optional)
  • USB Keyboard and Mouse (Optional)

Read more here:
https://www.raspberrypi.org/

For more blogs, visit: Blogs

Steps to install Raspberry Pi Imager

1. Download Raspberry Pi Imager from https://www.raspberrypi.org/software/

Rapsberry Pi

2. Follow the on-screen instructions to install the Raspberry Pi Imager and open it once it is installed. It should look like this

Raspberry Pi

3. Click on the Choose OS option, this will bring up a choice of many categories as shown below. Choose the “Other general-purpose OS” option.

Raspberry Pi

4. Next, choose “Ubuntu” to view all the available options

Raspberry Pi

5. On the next page, choose the Ubuntu Server 21.04 (RPI 3/4/400) option. Using ubuntu server is preferable to ubuntu-desktop since the server edition is more memory efficient and faster compared to the desktop version

Raspberry Pi

6. Insert your microSD card into your system now and choose the Choose Storage option

Raspberry Pi

7. Now, choose the MicroSD card in the populated menu. Be careful to not choose any other drive as this would lead to erasing all the data present on the drive

Raspberry Pi

8. Next, click on the WRITE button to begin flashing ubuntu server onto the microSD card

Raspberry Pi

9. You will be prompted stating all data will be erased. Press on Yes to continue with the formatting

Raspberry Pi

10. The flashing process will begin. It might take a while depending on the speed of the microSD card

Raspberry Pi

In case you do not want to use RaspberryPi Imager, you can use Balena Etcher as an alternative which can be downloaded here: https://www.balena.io/etcher/
All the steps are almost similar to Raspberry Pi Imager except the image of Ubuntu Server must be downloaded manually which can be found here: https://ubuntu.com/download/raspberry-pi

Congratulations!! You’ve successfully flashed ubuntu-server onto the microSD card

Steps to configure network for the ubuntu server Installation

1. Once the flashing of the image is done, remove and re-insert the microSD card into your system. You should be able to see a partition called system-boot on the microSD card. Navigate into this partition. In my case, I’m using a windows machine, and the partition as shown below

Raspberry Pi

2. The default configuration will look like below. By default, ethernet will be enabled along with DHCP. If you are using an ethernet cable for the internet and you do not need a static ip, you can keep this file as it is.

Raspberry Pi

3. In order the connect via wifi follow the below instructions

Uncomment the following by removing the # at the beginning of each line
wifis:
  wlan0:
    dhcp4: true
    optional: true
    access-points:
      <wifi network name>:
        password: "<wifi password>"

An example to connect to a wifi network named "home" and with password as "password", the configuration would be as shown below
wifis:
  wlan0:
    dhcp4: true
    optional: true
    access-points:
      "home":
        password: "password"

4. Save the changes and remove the microSD card from your system and insert it into the Pi and boot it up

The wifi connectivity might fail in the first boot. This can be fixed with a reboot. Reboot the Pi using the below command

sudo reboot now

Congratulations!! You’ve set up network connectivity for the Pi

Setup a Static IP for the Raspberry Pi (Optional)

Modify the network-config file as explained below

# To configure a Static IP for ethernet use the below snippet
# The DHCP is disabled using the "dhcp4: false" option
# Specify the required Static IP in the addresses section. In this case it is 192.168.0.128
# The gateway value is the address of the IP address. In this case it is 192.169.0.1
# The nameservers are the DNS servers that you need to use. If you have a DNS server specify the address here. If it is assigned by your ISP you can enter the IP address of your router here
ethernets:
  eth0:
    dhcp4: false
    addresses:
      - 192.168.0.128/24
    gateway4: 192.168.0.1
    nameservers:
      addresses: [192.168.0.1]
    optional: true

# To configure a Static IP for Wi-Fi use the below snippet
# The DHCP is disabled using the "dhcp4: false" option
# Specify the required Static IP in the addresses section. In this case it is 192.168.0.128
# The gateway value is the address of the IP address. In this case it is 192.169.0.1
# The nameservers are the DNS servers that you need to use. If you have a DNS server specify the address here. If it is assigned by your ISP you can enter the IP address of your router here
# The wifi network "home" is used here with password as "password"
wifis:
  wlan0:
    dhcp4: false
    addresses:
      - 192.168.0.128/24
    gateway4: 192.168.0.1
    nameservers:
      addresses: [192.168.0.1]
    optional: true
    access-points:
      "home":
        password: "password"

Congratulations!! You’ve set up a Static IP for the Pi

Booting the Pi

1. Now that we have everything set up, insert the microSD card into your Pi and turn it on. Wait for a few mins for it to complete the booting process. In case, you have a monitor available you can connect the monitor to the Pi and see the process. Once it is done use the credentials below to login to the server

Username: ubuntu
Password: ubuntu

2. Assuming that we do not have a monitor let us login to the Pi using ssh. SSH into the Pi using the below command. If you have configured a Static IP for your Pi you can use the same IP to login using ssh. If you have not configured the Static IP, then login to your router configuration to identify the IP of the Pi

ssh ubuntu@<ip-address of the pi>

# If IP of the Pi is 192.168.0.128, then
ssh [email protected]

You should be prompted to change the password immediately as shown below

Raspberry Pi

3. Once the password is changed, you will be logged out. Login again using the same ssh command and you should now be able to access the ubuntu server

Raspberry Pi

4. As an initial step, it is always good to update the ubuntu installation as this would keep the firmware and the security patches up to date. Run the update using the below command

sudo apt-get update && sudo apt-get upgrade
Raspberry Pi

Congratulations!! You’ve successfully installed Ubuntu Server on your Raspberry Pi

Setting up Public Key Authentication for SSH (Optional)

Setting up Public Key Authentication on your Pi will speed up the login process as you would not have to enter the password every time. The public key of your machine can be added to the authorized keys of the Pi which would enable the public key authentication and you will be able to login to the Pi without having to enter the password. To set this up, follow the below steps

1. Copy the public key of the machine from which you intend to SSH into the Pi as shown below

cat .ssh/id_rsa.pub

The key has been hidden for Privacy in the below image

Raspberry Pi

2. Log in to the Pi and paste this public key into the authorized_keys file in the .ssh directory as shown below

# To Login to the Pi
ssh ubuntu@<IP-address>

# Use an editor of your choice, I will be using nano in this case
nano ~/.ssh/authorized_keys
# Paste the public key into this file
# Save in nano using Ctrl + O and exit using Ctrl + X

Key is hidden for Privacy

Raspberry Pi

3. Once this is done, log out of the pi and now try to login using SSH with the same command. This time you should be able to login without having to enter the password

Raspberry Pi

Congratulations!! You’ve successfully set up Public Key Authentication on your Pi

3900cookie-checkUbuntu Server on Raspberry Pi 4

One Comment

  1. Pingback:OpenMediaVault NAS with Samba on Raspberry Pi » EasyCode

Leave a Comment

Your email address will not be published. Required fields are marked *