RogerBW's Blog

Setting up a headless Raspberry Pi under Linux 05 June 2014

I have a checklist for setting up a Raspberry Pi, which I run through before I do any task-specific customisation. It occurred to me that people might find this useful.

I don't remember whether I've ever actually plugged a Pi into a monitor; everything gets controlled over ssh. If you're stuck with Windows the commands will differ and you will probably want to do things differently.

I start with a Raspbian image, downloaded from the Raspberry Pi site. I copy this to the SD card with dd, then sync and remove it.

The SD card and network cable are inserted into the Pi; then I power on. This first boot takes a little while, as (among other things) ssh moduli are generated.

I have a DHCP server which interacts with the local DNS, so as soon as the Pi has booted and performed DHCP there is a DNS name "raspberrypi" pointing to it. If you are not so blessed, you'll need to look at the lease list on your network's DHCP server to find out the Pi's IP address and use that rather than the DNS name.

I connect to the Pi with

ssh -o UserKnownHostsFile=/dev/null pi@raspberrypi

(Ignoring the hosts file means that I don't have to worry about other Pis that have shared that hostname.)

On the Pi:

sudo su -

which brings up a menu: I expand the filesystem to fill the SD card, then finish and reboot. This can also take a little while. Later reboots are quicker.

Once the Pi has rebooted, I ssh in again, as above, and:

sudo su -
passwd

to set a root password for the Pi. (I know, the kids today don't use root shells, they run everything through sudo. Tough. If a root shell scares you, that just means you're doing it right.)

Then:

aptitude purge vim-common vim-tiny nano
apt-get update && apt-get dist-upgrade
apt-get install rsync nvi

The first line gets rid of some packages I never want to see. (One could probably add the graphical stuff here, but it takes a while and since it's not started automatically all it consumes when idle is space on the SD card.)

The second line will take a while, and is optional: it gets the system up to the latest versions of everything.

The third line installs my preferred lightweight text editor (if you don't speak vi, you may want to keep nano instead) and a synchronisation program that ends up on everything I build.

From the Linux host:

rsync -e 'ssh -o UserKnownHostsFile=/dev/null' -Pvaz home-pi/ pi@raspberrypi:~/

My "home-pi" directory contains all the files I want by default: my preferred .bash_profile and .bashrc, and my SSH key so that I don't have to keep retyping passwords (.ssh/authorized_keys). If you have no preferences here, you can omit this step.

Back on the Pi, at the root prompt:

cp -a ~pi/.ssh ~/
chown -R root.root ~/.ssh
vi /etc/hostname /etc/default/rcS
shutdown -r now

The first line copies that ssh key to root; the second sets the permissions. You can now log in as root with the ssh key rather than using the password.

Changing hostname is mostly useful because the Pi will register on the DHCP server with that name, so if you have multiple Pis running at once you need be in no doubt as to which one you're talking to.

/etc/default/rcS needs to have its last line changed to FSCKFIX=yes, so that if errors are found during boot the Pi will attempt to repair them and keep going.

Finally, you reboot the machine ready for installation of specific programs, or if you prefer shut it down (shutdown -h now).


  1. Posted by Owen Smith at 01:22pm on 05 June 2014

    So you don't run IPv6 on all your headless Pis then? That needs adding to the modules file.

  2. Posted by RogerBW at 01:25pm on 05 June 2014

    No; for the GPS trackers, I don't use the networking capabilities after initial setup (I just stick the SD card in a socket and mount the filesystem to get data off), so there's not much point. This may change with the birdcam when I get that sorted out.

Comments on this post are now closed. If you have particular grounds for adding a late comment, comment on a more recent post quoting the URL of this one.

Search
Archive
Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action advent of code aeronautics aikakirja anecdote animation anime army astronomy audio audio tech aviation base commerce battletech beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 crime cthulhu eternal cycling dead of winter doctor who documentary drama driving drone ecchi economics en garde espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 essen 2022 essen 2023 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geocaching geodata gin gkp gurps gurps 101 gus harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo 2022 hugo-nebula reread in brief avoid instrumented life javascript julian simpson julie enfield kickstarter kotlin learn to play leaving earth linux liquor lovecraftiana lua mecha men with beards mpd museum music mystery naval noir non-fiction one for the brow opera parody paul temple perl perl weekly challenge photography podcast politics postscript powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs ruby rust scala science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance the weekly challenge thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1