The Raspberry Pi is a small single board computer that was originally intended for education and developing countries. In the last 5 years, it achieved way more than this, as it is now one of the best and easiest platforms for tinkerers and DIYers. It is basically a full Linux computer with a small form factor and energy consumption, making it ideal for homemade projects.
Most of the time, you can use the Raspberry Pi as a headless machine, without a monitor, keyboard or mouse, and just use Telnet or SSH to access it. This considerably reduces the total cost of a project, as there will be no need to buy a screen, keyboard or mouse. However, the tricky part of this setup is the first installation of the OS. In this tutorial we’ll learn how to set up a Raspberry Pi in a fully headless way.
Download the OS image
First, let’s go to the download page for the Raspbian operating system. Feel free to use any other OS, as long as it has SSH enabled. As we won’t use the user interface, you can download the Lite version of the OS.
Once the download is finished, extract the .zip file. This will give you a .img file, which is the image we’ll copy to the SD card.
Copy the image to the SD card
For this step, I will describe the steps for a Mac OS X system. If you’re using Linux or Windows, there is a very good tutorial on the website of the Raspberry Pi Foundation.
- Connect your SD card to your computer, and format it to FAT32 if it’s not in this format already.
- Open a terminal, and find the name of your SD card by running the command diskutil list . On the list of disks, you’ll see a FAT_32 device. Take note of the disk number (here, /dev/disk2). Do not use the partition name (disk2s1).
- Before we copy the image to the SD card, we must unmount it, using the command diskutil unmountDisk /dev/disk2 (of course, replace disk2 by the number of your disk).
- Now copy the image, by typing sudo dd bs=1m if=2017-08-16-raspbian-stretch-lite.img of=/dev/rdisk2 . Enter your password when prompted. Once the command has started, there will be no progress bar or any indication of the time left. You can, however, get the current status by sending the signal SIGINFO to the process, which is Ctrl+T
In the latest versions of Raspbian, SSH is disabled by default. It wouldn’t be an issue for a desktop use of the Raspberry Pi (with a screen and keyboard), but in our case, we need it to be enabled at startup. To do this, simply create a file named ssh , without any extension or content, on the boot partition of the SD card.
Once the command dd has finished copying the image, go to the boot partition on your SD card. On OS X, type cd Volumes/boot . Here, create the file ssh by typing touch ssh . You can type ls to make sure the file has been created:
SSH into your Raspberry Pi
Power it up
Now that the SD card is ready, insert it in the Raspberry Pi. Connect the Pi to your router with an Ethernet cable, and power it up. For the first set up, we can’t use the Wi-Fi, even on the Model 3, because it always needs some kind of configuration. This is why we have to use a cable here.
Find the IP address of your Pi
To find the IP address of the Raspberry Pi, we’ll use nmap. If it is not installed on your computer, you can find it on the official website. Scroll down on the download page to “Mac OS X binaries”.
Make a ping scan with nmap, using the option -sP. This will only list the devices that responded to your ping, which means the online devices. On the list of hosts from nmap, you should see a device named raspberrypi. This is the IP address we want to use.
~$ nmap -sP 192.168.1.*
Starting Nmap 7.50 ( https://nmap.org ) at 2017-09-03 12:02 CDT
Nmap scan report for raspberrypi (192.168.1.80)
Host is up (0.012s latency).
Connect to the Pi
Now, SSH into the Raspberry Pi by typing ssh pi@[IP_ADDRESS] . Replace [IP_ADDRESS] by the address of your Pi.
If you get an error saying that the remote host identification has changed, don’t worry. Just generate a ssh key for this host, by typing ssh-keygen -R [IP_ADDRESS] .
@ 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
Please contact your system administrator.
Add correct host key in /Users/Alexandre/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/Alexandre/.ssh/known_hosts:4
ECDSA host key for 192.168.1.80 has changed and you have requested strict checking.
Host key verification failed.
Then, you can type the SSH command again ssh pi@[IP_ADDRESS] , and it will connect.
The default password for the user pi is raspberry.
Configure the system
Update the system
Before anything else, let’s update the Pi to the latest version, by running
sudo apt update
and then sudo apt dist-upgrade .
Open the configuration menu
The first thing you want to do once you are remotely controlling your Raspberry Pi, is to configure it. Run the command sudo raspi-config . This will open a menu where you can change different settings.
Change the user password
To change the user password for the current user (pi), select the first option. You will be asked to enter your new password twice. As the default password for the user pi is always raspberry, it is extremely important to change it from the beginning. Otherwise, anyone with access to your Raspberry could login as the pi user.
To make sure SSH will always be enabled, select the 5th option (Interfacing Options), and then select P2 (SSH). Then, select Yes when asked if you would like SSH to be enabled.
Expand the filesystem
By default, the filesystem is made to occupy the least amount of space possible. To expand it and let it use all the available space on the SD card, go to option 7 (Advanced Option) and select Expand Filesystem.
Configure the Wifi
If you’re planning on connecting your Raspberry Pi with Wifi, then you need to configure it so that you can get rid of the Ethernet cable. Here you can find another tutorial on how to connect your Raspberry Pi to a Wifi network from the command line.
Set up a static IP address
As we’re going to use the Raspberry Pi exclusively via SSH, we need it to have a static IP address. This will make it easier when we want to connect, as we won’t have to find its IP address with nmap every time. For a a detailed explanation about this, check this article on how to give a static IP address to your Raspberry Pi.
Reboot the Raspberry Pi
Some of the settings need a reboot to be applied. Reboot your Raspberry Pi by typing sudo reboot.
That’s it! Now your Raspberry Pi is all set up to be used as a headless server. If you followed the tutorials on how to connect it to a Wifi network and how to give it a static IP address, you won’t even have to find out its IP address, as it will always remain the same. Whenever you need to work on your Raspberry Pi, you’ll just have to SSH into it using the IP address you assigned it, and the password you set up for your user. Then it will be exactly like using it with a regular interface with a keyboard and screen, except you will only have a command line interface… But really, who needs a desktop environment?!