Tugulab Blog.

How to setup your Raspberry Pi 2/3 with Ubuntu 16.04, without cables (headlessly)

I am working on a robotic project which has at its core a Raspberry Pi with Ubuntu minimal (no GUI) and Swift 3.0.
author avatar

clacla

I am working on a robotic project which has at its core a Raspberry Pi with Ubuntu minimal (no GUI) and Swift 3.0.

The problem with a vanilla Ubuntu is that it requires you to have a monitor, keyboard and mouse. All of this in inconvenient and I would love to just do everything by ssh-ing into the machine. Sadly, it’s not possible out of the box.

After some experimenting I’ve been able to achieve that. What follows is the tale of this mighty nerd.

Step 1: Preparation

Get your Raspberry Pi 2 or 3, an SD memory card with 8Gb or more memory (class 10 or more), a micro USB cable (for power), an ethernet cable and the Wifi USB dongle.

Step 2: Flashing Ubuntu 16.04 server minimal on the SD card

Download Ubuntu server classic from here or here. This is a flavour of Ubuntu server, which have been stripped out of everything which is not strictly necessary, so it’s very light.

Now you have to flash the image on the SD card. On the web you will find a lot of multi step command line guides. But that’s nuts, since there’s a great apps, called Etcher.io, that make the process incredibly simple, fast and safe.

Download Etcher app (for all platform), then select the Ubuntu image, the SD card (which you have to insert in your computer) and flash it. Few minutes and it will be done and verified.

Put the SD card into the RPi, you’re ready to rock!

Step 3: Ssh-ing into your RPi

After you’ve inserted the freshly backed SD card into your RPi connect the ethernet cable between the RPi and your router. Turn the RPi on by connecting the micro USB cable to the electricity or to your computer.

Wait few minutes, since the first boot takes longer than usual. Then open your router’s dashboard and look at the ethernet connected devices. You will see a device hostname ubuntu-minimal. Take a note of the IP of that device.

Open your terminal and type:

ssh ubuntu@THE_IP_YOU_JUST_FOUND
(password ubuntu)

You should be in!

You are now connected to the RPi via the network. The only problem is that you still need to keep the RPi connected to the network throughout the ethernet cable. But let’s address that.

Step 4: Going wireless

First we need to update the operating system:

sudo apt-get update
sudo apt-get upgrade

Install the wifi support:

sudo apt-get install wireless-tools wpasupplicant

Now reboot. It’s important to do it now, since my wireless interface changed name after this step. From a nice “wlan0” to a weird “wlx000f6005a699”.

sudo reboot

Attach the USB dongle to your RPi. Then, ssh again into it. Then, list the wireless network interfaces with:

iwconfig

Take note of the wireless interface name (e.g. wlan0 or wlx000f6005a699).

Open the network interfaces configuration:

sudo nano /etc/network/interfaces

At the bottom of the file add (replacing the wlan0 with the name of your interface):

allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

Now, open the wireless configuration file:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

and add at the end of the file the informations about your wifi network:

network={
ssid="your-network-ssid-name"
psk="your-network-password"
}

Save and exit the editor. Remove the ethernet cable and then reboot:

sudo reboot

Now in about 30 seconds your RPi should be up an running. If you try again to ssh into the RPi (still looking for its IP in the router dashboard), you should just be able to connect to it via wifi!!

Step 5: Ssh-ing into the RPi with a dynamic IP

As you’ve already noticed, checking the the RPi’s IP every time you want to connect to it, it’s a bit of a pain in the ass.

So, let’s install avahi which will make you able to connect to the RPi via its hostname.

sudo apt-get install avahi-daemon

Now, after a reboot, you will be able to ssh into the machine:

ssh ubuntu@ubuntu-minimal.local
(password ubuntu)

Step 6: Securing the ssh authentication

I suggest you to secure your login with a rsa key authentication and enabling it only the port 22.

From your Mac laptop:

ssh-copy-id ubuntu@ubuntu-minimal.local

Now you should be able to ssh into RPi without password:

ssh ubuntu@ubuntu-minimal.local

The-wise-guy-last-step: Backup your RPi

It’s very easy to burn an SD card by writing to it too many times. Or irreversibly fuckup your configuration by doing what sudoer should not do. Cloning your SD card, as an image that you can flash on a new card when you need it, is the perfect backup strategy. Here you find how.

You are now ready to install Swift 3.0 developer preview on your sexy Raspberry Pi Ubuntu box.