Headless Pi

From Hackerspace Adelaide
(Redirected from Find a Pi)
Jump to: navigation, search

Enable SSH[edit]

As of November 2016, Raspbian has SSH disabled by default for security reasons. Probably a good idea since the password is well known.

Anyhow, you can enable it by putting an empty file named "ssh" (no extension) in the BOOT partition.

On a running Pi this is mounted on /boot

Find a Pi on the network[edit]

See The Adafruit Raspberry Pi Finder for a less nerdy option.

On a Debian-ish system, this will try to quickly find the IP address of a Raspberry Pi (with the default username / password) connected to eth0:

$ sudo apt-get install nmap sshpass

$ export IF=eth0 SSHUSER=pi SSHPASS=raspberry ; for cidr in $(nmap --iflist | awk -n '/ROUTES/ {exit} $1==ENVIRON["IF"] && !match($3,":") {print $3}') ; do nmap -n -Pn $cidr -p 22 --open -oG - | awk -n '$4="Ports:"&&match($5,/open/) {print $2}' | xargs -I{} sshpass -e ssh -l $SSHUSER -qoStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null {} echo {} looks like a Pi ; done

This will possibly work on OSX if you install nmap and sshpass using homebrew, and if you change eth0 to en0 (untested).

Output looks like: looks like a Pi

--pix (talk) 00:45, 28 July 2016 (CST)

Automatically fix SD card on boot[edit]

It's not hard to corrupt an SD card by cutting power to the Pi at the wrong time.

This often causes it to ask for the root password (which is normally unknown) in the console (which you can't see) during boot to do an interactive fix.

Changing the FSCKFIX option in /etc/default/rcS to yes will make it try to do an automatic fix, which should be sufficient if things aren't really messed up.

Work on a Pi SD card on a non-ARM system[edit]

Simplified from this StackExchange answer

On a Debian-ish system, if you insert the SD card and click on it in Nautilus (ie, the finder), it will normally turn up somewhere in /media/username.

It will normally appear as two different partitions. One called raspberrypi or BOOT and one with a-long-ugly-uuid.

The long ugly one is the one we want as it is the the actual unix filesystem. For this example, lets pretend it gets mounted in /media/user/deadbeef.

Install some stuff on your host machine[edit]

First we install a static build of qemu:

$ sudo apt-get install binfmt-support qemu qemu-user-static

Preparing the filesystem[edit]

Now, we are going to do a little tinkering with the Pi filesystem before we chroot into it, so, to make the paths less annoying, we change directory:

$ cd /media/user/deadbeef

Then, we copy the static arm qemu in to /usr/bin on the Pi:

$ sudo cp /usr/bin/qemu-arm-static usr/bin

New versions of Raspbian pre-load a Pi-specific library that breaks qemu, so we move it away temporarily:

$ sudo mv etc/ld.so.preload etc/ld.so.preload-orig

Then we remount some of the magical directories from our current linux session into the Pi file system (temporarily).

$ sudo mount --rbind /dev dev

$ sudo mount -t proc none proc

$ sudo mount --bind /sys sys

Diving in[edit]

Then, the magical moment:

$ sudo chroot .

This will open a new root shell, where the root of the filesystem starts in /media/user/deadbeef. Additionally the qemu stuff we installed means that all of the arm-compiled binaries in this system will "just work". It's actual magic.


bash not found[edit]

If you get an error about /bin/bash not being found, your Pi image might be very minimal, and you can try again with a less-ambitious shell:

$ sudo chroot . /bin/sh

no net access?[edit]

Sometimes the chroot won't be able to use your net connection. In that case, have a look at the apt-get install without internet section below.

While we're here...[edit]

Once you are in you can add some extra packages, like

# apt-get install avahi

to make it easier to find your Pi in future by connecting to raspberrypi.local instead of hunting for its IP.

And then change the hostname so that you can find yours amongst all the other rasberrypi.local's:

# echo betterhostname > /etc/hostname

Cleaning up[edit]

To unmount, you will need to:

# exit

Put the ld.so.preload file back:

$ sudo mv etc/ld.so.preload-orig etc/ld.so.preload

Unmount all of those magical device directories:

$ sudo umount dev/pts dev proc sys

Get out of the SD card directory so we can eject it:

$ cd

Then eject the SD card from Nautilus (the graphical file manager thingy, whatever-you-call-it).

Set up the WiFi settings by editing the SDCard[edit]

We'll assume that the ext4 partition (the one with the ugly name) has been mounted in /media/user/deadbeef.

Tell the Pi to automatically connect to WiFi[edit]

Edit the file /media/user/deadbeef/etc/network/interfaces and change

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf


auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

In other words, add auto wlan0 and change manual to dhcp.

Set up the WiFi connection information[edit]

Edit the file /media/user/deadbeef/etc/wpa_supplicant/wpa_supplicant.conf and add:


Yes, that's all. There are lots of other options, but they have sensible defaults that are permissive (that is, in most cases specifying more options only limits the range of connections that will work).

Obviously change ssid (ESSID or network name) and psk (password/passphrase) as appropriate if you aren't trying to connect to the router at the Woodville Hacksession.

EAP (untested)[edit]

On an EAP connection (with usernames and passwords) supposedly you can just replace the psk with identity and password. Eg:


apt-get install without internet[edit]

Sometimes the problem is you can't get the Pi online, but you need to install some packages. Here's how you can download the required packages separately for install.

Using the option --print-uris to apt-get, it prints the URLs that would have been downloaded instead of trying to download them.

(NOTE: this example is not being run on a Pi, so the package names and resulting URIs are just placeholders and wouldn't be useful in reality)

$ $ apt-get install --print-uris i2c-tools
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
# bla bla bla...
Use 'apt-get autoremove' to remove them.
Suggested packages:
  libi2c-dev python-smbus
Recommended packages:
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 277 not upgraded.
Need to get 58.4 kB of archives.
After this operation, 193 kB of additional disk space will be used.
'http://ftp.iinet.net.au/debian/debian/pool/main/i/i2c-tools/i2c-tools_3.1.1-1_amd64.deb' i2c-tools_3.1.1-1_amd64.deb 58438 MD5Sum:e7c43885069f3be658d8672522c48048

So on a machine with net access you can do something like:

$ wget http://ftp.iinet.net.au/debian/debian/pool/main/i/i2c-tools/i2c-tools_3.1.1-1_amd64.deb

Then somehow get the resulting i2c-tools_3.1.1-1_amd64.deb on to the Pi, and run

$ sudo dpkg -i i2c-tools_3.1.1-1_amd64.deb

(also see this askubuntu answer)


New versions of Raspbian include some fancy cloud-connected VNC stuff, but this command will quickly start a password-less VNC session of the main display that you can connect to using the Pi as an SSH tunnel (and "localhost" as the host).

DISPLAY=:0 vncserver-x11 -localhost -Authentication=None