When you’re building robotics applications on Raspberry Pi, you don’t always
have a keyboard and monitor available. During our workshops last year we worked
almost entirely with SSH in order to control the Pis. But in order to do that,
you need a working WiFi connection.

In the following tutorial, you’ll learn how to configure the Raspberry Pi to
connect to your WiFi network, whether or not you have a monitor and keyboard

Key concepts

This tutorial is written for a Raspberry Pi Zero W
running Raspbian, but the same steps should work for any Raspberry Pi, as long
as it has a working WiFi antenna – if your Raspberry Pi is another model, for
example, you might need to use a USB WiFi antenna.

You’ll also need to be familiar with these things:

IP address – This is a numerical address that a host, such as the
Raspberry Pi, can be reached under on a network. An address is assigned to a
host when it lots onto the network.

Two version of IP addresses are currently in use: IPv4 and IPv6. IPv6 is the
newer version and has longer addresses in order to be make it possible to use
more addresses in a network. An example IPv6 address might look like
2a00:1450:4016:800::200e. An example IPv4 address might look like

Hostname – This is a human readable name which is assigned an IP address
in a network. The intent behind this is to 1) make it easier for humans to
remember how to access a given host and 2) assign a logical name to a service,
so that it can be reachable even if it’s found at a different IP address than
expected. An example of this is google.com. No matter what machine
Google is currently using to host its search service, you should be able to
find it under google.com. If you’re working with one of the images
supplied by Full Stack Embedded at the workshops in 2017, your hostname will be
fse2017-x, where x is a letter. If you’re
working with a fresh install of Raspbian, your Pi’s hostname is probably

Portable WiFi hotspot – In case you don’t have access to a building’s
WiFi network, you can normally use your phone to create a WiFi hotspot. This
can be used to give your Pi internet access, or – in our case more importantly
– connect to your Pi from your computer. In order to make this possible, both
the Pi and the computer need to be connected to the same WiFi network, i.e.
your phone.

Not every phone allows the same options in order activate and configure a WiFi
hotspot, and some phones give you more information about the connected hosts
than others do. In this tutorial, the examples will be shown using a Neffos X1
Lite running Android 8. See the end of the tutorial for instructions on how to
set up such a hotspot.

Configuring the WiFi connection with a monitor and keyboard

If you have the necessary equipment, this is probably the easiest way to do it.
Boot your Pi and log onto the command line.


It’s useful to know your Pi’s hostname. It’s shown on the command line like

pi@raspberrypi ~ $

In this line, pi is the username, @ separates the
username and hostname, raspberrypi is the hostname, ~
shows you the folder you’re currently working in (~ is an alias
for the current user’s home directory, so in this case /home/pi),
and $ is a sign showing you that the line is a command prompt, so
your input comes afterwards. If you’re happy with that hostname, write it down.

You can also find the hostname located in a file, /etc/hostname.

Setting the hostname

The easiest way to set the hostname is to edit the file
/etc/hostname using your favorite text editor. When your Pi boots
the next time, it will use the content of that file as its hostname.

You can also do it with the hostname tool, e.g. as follows:

sudo hostname my-host-name

This sets the hostname of the current host to my-host-name.

Another alternative is to use raspi-config, which lets you set the
hostname interactively. Just follow the instructions in the prompts.

Enabling SSH

If you want to connect to your Pi using SSH, you’ll need to enable SSH. You can
enable it like this:

sudo systemctl enable ssh

This enables the SSH service to work.

sudo systemctl start ssh

Afterwards, this starts the SSH service.

Another way to do this is interactively, using the raspi-config

After you’ve completed those steps, you can connect to your Pi from another
network as follows, filling in the variable names to match your Pi’s


And remember, the hostname is actually an alias for the IP address, so you can
also connect like this:


Of course, this will only work if you fill in the correct values to substitute
the variables.

Configuring WiFi access credentials

You can configure your Pi for WiFi access by editing the file
/etc/wpa_supplicant/wpa_supplicant.conf. This file must be edited
as a super user, so you’ll need to access it using sudo $EDITOR
(of course substituting $EDITOR for the editor of your choice,
like nano or vi). In order to let your Pi connect
automatically to your network, add the following text to the file:


As usual, substitute out the values for $NETWORK and
$PASSWORD to match your network.

If you’re not sure what your network is called, you can find the available
networks by running:

sudo iwlist wlan0 scan

This will output a lot of information about all the available networks. You can
limit the output to only the network names like this:

sudo iwlist wlan0 scan | grep ESSID

You’ll still need to know the password, of course!

Securing your connection credentials

If you’re worried about storing your password in plain text on the Pi’s SD
card, you’re absolutely right. Anybody with access to the SD card could find
out your password. A better way is to store an encrypted PSK. You can generate
one like using the wpa_passphrase tool like this:

