How to set up your Raspberry Pi for WiFi without connecting a screen or keyboard to it

This is the quickest and easiest way to set up a new Raspberry Pi image that you can log into over Wifi.  This works completely without the need for connecting a display, keyboard or ethernet cable to your Raspberry Pi.

 

On your computer, download and install the Raspberry Pi imager directly from raspyberrypi.org for your platform. 

Insert a 4GB or larger SD card into your computer that you are ok with to completely erase.

Open the Raspberry Pi Imager that you just installed and 

- Choose OS > Raspberry Pi OS (other) > Raspberry Pi OS Lite

Note that we are installing the Lite image instead of the Full, as we are not needing the graphical environment and all its dependencies.

- Choose SD Card > Select the inserted card

- Click Write > ...and confirm

Raspberry Pi Imager

Once completed, close the Raspberry Pi Imager and remove and re-insert the SD Card. On your computer, the boot partition of the newly created SD card should now be accessible. On a Mac, it will automatically mount in the directory /Volumes/boot. On Windows it will show as a drive called 'boot'.

We are ready to create a special text file and store it on the SD card.  This text file allows us to configure the Raspberry Pi to connect to your WiFi and enable ssh login.  Use your text editor of choice and store a text file with the following content on the top level directory of the SD card's boot partition and name the file wpa_supplicant.conf:

country=US # Your 2-digit country code
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
network={
ssid="Enter your WiFi name here"
psk="Enter your WiFi password here"
}

To do this on a Mac, you can for instance open the Terminal app and write

vim /Volumes/boot/wpa_supplicant.conf

Then create an empty textfile called ssh in that same directory. On a Mac, you can do this with this command

touch /Volumes/boot/ssh

Then close your text editor and unmount or eject the SD card from your computer.  On a Mac, you can do that with the command

sudo diskutil unmount /Volumes/boot

Remove the SD card and insert it into your Raspberry Pi.  When connecting power to the Raspberry Pi, it will now already connect to your WiFi if you executed the above steps correctly. You can connect to it using the following command from your computers terminal:

ssh -lpi raspberrypi.local

If this is the first Raspberry Pi that you log in, this will work. On your subsequent Raspberry Pis, your computer will recognize that the Raspberry Pi hardware has changed, but has the same name and give you an error like this:


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The ECDSA host key for raspberrypi.local has changed,
and the key for the corresponding IP address
2600:1700:3ec0:8cb8:803:4697:f8cf:fedf
is unknown. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:dapIrjOu2LTHta+F6o3OzHzQAcec+V+CICfTmGIg65I.
Please contact your system administrator.
Add correct host key in /Users/gany/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/gany/.ssh/known_hosts:11
ECDSA host key for raspberrypi.local has changed and you have requested strict checking.
Host key verification failed.

If this happens, the fix is easy, simply delete one line out of the file ~/.ssh/known_hosts.  Notice how the error in my case above shows kown_hosts:11, meaning that in my case, I have to delete line number 11. After that, if you retry to log in, you should get to the login prompt. The default login for a Raspberry Pi is:

Login: pi
Password: raspberry

Because all Raspberry Pi boards start off with the same settings, there are a few of them that we should change so you can identify the board and have a secure login.

In the logged in shell of the Raspberry Pi, write the following to change the password to something unique:

passwd

Optionally you can also create your own user account

sudo adduser YourLoginName

...and add the new user to the sudoer's list:

sudo usermod -aG sudo YourLoginName

When we logged into the Raspberry Pi, we used it's hostname of raspberrypi. If you have multiple raspberry pi's then this will fail, so it is a good idea to change the hostname to something more descriptive, as soon as you have your Raspberry Pi set up.  This way you can set up many more Raspberry Pis this way.  To change the hostname, you need to do these things:

Run

sudo raspi-config

And go to 

System Options > S4 Hostname

Enter your hostname. Make sure to only use letters, numbers and the '-' character.

When you exit the raspi-config tool, it will reboot the raspberry pi for you and you need to log in again.  Give it 30seconds, then log in with your new hostname:

ssh -lpi YourNewHostname.local
It is also a good idea to bring the Raspberry Pi image up to the latest state, despite you just downloaded it.  The image might have been created some time back. You can also install a few command line tools that you generally install, and do all that using a one liner that allows you to walk away from your computer for 10 minutes...
sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get install -y vim

Leave a comment

Please note, comments must be approved before they are published