sawyl: (A self portrait)
[personal profile] sawyl
A couple of weeks ago, on a bit of whim, I bought myself a Raspberry Pi as a bit of a toy. Having done this I promptly noticed a minor snag: my my total lack of USB input devices and the absence of HDMI ports on any of my monitors.

After a bit of thought, I realised that I was being a fool and that I could use my long-honed command line skills to set up a headless box in no time at all. Thus, I grabbed a copy of Raspbian, dd'd it to my SD card, booted the Pi off the network, used ssh to install a VNC server, installed and configured Avahi to register the VNC with OS X, and used the Screen Sharing App on my iMac as my VNC client.

  1. Flash the SD card with Raspbian
  2. Connect the Pi to the network and boot it up
  3. Find the IP address assigned to the Pi — I queried my router's list of connected devices
  4. Connect to the default account: ssh pi@<ip address>
  5. Change the default password immediately: passwd
  6. Update everything:
    1. sudo apt-get update
    2. sudo apt-get upgrade
    3. sudo apt-get dist-upgrade
  7. Set defaults like the timezone: sudo raspi-config
  8. Change the Pi to use a static IP address - this is unnecessary if using zero-configuration to publish addresses of services
    1. Edit the network configuration file: sudo vi /etc/network/interfaces
    2. Change the line "iface eth0 inet dhcp" to iface eth0 inet static
    3. Add your choice of address: address <ip address>
    4. Set the netmask to match the address: netmask <netmask>
    5. Set the gateway to your router gateway <gateway address>
    6. Save the changes and quit the editor
  9. Install the VNC and zero-configuration software:
    1. sudo apt-get install tightvncserver
    2. sudo apt-get install avahi-daemon
  10. Add the following definition files to the /etc/avahi/services directory:
    1. sudo vi /etc/avahi/services/multiple.service
      <?xml version="1.0" standalone='no'?>
      <!DOCTYPE service-group SYSTEM "avahi-service.dtd">
      <service-group>
              <name replace-wildcards="yes">%h</name>
              <service>
                      <type>_device-info._tcp</type>
                      <port>0</port>
              </service>
              <service>
                      <type>_ssh._tcp</type>
                      <port>22</port>
              </service>
      </service-group>
      
    2. sudo vi /etc/avahi/services/rfb.service
      <?xml version="1.0" standalone='no'?>
      <!DOCTYPE service-group SYSTEM "avahi-service.dtd">
      <service-group>
        <name replace-wildcards="yes">%h</name>
        <service>
          <type>_rfb._tcp</type>
          <port>5901</port>
        </service>
      </service-group>
      
  11. Next create a script to automatically start VNC for the pi user — it's easy enough to create a new user account and use this instead — by running: sudo vi /etc/init.d/tightvncserver
    #!/bin/bash
    ### BEGIN INIT INFO
    # Provides:          tightvncserver
    # 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 for a user
    # Description:       Allow remote VNC access for a user
    ### END INIT INFO
    
    VNCUSER=pi
    SERVERID=1
    
    . /lib/lsb/init-functions
    
    server_start() {
    
        CMD="/usr/bin/tightvncserver :$SERVERID"
    
        if [[ `id -un` = $VNCUSER ]]; then
    	$CMD > /dev/null 2>&1
        elif [[ `id -u` = 0 ]]; then
    	/bin/su - $VNCUSER -c "$CMD > /dev/null 2>&1"
        else
    	/bin/false
        fi
    
    }
    
    server_stop() {
    
        pkill -U $VNCUSER "Xtightvnc"
    
    }
    
    server_status() {
    
        pgrep -U $VNCUSER "Xtightvnc" > /dev/null && \
    	echo "VNC is running for user $VNCUSER" || \
    	echo "VNC is not running for $VNCUSER"
    
    }
    
    case $1 in
    
        start)
    	log_daemon_msg "Starting VNC for user $VNCUSER" "tightvncserver"
            server_start
            log_end_msg $?
    	;;
    
        stop)
    	log_daemon_msg "Stopping VNC for user $VNCUSER" "tightvncserver"
            server_stop
            log_end_msg $?
    	;;
    
        restart)
    	$0 stop
    	$0 start
    	;;
    
        status)
    	server_status
    	;;
    
        *)
    	echo "usage: $0 {start|stop|restart|status}"
    	exit 1
    	;;
    esac
    
    exit 0
    
  12. Create the necessary boot links for the script by running: sudo update-rc.d tightvncserver defaults
  13. Start the VNC server interactively to set a password and to ensure that all the necessary files exist: tightvncserver :1
  14. Reboot in the usual way: sudo shutdown -r now
  15. If everything has worked the system should come up again with its static IP address and should be offering a VNC session via zero-configuration

On OS X, connections and zero-configuration can be tested using the "Go > Connect to Server..." menu in Finder and "Shell > New Remote Remote Connection" in Terminal

Easy!

Profile

sawyl: (Default)
sawyl

August 2018

S M T W T F S
   123 4
5 6 7 8910 11
12131415161718
192021222324 25
262728293031 

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 31st, 2025 12:48 am
Powered by Dreamwidth Studios