Sunday 1 July 2012

First Boot

Power on Pi

What you see depends on how you've wired up your Pi. I strongly suggest you actually plug a telly or something in so you can monitor progress. I didn't, well not at first. And when SSH told me no route to host, that's when I resorted to making a video out connection. I plugged the Pi's composite out into my PVR-150 capture card ... so I could monitor booting and resolve SSH issues.
For those wanting a similar experience, this is how I got the grainy image of a Pi booting on my linux host machine.
v4l2-ctl -d /dev/video0 -i 2 # Tune card to composite 1 input
v4l2-ctl -d /dev/video0 -s pal-60 # Set input to UK pal-60
cat /dev/video0 | mplayer - # view stream in mplayer
What I saw was a steady boot up until registering the WiFi adaptor, where abouts progress stalled and the device appeared to hang. I'd read elsewhere of reports of conflicts between USB peripherals, so I yanked the keyboard receiver out, and immediately the Pi boot continued. Curious to know whether this was an irreconcilable issue, at the point of prompting for log-in I reinstated the keyboard USB dongle and surprisingly all seemed well.

Log in pi

The Debian Image comes with one preconfigured Super User account who's password is raspberry. We are going to change that post-haste. Log in with these credentials and proceed to configure accounts.
passwd pi # change the pi account's password
sudo su - root # use pi to switch user to root
passwd root # change the root account password
exit # drop out of root, back to pi account
sudo adduser {supply a user name} # add a new user to the system
sudo su - root # switch back to the root account
visudo # Add the new user to the list of super users (see below for more on visudo)
exit # drop out of root, back to pi account
exit # log out of pi
visudo is a text editor designed specifically for editing the sudoers list. That is, a list of users and their associated special privileges. We need to add our new user to this list and grant them the ability to run all commands normally executed by root, when prefixed with sudo. In Debian only root can run visudo. The editor is based on vi, a low level text editor that requires knowledge of special key strokes to do stuff. We simply need to scroll down (arrow key down) to the section as shown below (around line 18). You'll replace ian with your new username.
# User privilege specification
root    ALL=(ALL) ALL
suse    ALL=(ALL) ALL
pi      ALL=(ALL) ALL
ian     ALL=(ALL) ALL
Having made the change, save the edits by hitting ctrl-k x

Log in new user

You can now log in as the new user and finish off our pre play customizations. Lets set up passwordless SSH. This will enable us to connect to our Pi from other computers without the need to enter login details all the time. Accept defaults for all in the following.
cd # just to be sure we are top level home.
ssh-keygen # create a key pair, but just press enter when prompted for a passphrase
cd ~/.ssh # enter the .ssh directory to do more with your key.
ln id_rsa.pub authorized_keys # ssh expects the public key to be called authorized_keys
cd # go home.
We will need to know the IP address of our Pi (line in the next command is a pipe character e.g. shift back slash left of z on a UK keyboard)
ifcongfig | grep inet # its the dotted number left of the bottom line.
E.g.
ian@raspberrypi:~$ ifconfig |grep inet
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet addr:192.168.1.90  Bcast:192.168.1.255  Mask:255.255.255.0
Most modern routers automatically reserve an IP address when they issue to a new device. So I can be fairly sure everytime I boot Pi it will always get 192.168.1.90. However, there are two things we can do to make this safer. We can set up a static IP address by editing the network interface, or we can navigate to the webadmin page of the router and reserve the address for the PI. The latter is the easier, assuming you know how to log in to your router. I did, so that's what I've done. When you are reserving addresses you need to know the MAC address (hardware address, HWaddr) of the questionable device, as shown below:
ian@raspberrypi:~$ ifconfig |grep wlan0 |grep HWaddr
wlan0     Link encap:Ethernet  HWaddr 54:04:xx:df:xx:xx
Now head back to your host machine and grab the private half of your key-pair. To do a proper job, you should put this in .ssh of your local account. If you've not got one, make one with mkdir ~/.ssh, but don't do that until you've checked with ls ~/.ssh first. You should also add your Pi's IP address to your /etc/hosts file so that we don't need to type the IP address in every time we connect to it. The following assumes you are sudo on your main machine, otherwise su - root, or don't bother. Also, replace user with your new user account name.
sudo echo "www.xxx.yyy.zzz pi pi" >> /etc/hosts # add pi to local DNS
cd ~/.ssh # move back to your host .ssh directory
scp user@pi:~/.ssh/id_rsa pi_key # snatch the private key from your pi
cd # move back home
ssh -l user -i ~/.ssh/pi_key pi # test out your passwordless ssh
I'm too lazy to type that in each time, so I make an entry in ~/.ssh/config to simplify the process. Edit your ~/.ssh/config, or make a new one in your favourite text editor. Mine looks like this, note the bit about compression helps speed up transfers.
host pi
hostname pi
identityFile ~/.ssh/pi_key
compression yes
compressionLevel 6
Now I can connect to the Pi simply by typing ssh pi Which you should do because we need to be back in the Pi to set up VNC.

VNC at boot

VNC lets us connect to a Pi desktop environment from the comfort of our main machine. The Pi can be enjoyed without a dedicated keyboard, mouse and monitor. We call it working headless, though it doesn't really involve decapitation. Services that start at Boot time are controlled by the update-rc.d command. Essentially, we need to create a script that runs a vncserver before login. NB. replace XXX on lines 14/15 with your username The script below was originally posted at elinux, and retweeted to the Pi community by dktucson.
### BEGIN INIT INFO
# Provides: vncboot
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start VNC Server at boot time
# Description: Start VNC Server at boot time.
### END INIT INFO

#! /bin/sh
# /etc/init.d/vncboot

USER=XXX
HOME=/home/XXX

export USER HOME

case "$1" in
start)
echo "Starting VNC Server"
#Insert your favoured settings for a VNC session
su - $USER -c "/usr/bin/vncserver :1 -geometry 900x640 -depth 16 -pixelformat rgb565 &" &
 ;;

stop)
echo "Stopping VNC Server"
/usr/bin/vncserver -kill :1
 ;;

*)
echo "Usage: /etc/init.d/vncboot {start|stop}"
exit 1
 ;;
esac

exit 0
First we need to update the pi package list, and install our vncserver program. To manage software on the Pi we need to use apt The Advanced Packaging Tool. Read all about it here.
apt-get update
apt-get install tightvncserver
Now we set up a password to gain access to the desktop.
vncpasswd user # replace user with the account name
Now fire up a text editor, copy and paste the boot script from above.
sudo nano /etc/init.d/vncboot
{ copy and paste}
{ ctrl-O, ctrl-X}
sudo update-rc.d vncboot defaults
That's it. You can reboot your pi and enjoy it from a far thanks to VNC. You'll need a vnc client on the remote machine. Assuming you've got one you can connect to the pi thus:
vncviewer pi:1
Here's my local machine ssh'd into pi and viewing the public key (left) and a VNC viewer displaying my blog post with a terminal showing the public key too.

No comments:

Post a Comment