GET HEADLESS WITH RASPBERRY PI OS
Desktop? Where we’re going we don’t need desktops
Set Headless Resolution
When accessing Raspberry Pi OS remotely, without a monitor attached, you will find the desktop defaults to a tiny 640×480 resolution. There is an easy way to fix this. Open Raspberry Pi Configuration and click Display. Set Headless Resolution to a higher setting (it will need to be compatible with your monitor). Click OK and you will be prompted to restart your Raspberry Pi. When it reboots, you’ll be using the new resolution.
“Your Raspberry Pi computer’s hostname is well worth getting to know”
Set a custom hostname
Your Raspberry Pi computer’s hostname is well worth getting to know. The hostname is a label that can be used to identify and locate your Raspberry Pi on a network. By default, all Raspberry Pi computers have the hostname ‘raspberrypi’. You can connect to a Raspberry Pi using the hostname and a ‘.local’ suffix instead of the IP address.
ssh pi@raspberrypi.local
If you have more than one Raspberry Pi, it’s a good idea to start renaming them. Open the Raspberry Pi Configuration tool under Raspberry Pi menu > Preferences and choose the System tab. Change the text field in ‘Hostname’ to your custom name and click OK. You will need to restart Raspberry Pi OS for the change to take effect.
Remote control: SSH
SSH (Secure Shell) is the simplest way to access a Raspberry Pi on your network. The downside is that it works via the Terminal and is command-line based. You need to activate SSH on your Raspberry Pi first. Launch Raspberry Pi Configuration from the Preferences menu and choose Interfaces. Set the radio button next to SSH to Enabled and click OK. You’ll need the IP (Internet Protocol) address for your Raspberry Pi, which you can see by hovering your mouse over the network icon in the menu bar. It will be a sequence of four numbers separated by dots. Ours is: ‘192.168.0.40’ and the username of Raspberry Pi, which on our computer is ‘pi’. Open a Terminal app on a macOS or Linux system, or PowerShell on a recent edition of Windows. Many Windows users use a Terminal app called PuTTY ( putty.org ). To connect, you need to enter `ssh <user>@<ip address>`. For example:
ssh pi@192.168.0.40
You will be prompted for that username’s password of the remote computer (the Raspberry Pi you are connecting to). Enter it and respond with ‘yes’ to the security prompt. You will only see this prompt the first time you attempt to log in. You will now be signed in to your Raspberry Pi.
When you first set up Raspberry Pi, you generally connect a screen, keyboard, and mouse and navigate Raspberry Pi OS using the graphical user interface. This isn’t the only way to use Raspberry Pi though, and you can install Raspberry Pi OS Lite (64-bit). This is a port of Raspberry Pi OS with no desktop environment – everything takes place in the Terminal. There are a lot of advantages to running Raspberry Pi OS in headless mode. Especially if your project is going to be used remotely, without a screen attached (say, as a weather monitor or security camera). Once these projects are set up, you’ll only access them remotely via SSH, so it makes sense to set them up as light as possible. The download is only 0.4GB, so it fits on smaller microSD cards. Thanks to a recent update in Imager, it’s much easier to add custom settings to Raspberry Pi OS during the installation procedure. You can now add custom hostname and wireless LAN settings, as well as activate SSH for remote access. This makes it possible to fully set up a headless Raspberry Pi without ever having to attach a keyboard and monitor.
Use Imager to set up a headless Raspberry Pi
Install Imager
Imager is an app for Windows, macOS, and Linux that makes it easy to set up Raspberry Pi OS and other operating systems. View installation instructions at magpi.cc/imager. Open Imager and attach a microSD card to your computer. Click on Choose OS and select Raspberry Pi OS (other) and Raspberry Pi OS Lite (64-bit) if you are using a Raspberry Pi 3, 4, 400 or Zero 2 W; or 32-bit if you are using an earlier computer.
Set the hostname
Choose Storage and select your microSD card (we’re using a 4GB microSD card as we don’t need the full 8GB required by the full desktop OS). Before starting the process, click on the Advanced Options cog in the bottom-right. Here you can set up a range of options for the install, which will be very helpful when running headless mode. Start with ticking ‘Set hostname’, and change it to something unique on your network; we’re changing from the default ‘raspberrypi’ to ‘zero2’.
Connect to the network
Check the ‘Set username and password’ and enter a password. Now check ‘Configure wifi’ and enter your SSID (wireless network name) and wireless LAN password. Finally, set the locale settings to your location. Click Save when you’re done. Click Write and Yes to record the Raspberry Pi OS Lite operating system to the microSD card. And Yes again to the warning and enter your password if required.
Connect via SSH
Wait till it’s finished and transfer the microSD card to your Raspberry Pi. Now boot up and Raspberry Pi will connect to your local wireless network. And you can connect to it via SSH from Terminal on another computer. Wait for Raspberry Pi to boot and use the custom hostname (that you set up in Imager) to connect to it. You can locate the IP address using:
ping zero2.local
And connect to the computer using SSH and the hostname:
ssh pi@zero2.local
Or via the IP address (substituting the numbers for Raspberry Pi on your network):
ssh pi@192.168.0.22
From here on in, you can access your Raspberry Pi via SSH on another computer. This setup is ideal for Raspberry Pi projects that don’t use a keyboard and screen, and which embed Raspberry Pi in a mobile or hard-to-reach location, such as a robot or security camera.
“There are a lot of advantages to running Raspberry Pi OS in headless mode”
Decode the LEDs
Raspberry Pi boards have a red power light and a green status light. If Raspberry Pi fails to boot or has to shut down, the LED will flash a specific number of times to represent the situation. Use this table to decode the LEDs.
| Long flashes | Short flashes | Status | | :--- | :--- | :--- | | 0 | 3 | Generic failure to boot | | 0 | 4 | start*.elf not found | | 0 | 7 | Kernel image not found | | 0 | 8 | SDRAM failure | | 0 | 9 | Insufficient SDRAM | | 0 | 10 | In HALT state | | 2 | 1 | Partition not FAT | | 2 | 2 | Failed to read from partition | | 2 | 3 | Extended partition not FAT | | 2 | 4 | File signature/hash mismatch – Pi 4 | | 3 | 1 | SPI EEPROM error – Pi 4 | | 3 | 2 | SPI EEPROM is write protected – Pi 4 | | 3 | 3 | I2C error – Pi 4 | | 4 | 4 | Unsupported board type | | 4 | 5 | Fatal firmware error | | 4 | 6 | Power failure type A | | 4 | 7 | Power failure type B |
The joy of text with Raspberry Pi’s command-line interface
Switch to root
An elevated `su` command on its own switches to the root user. Enter:
sudo su
…to run a shell as the root user. Every command you enter will be as a superuser, so you do not need to use `sudo` or enter a password. This can be helpful with complex installations that require multiple sudo privileges. It’s a bad habit though, and you should only use `sudo su` when required and exit from the root shell as soon as possible (enter `exit`). You can learn more about sudo in Raspberry Pi’s documentation (magpi.cc/sudo).
Config.txt
The `config.txt` file is read by the GPU before the ARM CPU and Raspberry Pi OS Linux is initialised. It is located on the first (boot) partition of the microSD card alongside the `bootcode.bin` and `start.elf` files. It is possible to view the values of `config.txt` without opening it, using `vcgencmd get_config <config>`. For example:
vcgencmd get_config arm_freq
…will return the CPU frequency set in `config.txt`. You can also use `int` and `str` to see all values stored as integers and strings.
vcgencmd get_config int vcgencmd get_config str
You can edit `config.txt` using:
sudo nano /boot/config.txt
It is also one of the files you can edit manually by removing the microSD card from Raspberry Pi and attaching it to another computer (macOS, Windows, or Linux). Any changes you make to `config.txt` will only take place after you reboot Raspberry Pi. Take a look at the official Raspberry Pi documentation (magpi.cc/configtxt) to discover more about the settings in `config.txt`.
Raspi-config is your friend
When Raspberry Pi first came out, raspi-config was used widely to adjust settings without diving into the underlying configuration files. This handy tool can be accessed from Terminal with:
sudo raspi-config
These days it’s easier to use Raspberry Pi Configuration (under menu > Preferences) when in the desktop interface. But if you’re using Terminal, headless, or accessing via SSH knowing raspi-config can be a game- changer. See magpi.cc/raspiconfig for more info. Behind Raspberry Pi Configuration and raspi-config is a text file called `config.txt`. This sets many settings for Raspberry Pi OS.
Change default shell
You can change your default shell from Bash to Zsh (or earlier shells) using:
chsh -s $(which zsh)
You will be asked to enter your admin password. And changes only take effect on restart:
sudo reboot
Open a Terminal window and you will be greeted with Z Shell. To get back to Bash, enter:
chsh -s $(which bash)
…and reboot.
Go back in time
If you want to go the other way, you can install earlier shells.
sudo apt install tcsh sudo apt install csh
Then open into them with `csh` or `tcsh`. They are a lot more limited compared with the default Bash shell, and csh (C Shell) doesn’t have tab autocomplete or history. Use `exit` to back out and back to your default shell.
“Change your default shell from Bash to Zsh”
Tab completion
It’s a basic hack but if you don’t know it, tab completion is a game-changer. When using the Terminal, simply start typing a command or directory location and press the **TAB** key to complete it. For example, to navigate to the Documents folder, open a Terminal window and enter:
cd Doc
…and press **TAB**. Hey presto, it automatically fills up the rest. If there’s more than one location, it’ll show you the options and let you carry on entering letters. Try:
cd D
…And press **TAB** and you’ll see ‘Desktop/ Documents/ Downloads’. Carry on entering letters until you can autofill the command. You can use **TAB** multiple times in each command to fill out commands, files name, directory paths, and much more.
Top and htop
Top is an app used to display Linux processes, you can get it from the command line using:
top
On the right is the COMMAND column, which has the name of the processes running. The PID is the process ID (the number of each process. The VIRT, CPU, and MEM columns tell you how much virtual memory, physical memory, and CPU usage for each process. Unlike most Terminal commands, top runs in an interactive mode with constantly refreshing windows. Press **H** to view keyboard commands, and **ESC** to return to the main window. Press **Q** to quit top and return to the command line. Htop is a more interactive, colourful version of top. Enter this at the command line:
htop
…And press **Q** to quit.
sudo apt-get install htop
Pressing **H** or **F1** will bring up the help menu. Enter `man htop` to take a look the manual, and the `htop.dev` website has more information on how to use the program.
Use vcgencmd
The `vcgencmd` tool is a special terminal command designed for Raspberry Pi. It outputs information from the VideoCore GPU on Raspberry Pi and is especially handy for getting information on all aspects of hardware performance. Get a complete list of all the `vcgencmd` commands using:
vcgencmd commands
You can use:
vcgencmd measure_temp
These return the current system temperature. And check how much RAM is in your Raspberry Pi with:
vcgencmd get_config total_mem
Measure the current screen resolution with:
vcgencmd get_lcd_info
Get more information on using `vcgencmd` in Raspberry Pi’s documentation pages (magpi.cc/vcgencmddoc). For the curious, take a look at the source code for the `vcgencmd` utility on GitHub (magpi.cc/vcgencmdgit).
Oh My Zsh
Oh My Zsh (**ohmyz.sh**) is an extension to ZSH which enables you to take things even further, with a large number of alias shortcuts, themes, and plug-ins that enable you to add a huge range of functionality to the shell – everything from integrated web search to advanced GitHub functionality. Install Oh My Zsh using a script (also available on the website):
“Zsh has a bunch of modern features”
``` sh -c "$(curl -fsSL https://raw.github.com/ ohmyzsh/ohmyzsh/master/tools/install.sh)" ```
Take a look a the documentation if you want more information on Oh My Zsh (**magpi.cc/omzwiki**), and bookmark the cheatsheet (**magpi.cc/omzcheatsheet**). There are some great features in Oh My Zsh: you can use **1**, **2**, **3**, and so on to return to preview directories. Also, be sure to make use of Oh My Zsh’s extensive plug-ins (**magpi.cc/zshplugins**). These feature everything from web search integration to *Hitchhiker’s Guide to the Galaxy* quotes.
``` sudo apt install zsh ```
Then start Zsh with:
``` zsh ```
``` take test ```
...is equivalent to:
``` mkdir test cd test ```
A new **where** command joins **which** and shows all locations of a file on Path. To get back from Zsh to your usual Bash shell, enter:
``` exit ```
…And you’ll be back to Bash. Take a look at the Zsh website (**zsh.sourceforge.io**) for more information and documentation.
Experiment with Zsh
Terminal uses Bash (Bourne Again Shell) by default, which provides many of the advanced features such as tab completion and brace expansion. However, it’s not the final word in shell technology and you can switch to the more recent Zsh (Z Shell). This is an expanded Bourne shell. ZSH has a bunch of modern features.
The first time you run Zsh, you will be offered Z Shell configuration options. This sets up the **~/.zshrc** file (the configuration file). For the first time, we recommend using option ‘**2**’, which populates the **.zshrc** file with recommended settings. Z Shell has a colourful interface and a number of useful features. For example, in Zsh you now get the new **take** command, which combines **mkdir** and **cd** so you can make a directory and enter it with a single command: