This is a starting point that I do on every one of my raspberry pi projects. You can branch off from this point to any number of projects. I used to like a screen and keyboard/mouse interface, but this is how to set up a headless rpi that you only control over the network. it is much easier than it sounds.
Hardware:
- Raaspberrypi startup kit with power cable and SD card.
- Computer (I have windows 10 but there are guides out there for mac and linux as well)
- Wireless network to connect to
- Maybe a camera to format the SD card?
Software Tools required :
- SD card formatter (I use a DLSR camera because sometimes even this tool won’t format the cards right)
- Balena etcher to burn the raspberryPiOS to the SD card
Firstly, I downloaded and installed the latest raspberrypiOS Lite to an SD card. Once it was installed, I reinserted the SD card into my laptop and created a wpa_supplicant.conf file in the partition I was able to open in windows (one will be openable, the other won’t be). This file sets up your Wifi settings so you can control the pi remotely instead of trying to find the right HDMI or component cable an display and connecting a keyboard and mouse to it. I can simply ssh into the Rpi and run the scripts I need. This may sound intimidating, but it isn’t too hard at all.
Open a text editor (NOT word or notepad, download something like Sublime3 or notepad++) and create a new file names wpa_supplicant.conf. Paste the following and make sure to enter your WIFI’s credentials and keep the quotation marks.
country=US update_config=1 ctrl_interface=/var/run/wpa_supplicant network={ scan_ssid=1 ssid="Put your networks SSID here" psk="Put your networks password here" }
That will get the pi on the network, next we need to be able to actually control the Rpi from another computer on the network. To do so, just create a blank file with no file extension named “ssh” in the same SD card partition as wpa_supplicant.conf. That’s it. This empty file just tells the Rpi to turn on ssh, which allows you to connect and control it remotely.
Now you can insert SD card into Rpi and plug in power to boot up.
With my older Rpi3 I give it like 5 minutes depending on the OS. Then you can check to see if your Rpi is on the network. Open the command window in windows (windows key, then type “cmd” then enter) and type
ping raspberrypi.local
You should see a response. If it times out, then give it a little more time to install the OS and try again. If you can’t ping it (communicate with it ) after 15 minutes after you booted t up (or 30min or longer sometimes for a pi Zero w) need to start from scratch because something went wrong in your wpa_supplicant file. Triple check that the file is not saved as “wpa-supplicant.conf” or “wpa_supplicant.conf.txt” For that last one you may need to “show file extensions” in window’s explorer.
I always used putty to ssh into linux machines from windows, but Windows 10 apparently has ssh built right in, so you can just click the windows icon and type “powershell” to open a command window, then enter
ssh pi@raspberrypi.local
We’ll use powershell instead of the Cmd window to allow us to copy and paste stuff into the window easily.
The first time you do this, it’ll give you a warning that it “can’t verify the [raspberrypi], do you want to continue” just type “yes” and hit enter. Then you will be asked for the password. Note that as you type, you will see no letters appear in the terminal. This is normal for password entry on linux machines. The default username is “pi” and default password is “raspberry”. When you type it and hit enter you should see a green line that says “pi@raspberry” which means you are logged into the pi.
The first order of business is to change the default password. type the following:
passwd
Enter your new password and you’re set. Now you can go off doing whatever random things you want to use the Rpi for.
To copy and paste into the SSH window you may need copy as usual form a webpage then right-click into the powershell window (maybe do this twice if you hadn’t already selected the powershell window) and it’ll automatically paste it for you.
Static IP: Next I like to set up a static IP address for my Pi so I always know where it is. This also helps things like streaming a webcam for Octoprint since the address won’t change. Android phones won’t use the mDNS entry of “raspbeypi.local” so if you want to use your phone to control or view things on the pi you need to set a static ip. Do so by issuing the following command:
sudo nano /etc/dhcpcd.conf #this opens nano command line text editor to the IP address file...
My pi is on wifi so I’ll adjust the wlan0, but you can replace this with “eth0” if your pi is using ethernet.
interface wlan0 static ip_address=192.168.0.100/24 static routers=192.168.0.254 static domain_name_servers=192.168.0.254 8.8.8.8
This way I know all my raspberrypi stuff is found at 192.168.0.100. I can just type that in for ssh, or into a browser if I’m running a server on it (like octoprint, hassio, mjpg streaming video, etc.
Remote Desktop: To make it easier to connect to the pi in the future and to remote into it and control it’s graphical desktop from any other computer (such as your desktop or laptop), you can set up your pi to allow VNC. First, in your ssh terminal, we need to enable VNC.
sudo raspi-config
Then use arrow keys to select “Interface Options”. Select “VNC” and you’ll be prompted to enable VNC the server. Then you can exit Raspi-config. On your desktop/laptop/other computer you need to install a VNC viewer which will allow you to connect. Visit https://www.realvnc.com/en/connect/download/viewer/ and install it. You should then be able to connect via the IP address of the pi, login to it and have full access and control as if you controlling it with your keyboard, mouse and monitor.
Remote shutdown:
Next you need to know how to shutdown and restart your rpi safely. It is a computer after all and I can’t tell if just unplugging power will corrupt the SD card, so a safe method of shutdown is required. I use a couple. There’s a script you can add to allow shutdown by connection one of the GPIO pins to ground. This is essentially what you do on a regular computer’s power button. You tell the computer you’d like it to shutdown so it will trigger the shutdown functions. Secondly, you might want the ability to shut down or restart via ssh.
sudo shutdown -h now
or
sudo poweroff
and a restart is
sudo reboot
I’ve added a plugin and scripts to my octoprint setup to do the GPIO and I can shutdown from the web interface.
Now if at any point you mess up and can no longer communicate with the pi (setting the wrong IP address, etc) simply format the SD card in windows or in a digital camera and try again.
I recommend once you get all your settings correct, you backup your Rpi OS periodically. There’s a script that you can use to copy a bootable filesystem to another (can even be smaller) SD card you plug into a USB card reader on the pi.
Hi, I’ve been enjoying your posts for a while! I also have a blog were I describe a problem I came across when using Balena Etcher where I would get an annoying checksum error when verifying an SD card. See here: https://dfdm-consulting.blogspot.com/2020/02/raspbian-sd-card-checksum-errors.html
Also, a while back I posted and then commented on my own blog on a problem I had with reusing an SD card from a Rpi:
“Note to self: if the microSD card has already been used in an RPi and you need to install a new version of Raspbian, the partition visible to Windows will be too small for Etcher to write to (for example 41 MB on a 16 GB card)
First you will need to use the Windows CMD prompt to run DISKPART where you can ‘select’ and ‘clean’ the SD card boot partition.
Eject the SD card and reinsert into you Windows computer.
The full card will then be available to Etcher.”
Thanks for the tips! I’m glad you’re enjoying the content here