Tips on Running a Headless Raspberry Pi

A Raspberry Pi with a head.  Well, not the usual meaning of 'head,' I guess.  (Giraffe and Carcassonne box included for scale.)
A Raspberry Pi with a head. Well, not the usual meaning of ‘head,’ I guess. (Giraffe and Carcassonne box included for scale.)

I was recently helping out a friend with a headless Raspberry Pi setup, and thought it would be helpful to consolidate a few useful bits here. From here, you can set up all kinds of cool projects using the GPIO pins, set up a headless web server, or anything else you can think of. For my part, when I hurt my ankle a few months ago, I hooked the Pi into a hard-to-get-to stereo system and logged in remotely from the other side of the room to play music… I also used a headless setup to run the really long compile for the Sage computer algebra system a few months ago.

  1. [EXPAND Get Linux to access the Pi’s filesystem.]
    Being able to mount the Pi’s filesystem allows you to change configuration files without booting the system.  This is helpful for doing things with the Pi both headlessly and without a network.  It’s also useful if you do want to set up headless network access to the Pi. Direct access is easy with a linux machine: you can just mount the Pi’s SD Card directly.  With a Windows machine, a bit more work is involved.  As far as I can tell (I don’t actually have a Windows machine to try things on), the software out there for opening Linux filesystems in Windows tends to be read-only.  This isn’t so helpful for tweaking configuration files.  So here are three other options.

    1. [EXPAND Boot Linux from a LiveUSB.]
      For this, you’ll download an iso from (say) the Ubuntu home page, and then use software like UNetBootin to make the bootable USB key.  From there, you reboot the computer using the live usb, and choose the ‘try Ubuntu’ option.  In my experience, the boot time for a live usb tends to be pretty slow, but speeds up a bit after the first boot (at least if you’ve enabled some storage for the OS, which you should).
      [/EXPAND]
    2. [EXPAND Install a Linux Virtual Machine.]
      A virtual machine lets you run another operating system inside of another operating system without having to reboot.  For this option, you should download VirtualBox and a Ubuntu disk image to run inside Virtual Box.  One place to get such an image is here.  (Note that I can’t actually vouch for the security of any disk image downloaded!)
      [/EXPAND]
    3. [EXPAND Setup your machine to dual-boot.]
      Exactly the same process as making a live usb, but you choose the ‘Install Ubuntu’ option instead.  (Actually, I usually do the ‘try Ubuntu’ option and then run the installer from the desktop, once I have the wireless set up.)  You’ll choose ‘install Ubuntu alongside windows’ to get the dual-boot; it’s actually really easy to install.  You should definitely back everything up, though, and be ok with a small probability of having to do a Windows repair or installation if things go terribly for some reason.
      [/EXPAND]

    [/EXPAND]

  2. [EXPAND Use crontab to automate tasks.]
    Cron is a program for running other programs at timed intervals.  The Pi has a really simple crontab that (unlike most systems) lets you manipulate the ‘crontab’ file directly, which defines which programs to run and when.  (Most systems have some extra security measures for the crontab, because you can use it for some nefarious things.) Here’s a line that I put into my Pi to get network information:

    */5 * * * * root ifconfig > connections.txt; iwconfig >> /home/pi/connections.txt

    This gets information from ifconfig and iwconfig and writes it to a file called ‘connections.txt’ once every five minutes. This is useful for finding out the IP address and SSID that the Pi has connected to. Obviously, the crontab can be used for lots and lots of other things. Like if you hook in a nice webcam and want it to take a picture once every minute and splice the resulting photo into a time-lapse video.
    [/EXPAND]

  3. [EXPAND Setup wireless with wpa_supplicant.]
    If you have a USB wireless adapter for your Pi, you can set up the Pi to connect to a network automatically when it boots up. To do this, open the file ‘/etc/wpa_supplicant/wpa_supplicant.conf’, and add information for a wireless access point. Here’s an example configuration:

    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    
    network={
    	ssid="foundAJob"
    	psk="talkingheads"
    	proto=RSN
    	key_mgmt=WPA-PSK
    	pairwise=CCMP
    	auth_alg=OPEN
    }
    

    This will connect automatically to the network ‘foundAJob’ using the password ‘talkingheads’.
    You may also need to add the following to the end of the file ‘/etc/networks’ to define some network interfaces:

    allow-hotplug wlan0
    iface wlan0 inet manual
    wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
    iface default inet dhcp
    iface usb0 inet dhcp
    

    The ‘iface usb0 inet dhcp’ line lets you tether with an Android phone!
    [/EXPAND]

  4. [EXPAND Set up a wired connection.]
    There are two main methods for getting a wired connection to a headless Pi. The easy one is to just hook the Pi to a router using an ethernet cable. The Pi will connect automatically with DHCP and get an ip address. Once you know the ip address it’s connected with, you can ssh in, as described below. Alternately, you can set up a DHCP server on your machine and directly connect the Pi to your laptop with an ethernet cable. (This USUALLY requires a special ethernet cable called a ‘crossover cable,’ but the Pi’s ethernet is set up to detect direct connections and switch things up accordingly. So you DON’T need a special cable for this!) There are a number of ways to set up the DHCP server on a windwos machine, which I won’t get into here. You can also do this on linux. Since I also use my ethernet port to connect to internet sometimes, I don’t bother with a direct connection since it means setting up and switching out config files every time I want to connect to the Pi instead of a router….
    [/EXPAND]
  5. [EXPAND Use ssh to connect to the Pi.]
    Once you’ve got your network configured and know the Pi’s ip address, you can connect to the pi using SSH from another computer attached to the same network. From a linux terminal, this is as easy as typing ‘ssh pi@192.168.0.121’ (or whatever the right ip address is) and then giving the password, which, by default, is ‘raspberry’. With a Windows machine, you can download PuTTY, a perfectly reasonable SSH program, and use that to connect to the Pi.
    [/EXPAND]