wpa_passphrase $NETWORK $PASSWORD

This will output an example network configuration to the screen. You can use
this in /etc/wpa_supplicant/wpa_supplicant by cutting out the line that
starts with psk= and pasting that into the file. That way someone with
access to your SD card can gain access to your network, but not your password.

Connecting to multiple networks

You can configure your Pi to connect to multiple networks by including more
than one network object in your file. If you do this, each network needs an
id_str attribute which gives the network a unique ID within the
file. Additionally, you can provide a priority attribute. If more
than one configured network is in range, the Pi will choose the network with
the higher priority. Here’s an example:



In this example, the Pi would always connect to fse2017 if it’s in
range, but if not, it would connect to homeNetwork.

Connecting to an unsecured network

If your network doesn’t use a password, you can still connect to it by
replacing the psk entry with key_mgmt. In this case,
your network configuration might look like this:


Testing the configuration

After setting up your Pi’s configuration, run

wpa_cli -i wlan0 reconfigure

to connect to the configured WiFi network. You can test if this was successful
by running

hostname -i

This should display your current IP address if you have a connection. If not,
it will display the error

hostname: Name or service not known

Configuring the WiFi connection with another computer

If you don’t have access to a monitor and keyboard,
you can use another Linux computer to configure your Pi’s WiFi. Take the SD
card out of the Pi, insert it into the Linux computer’s SD reader, and get

Enabling SSH

If SSH isn’t already enabled, you can do this from the other computer by
mounting the Pi’s boot partition. This will be the smaller of the
two partitions you can see on the card.

Enter the partition. An easy way to do this is by navigating to the SD card
using a file manager, and then cutting out its address from the address bar and
pasting that into your terminal. Then, place a file named ssh into
the partition’s root directory, e.g. like this:

sudo touch ssh

The ssh file doesn’t need any contents. It is only used to enable SSH
when the Pi boots. If, when the Pi is booting, it finds a file called
/ssh, it sets its configuration to always enable and start the SSH
service on boot, then removes the file.

Configuring wpa_supplicant

Mount the main partition on your SD card and navigate into it. This will be the
larger partition in your SD card. The root folder of the partition is seen by
the Pi as the root folder / of the whole file system when it’s
running. From there, you can edit the the hostname and enter the configuration
details as required – see the section above on what files to edit. Just
remember, don’t edit /etc/hostname or
/etc/wpa_supplicant/wpa_supplicant.conf, as in the example – that
would mean that you’d edit those files on the computer you’re using to access
the SD card. Instead, if $PATH_TO_PI is the path to your mounted
SD card, you should edit $PATH_TO_PI/etc/hostname or

Finishing up

Unmount whatever partitions of the SD card you mounted, then put the card in
the Raspberry Pi and start. It should automatically log onto your network if
you’ve set things up right.

Connecting to the Pi

Now that your Pi is connected to your network, connect your computer or
whatever device you want to use to the same network in order to access it. You
should be able to do it like this, of course substituting in the correct values
for your Pi:


When asked, enter the password for $USERNAME and voila! You’re logged
onto the Pi.

If your computer isn’t able to resolve the hostname, try using the IP address
to connect directly, e.g. like this:


Below there are a few tips if you’re having trouble discovering your hostname
or IP address.

Using a monitor

If you have a monitor and keyboard available, connect them to the Pi and log
on. You can discover the hostname and IP address as follows:

hostname  # Prints the Pi's hostname
hostname -i  # Prints the Pi's IP address

If these don’t work, your Pi isn’t properly configured to connect to the WiFi,
so see the steps above.

Without using a monitor

There are several methods of finding what hosts are connected to a network, and
we won’t cover them all here. If you’re using your phone as a WiFi hotspot, you
might be able to see the connected users in the “Set up WLAN hotspot” section
of your settings. If you tap any of the hosts shown there you should find out
more information about it.

If that isn’t available, try using ipscan or an IP Scanner you download from your phone’s app store.

Once you’ve discovered the IP address of your Pi, you can substitute that for
the target hostname and log on. Use those to log on and have fun!

Tangent: Setting up a hotspot using a phone

If you’re working far away from a WiFi network, there is a good chance your
phone can provide you with the connectivity you need in order to connect your
Pi to the Internet or connect to your Pi with another device. The pictures
below show the necessary steps.

Tap “More connection settings”.
Tap “Tethering and portable hotspot”.
Enable “Portable WLAN hotspot” and tap “Set up WLAN hotspot”.
Note or edit your hotspot’s name and password.

In some phones, connected devices are shown on the last screen as well. Tap them for more info, e.g. to find out their IP address in the local network.