The following instructions will work anytime, you don't necessarily have to follow them for the first boot - this is just a very convenient way to get your Raspberry Pi onto a network without using any plug-in peripherals like a keyboard, mouse or monitor.
Today we’re going to set up our Raspberry Pi Zero W to connect to a WiFi network on its very first boot. This means we can do away with bulky hardware like a monitor, keyboard and mouse and we’ll just be remotely accessing the Raspberry Pi over WiFi - lets get started!
Over on the Raspberry Pi Foundations website I’ve already gone to downloads and downloaded Raspbian Jesse Lite. Because we’re accessing remotely I’ve chosen Lite because it doesn’t have the Graphical user interface that you would normally get if you had a monitor plugged in - if you still wish to keep that you can use Raspbian Jesse with Pixel, that’s fine but I’ve just gone for Lite today.
So I’ve already downloaded that into my downloads folder as you can see here and the first thing we have to do is flash it onto an SD card. I’ve got my 16 GB Micro SD card on the bench here so I’m going to put that into the SD adaptor and we’ll flash that using a utility called win32 disc imager. So here’s our imaging utility - so my drive is drive G, that’s correct. I’m going to go to downloads and select the Raspbian Jesse Lite image and then write it to the SD Card. We just get a warning but we’re just going to say yes to that and we’ll just wait for that to finish flashing. Ok, so our image has been flashed successfully and we’ll close those dialogues. Now in order to connect our Raspberry Pi to a WiFi network we’re going to modify the image that is on the SD card slightly. I’m going to open our SD card, here’s our G Drive, and we can see that it has been labelled boot now. We’re accessing a special part of the SD card now that is only visible on a Windows operating system. So the Raspberry Pi Foundation have included some features that will allow us to create some files in this boot partition to allow us to affect our Raspberry Pi’s behaviour when it boots up. The first thing we can do is give it the WiFi credentials of the network we want to join. To do that we need to right click and select new text document and I’m going to call this wpa_supplicant. Now we can open the file and this is where we enter our WiFi credentials. We do this by specifying network and then and equal and an open brace then on a new line we enter ssid and this is the name of the network we want to join. So if you had mywifinetwork that is where you would put it. On the next line you enter psk which is the password for this network - so that could be something like myStrongPassword123 - not particularly strong but good for an example. We finally enter the key management that we’re using. So, I’m using wpa2, so I’m going to enter key mgnt equals wpa underscore, sorry, hyphen psk. There are no quotes around that one. Then, on the next line we just close that brace. We can save that file now and close it and the last thing we need to do for this wpa supplicant file is change its extension. So we backspace txt and we insert co and f for configuration and hit enter. We’ll get prompted that we’re changing the file name and that’s ok. Now if you didn’t see that file extension and in windows 10 you can go to view and I have this checkbox here ‘file name extension’ is checked so you might have to check that just to make file name extension visible.
So that is going to cause our Raspberry Pi to connect to the network we want it to but now we also need to enable remote access with ssh and that is really really simple to do. Once again we create a new file. I’m just going to choose a text document and we label this ssh and now we just delete the file extension. So we’re creating an empty file with no extension called ssh. Select yes and thats all we have to do to the SD card. Now our Raspberry Pi is going to boot, connect to the network that we specified and open itself to remote connections.
We’re ready to boot our Pi up now. Ok, I’ve ejected my SD card so now I just need to take the micro SD out of the adaptor, plug it into the Raspberry Pi. We need to power it, so the first power sequence takes about a minute. Ok, here we are about a minute into the future and now we’re going to connect to our Raspberry Pi remotely using a programme called PuTTY which is an ssh program. I’ve included a link in the description for this content on how to download that. Just going to open up PuTTY and this is what it looks like - so when you first power ups Raspberry Pi running Raspbian it has a default host name - that is the name of the machine and that is Raspberry Pi. So, now that our Raspberry Pi is connected to our WiFi network and has that default host name we know exactly how to connect to it. Just in the host name box I’m going to enter RaspberryPi and hit open. Ok, so we get our back screen which is our terminal and a security alert. This is just an alert saying that we haven’t connected to this machine before but I’m confident that it is the machine that I do want to connect to so I’ll just say yes and there is our log in screen. Can I change the font on that? - just a break there while I change the font size. So now we’re going to log in to our Raspberry Pi remotely and the default user name is Pi and the default password is Raspberry. There’s our prompts and now we have an ssh connection to our Raspberry Pi and we can access it exactly as if we were sitting right in front of it.
Before we proceed it’s probably a good idea to change the default host name - that is Raspberry Pi. We want to change that because if we ever deploy another Raspberry Pi that we want to set up as headless it’ll be useful to have Raspberry Pi as available on the network so we’re going to change it to something that is going to be unique. We can do that with sudo raspi config and the second option there is the host name so we’re going to change that. This is just telling us what characters we can use. I’m going to call this maybe devpizero and hit ok. So now we’ve changed the host name and thats how we’re going to have to connect to this Raspberry Pi in the future. While we’re at it its a good idea to change the default user password because everybody knows that the default user is Pi and Raspberry. We can leave the default user as Pi but lets just change the password. Lets enter a new password, I’m just going to choose something simple and re-enter it - but at least now we don’t have Raspberry Pi as the host name thats on the network and we’ve at least changed the default password - so now there’s at least some security. For those changes to take effect we’re just going to hit finish and that’ll prompt us to reboot so I’ll say yes. While that is rebooting this PuTTY session is about to close so I’ll just close that now. Now that we’ve renamed our Raspberry Pi - the machine name - I’m going to save that machine name as a session and this will be an easy way for us to keep track of what machines we’re connecting to. The new host name that we’ve chosen is devpizero and I can copy that and paste that into the saved sessions box and press save. Now it’ll be really easy for me to reconnect back with my Raspberry Pi by double clicking that option.
So, that should have rebooted now, lets give that a go. I’ll just select that and press open and now we get another security warning because the previous session we were connecting to a machine called Raspberry Pi and now we’re connecting to essentially the same address but it has a different name and this is just PuTTY saying - hey, that might be a problem. But I know that we’ve done that so I can just say yes and I’ll log in as Pi with my new password and you can see we’ve logged in and of course the host name has changed on the prompt.
That wraps things up for this tutorial, now we can deploy a truly headless Raspberry Pi Zero without it ever having needed to be connected to a monitor, keyboard or mouse.
I’ll see you next time.