I'm using a RPi2 Model B and my primary computer is a Linux machine. I bought this kit from Amazon.
I like to start with a minimal base and build up as needed. I also like to work from my laptop, accessing the RPi remotely. This means I can work with the RPi without a dedicated keyboard, monitor and mouse. This is known as headless setup. Later, we can connect to the RPi via SSH for terminal access (or VNC if we had an X-server running).
These are just my reference notes. The official docs cover Windows, Mac and Linux in detail.
I download the image from the main website. Specifically the lite distribution.
- https://www.raspberrypi.org/downloads/raspbian/
- https://downloads.raspberrypi.org/raspbian_lite_latest
After the download completes, unzip the .img
file. We're going to flash this image onto the SD card.
First, plugin the SD card and find it's address. We can show all the mounted drives using:
$ df -h
Now we need to unmount all partitions of the SD card using:
$ umount /dev/sdx1 # partition 1
$ umount /dev/sdx3 # partition 3
Now we can flash the image to the SD card.
WARNING: Don't provide in the wrong drive path here. You'll lose everything on the target drive.
$ sudo dd bs=4M if=/path/to/raspbian-lite.img of=/dev/sdx
Notice how we used the drive path without the partition number, /dev/sdx
not /dev/sdx1
.
Now that's done we can eject the SD card and put it in our RPi.
There's much more detail about this in the official docs:
Working with a headless setup depends on a network connection. Things are easy if you plugin your RPi to a router via an ethernet cable. Just find the IP address and you're ready for SSH or VNC.
At home my router is in another room and I'd rather have the RPi near me, networked wirelessly. In order to make our wifi setup work, we'll need to use an ethernet cable. Actually, we need an ethernet cable whenever we need to change the WiFi configuration.
To connect directly to the RPi we need to set the IP address on our computer's ethernet adapter manually. We'll set it to 169.254.0.1
and the subnet mask 255.255.255.0
.
Now we need to set the IP on the RPi. To do this we need insert the SD card and edit the cmdline.txt
file located on the boot partition. Once that file is open we need to add the parameter ip=169.254.0.2
and save the changes.
Now we can eject the SD card, put it back into the RPi and boot it up. Once the RPi is booted up we should be able to ssh into it via:
$ ssh [email protected]
The default password is raspberry
.
NOTE: If you set a static IP in cmdline.txt
, mentioned above, undo that change. We want the RPi to get it's IP address automatically from the router.
Now just plugin the RPi to your router with an ethernet cable and power it on.
Now we need to find out it's IP address. You can do this by looking at the DHCP table of your router. If you don't have admin access to the router you may be able to find it by using:
$ arp -a
Once you have the ip you can connect via SSH like this:
$ ssh [email protected]
The default password is raspberry
.
NOTE: Anytime we need to configure WiFi settings, we'll need to connect to the RPi using an ethernet connection.
Now that we have SSH access we can configure the WiFi adapter. The WPA supplicant toolset comes with Raspbian. The WPA supplicant docs I refer to are here:
While connected to the RPi via SSH over ethernet, we can use wpa_cli
to interactively configure our network:
pi@raspberrypi:~ $ wpa_cli
> add_network
0
> set_network 0 ssid "MYSSID"
> set_network 0 psk "passphrase"
> enable_network 0
<2>CTRL-EVENT-CONNECTED - Connection to 00:00:00:00:00:00 completed (reauth) [id=0 id_str=]
Make sure you're connected before continuing.
Now that we're connected we can save the configuration:
> save_config
OK
(press ctrl+c to exit wpa_cli
)
Ok great. We have our WiFi configured, but it's not going to start automatically. What we want is for WiFi to connect automatically when we boot the RPi. When that happens, we can free ourselves from the ethernet cable and move about with our RPi.
Let's use wpa_passphrase
to get a version of our WiFi password that isn't plain text:
pi@raspberrypi:~ $ wpa_passphrase MYSSID passphrase
network={
ssid="MYSSID"
#psk="passphrase"
psk=59e0d07fa4c7741797a4e394f38a5c321e3bed51d54ad5fcbd3f84bc7415d73d
}
That long psk
is what we're looking for. Now we need to update our config file:
pi@raspberrypi:~ $ sudo vi /etc/wpa_supplicant/wpa_supplicant.conf
Update that file replacing the plain text password with the long one we just generated with wpa_passphrase
and save the file.
Next we need to enable and start the wpa_supplicant
systemd
service.
pi@raspberrypi:~ $ sudo systemctl enable wpa_supplicant
pi@raspberrypi:~ $ sudo systemctl start wpa_supplicant
And now we should be able to disconnect our RPi from ethernet and power, move to another location in range of our WLAN and power the RPi back on. Once the boot process completes we should be able to connect via SSH or VNC again.
NOTE: The IP address obtained by the WiFi adapter will likely be different than the one obtained with the ethernet cable. Make sure you know the IP address of the WiFi connection. You may need to look it up like we did before.
Change the pi
user's password:
pi@raspberrypi:~ $ passwd
Change the root
user's password:
pi@raspberrypi:~ $ sudo passwd
The way we flashed the Raspbian image to the SD card doesn't automatically expand the root partition for the full size. We can inspect the size of our partitions like this:
pi@raspberrypi:~ $ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 1.3G 931M 261M 79% /
#...
There are some lenghty instructions on how to do this manually. Luckily Raspbian comes with a utility that will do this for us.
pi@raspberrypi:~ $ sudo raspi-config
Choose the "Expand Filesystem" option. When it's done you'll be instructed to restart.
Once the RPi is rebooted, connect via SSH and confirm our drive has been resized:
pi@raspberrypi:~ $ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/root 7.2G 933M 5.9G 14% /
#...
With a keyboard connected directly to the RPi, my "
key was producing the @
character and vise versa. We can fix this by updating our locale and keyboard. Fire up the raspi-config
tool and choose the "Internationalisation Options" menu. I chose the en_US.UTF-8 UTF-8
option for the locale. And then I changed the keyboard model to Generic 104-key PC
and changed the keyboard layout to English (US)
.
pi@raspberrypi:~ $ sudo raspi-config
Use the force.