Setup a Raspbian image for headless usage

This HOWTO describes, how to create a bootable Raspbian SD card for the RaspberryPi without the need to connect a monitor to the Pi. Most other HOWTOs ask you to log into the RaspberryPi with monitor and keyboard connected to do the final configurations. We do it over ssh instead, so a network connection and a DHCP server is needed instead.

We do this on Linux, not Windows. It is possible to create the bootable SD card itself on Windows, too, but the steps following afterwards can't be done on Windows. We need:

  • A Linux PC (or virtual machine) with an SD card reader (I use a Laptop with a reader built in)
  • A SD card >= 2 GB (The Raspbian image is 2GB large, but the filesystem can be expanded to use the additional space of a larger card)

Download 

Download the latest official raspian image from http://www.raspberrypi.org/downloads. Choose "Raspbian wheezy". Once finished, you have a zip file named "2013-02-09-wheezy-raspbian.zip" or similar (the date may change). I asume it in ~/Downloads for the further steps.

Create a directory to work with the image and unpack the zip file there:

me@host:~$ mkdir raspiimg
me@host:~$ cd raspiimg
me@host:~/raspiimg$ unzip ~/Downloads/2013-02-09-wheezy-raspbian.zip
Archive: /home/me/Downloads/2013-02-09-wheezy-raspbian.zip
 inflating: 2013-02-09-wheezy-raspbian.img
me@host:~/raspiimg$ ls
2013-02-09-wheezy-raspbian.img

So now you have the unpacked image "2013-02-09-wheezy-raspbian.img" in the raspiimg folder.

Write Image to SD card

There are several ways to write the unpacked image to your SD card described at the download site http://www.raspberrypi.org/downloads. I use the "dd method". The dd method is somewhat dangrous: Make sure - reall, really sure - that you use the right device!!!

I use gparted to have a look at the disks in my machine. First while the SD card is not inserted:

/dev/sda
/dev/sdb

Then Insert the SD card into the reader. Your system will notify you about the new device. Look at the disks gparted sees now:

/dev/sda
/dev/sdb
/dev/mmcblk0

Here it is: /dev/mmcblk0 that is my SD card (the name /dev/mmcblk0 is very common, but maybe your SD card will have a different name)

Now copy the raspian image to the SD card. All data on the SD card will be lost! (More exactly: all data on the of=<device> will be lost, so make sure to use the right one. As this step needs to be done with sudo, you could damage your whole system otherwise.) This will take a while ...

me@host:~/raspiimg$ sudo dd if=2013-02-09-wheezy-raspbian.img of=/dev/mmcblk0 bs=1M
[sudo] password for me:
1850+0 records in
1850+0 records out
1939865600 bytes (1,9 GB) copied, 381,424 s, 5,1 MB/s

 

Resize the filessystem on the SD card

Resizing the RootFS on the SD card can be done on the RaspberryPi via the "raspi-config" utility. Another way is to do it with you Linux PC before booting the RaspberryPi:

I recommend gparted for that:

 

Select the SD card device in the the upper right, /dev/mmcblk0 in this case:


right click on the largest (last) partition, select "Resize/Move", the following dialog will open:


Pull the right Edge of the partition all trough the unallocated space to the right until free space becomes zero

Push "Resize/Move"

Back in the Main Window, Click "Apply". The result should look like this:

 

If you have trouble applying your changes, it may help to remove and re-insert the SD card and restart gparted.

Change some settings on the RootFS

As we do not want to (or can't for some reason) connect monitor and keyboard to the RaspberryPi, we have to adjust some settings inside the RootFS on the SD card, so wen can log in via SSH once the RaspberryPi is bootet from the SD card.

  • Set a hostname, so we know where to ssh to
  • enable ssh-server on the target
  • If no DHCP server is available, set a fixed IP address in /etc/network/interfaces (on the SD card)
me@host:~$ sudo mount /dev/mmcblk0p2 /mnt
[sudo] password for me:
me@host:~$ cd /mnt
me@host:/mnt$ ls
bin boot dev etc home lib lost+found media mnt opt proc root run sbin selinux srv sys tmp usr var

Now, the RootFS of the RaspberryPi (located on the SD card) is mounted to /mnt on your host and can be modified. The RaspberryPi's /etc directory is now /mnt/etc on your host.

Set the hostname

Edit the hostname in

/mnt/etc/hostname

and the 127.0.1.1 entry in

/mnt/etc/hosts

Enable SSH server

cd /mnt/etc
sudo ln -s init.d/ssh rc2.d/S02ssh
sudo ln -s init.d/ssh rc3.d/S02ssh
sudo ln -s init.d/ssh rc4.d/S02ssh
sudo ln -s init.d/ssh rc5.d/S02ssh

sudo ln -s init.d/ssh rc2.d/K01ssh
sudo ln -s init.d/ssh rc3.d/K01ssh
sudo ln -s init.d/ssh rc4.d/K01ssh
sudo ln -s init.d/ssh rc5.d/K01ssh

 

Unmount the SD card:

sudo umount /mnt

Boot the RaspberryPi from the SD card. The Board will respond to ping very soon after power on, but the ssh daemon will takt a little while (~30sec) to become ready. Login with user "pi" and password "raspberry":

ssh pi@raspi1
The authenticity of host 'raspi1 (192.168.13.25)' can't be established.
ECDSA key fingerprint is f4:28:53:2d:e3:b9:a0:5b:f6:c1:13:c2:e7:bf:12:fd.
Are you sure you want to continue connecting (yes/no)? yes
pi@raspi1's password:  
Linux raspi1 3.6.11+ #371 PREEMPT Thu Feb 7 16:31:35 GMT 2013 armv6l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.

NOTICE: the software on this Raspberry Pi has not been fully configured. Please run 'sudo raspi-config'

pi@raspi1 ~ $