Raspbian on Pi 2: Headless With No Keyboard or Mouse

Updated: 2/23/2016

Prerequisites:

To accomplish this install completely without a monitor, keyboard, or mouse, you need to be able to install Raspbian on the microSD card (you can’t use NOOBS), and you must be able to connect the Pi to your router with an ethernet cable and log onto your router to get the Pi’s IP address. Otherwise, you can still get to the desired state, but you may need to temporarily connect a monitor, keyboard, and mouse.

Download and Install Raspbian on the microSD card:

I downloaded Raspbian from here:

https://www.raspberrypi.org/downloads/raspbian/

To install the image onto the microSD card, I used the guide for Mac OS linked at the bottom of this page:

https://www.raspberrypi.org/documentation/installation/installing-images/README.md

You’ll, of course, want to use the guide appropriate for your operating system.

Connect the Pi To Your Router To Get the IP Address:

I inserted the microSD card with the Raspbian image, then I used an ethernet cable to connect the Pi to my home router. I plugged in the micro USB cable for power and then waited for the Pi to boot and get a DHCP IP address from the router. After a couple of minutes, I logged onto my router to find the IP address. I have Comcast Xfinity, so I used a web browser to go to http://10.0.0.1/ where I looked at the connected devices.  My Pi was listed, appropriately enough, as ‘raspberrypi’ and I found it was assigned an IP address of 10.0.0.34. This process will be different for you depending on your home router.

If you can’t log onto your router, you’ll likely have to temporarily connect the Pi to a TV, keyboard, and mouse to find the IP address. You’ll open a terminal window and type:

$ ifconfig

You’ll get a reading similar to:

pi@raspberrypi:~ $ ifconfig
eth0      Link encap:Ethernet  HWaddr b8:27:eb:b6:e3:d1
          net addr:10.0.0.34  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::9623:dfc0:2ffa:f179/64 Scope:Link
          inter addr: 2601:482:80:32fc:c97b:8b5:9688:fe55/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:10450 errors:0 dropped:10 overruns:0 frame:0
          TX packets:2188 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1756005 (1.6 MiB)  TX bytes:328808 (321.1 KiB)
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:200 errors:0 dropped:0 overruns:0 frame:0
          TX packets:200 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:16656 (16.2 KiB)  TX bytes:16656 (16.2 KiB)
wlan0     Link encap:Ethernet  HWaddr 74:da:38:68:fc:af  
          inet6 addr: fe80::99bb:f28d:be53:f538/64 Scope:Link
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:40 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

The IP address is eth0 section after net addr (highlighted in blue above).

If you can’t connect to the router with an ethernet cable, you’ll need to jump down to the section on configuring Wi-Fi, then come back here to get the IP address from the wlan0 section.

SSH Into the Pi:

Using the IP address of the Pi, I used SSH to access its command line interface. The default username is ‘pi’ and the password is ‘raspberry’. On Mac OS or Linux, just open a terminal and type:

$ ssh -l pi 10.0.0.34

Remember, though, to replace 10.0.0.34 with the IP address of your Pi.

Once you’re logged onto the Pi, you’ll see this:

FirstLogon

Expand the Filesystem:

Next I wanted to expand the filesystem to use all the space on my microSD card using raspi-config:

pi@raspberrypi:~ $ sudo raspi-config

you’ll see this:

raspi-config

Press the tab key to jump to <select> then press enter. The partition will be resized and you’ll get this message telling you that you need to reboot:

RebootAfterResize

Press enter, then tab twice to finish, and then press enter at the message asking if you want to reboot now. The Pi will reboot and you will be disconnected from the SSH session. Wait a couple of minutes for the reboot, then reconnect.

Apply Patches with apt-get:

Now, I wanted to upgrade Raspbian to apply all the security updates and bug fixes released since the ISO was packaged:

pi@raspberrypi:~ $ sudo apt-get update

I waited for apt-get to download and read all the package lists, then I typed:

$ sudo apt-get upgrade
Install tightvncserver:

Now that everything was updated, I wanted to get a graphical user interface. VNC is an open protocol for viewing and controlling a desktop on a remote PC. There are several different implementations of VNC; I chose to use tightvnc mainly because it’s included in the Raspbian repository and, therefore, trivial to install:

pi@raspberrypi:~ $ sudo apt-get install tightvncserver

After it’s installed, I started the server with this line:

pi@raspberrypi:~ $ vncserver :1 -geometry 1024x768 -depth 24

This starts a new X Windows session and sets it to session 1. The resolution of the original iPad Mini is 1024 x 768 and the iPad 2 Mini with Retina Display is 2,048 x 1,536. I have an iPad 2 Mini, but I set the resolution of the Pi desktop to 1024 x 768 because at the higher resolution, the scaling doesn’t work and you get a really big desktop with tiny icons and tiny menus. The depth is how many bits each pixel gets for color. My experience is that it doesn’t work if I set it at 32, so I set it at 24. I’m not sure why. Once I enter that the first time, I get a message that I need to set a password:

pi@raspberrypi:~ $ vncserver :1 -geometry 1024x768 -depth 24

You will require a password to access your desktops.

Password: 
Verify: 
Would you like to enter a view-only password (y/n)? n

New 'X' desktop is raspberrypi:1

Creating default startup script /home/pi/.vnc/xstartup
Starting applications specified in /home/pi/.vnc/xstartup
Log file is /home/pi/.vnc/raspberrypi:1.log

pi@raspberrypi:~ $

You can run the same command again assigning a different desktop with a different resolution:

pi@raspberrypi:~ $ vncserver :2 -geometry 1280x1024 -depth 24

New 'X' desktop is raspberrypi:2

Starting applications specified in /home/pi/.vnc/xstartup

Log file is /home/pi/.vnc/raspberrypi:2.log

pi@raspberrypi:~ $

Now I have 2 desktops running: one for my iPad and one at a higher resolution for when I want to work on it from my iMac. The desktop number is important because it determines the port number from which you access the desktop. If your desktop is 1, you access it at port 5901. If it’s 2, the port is 5902, etc. Pay attention to how your Viewer wants you to enter this port information. Some like the address as 10.0.0.34:5901 and some like it as 10.0.0.34:1 while others want two colons, like 10.0.0.34::5901

Get a Viewer for Your Device:

To access the desktops, you install a VNC viewer on your device.

This one is highly rated for Android on Google Play called bVNC (I’ve never actually used it) :

https://play.google.com/store/apps/details?id=com.iiordanov.freebVNC&hl=en

I haven’t used RealVNC on Android, but I’ve used it on Windows, Mac OS X and iOS; so it’s probably just as good on Android:

https://play.google.com/store/apps/details?id=com.realvnc.viewer.android&hl=en

On Windows, I use the TightVNC Viewer:

http://www.tightvnc.com/download.php

On Mac OS X you can use the built in Screen Sharing by either typing Screen Sharing into spotlight or, in the Finder, click Go->Connect to Server… and enter vnc://10.0.0.34:5901 into the Server Address field. Make sure you replace 10.0.0.34 with your address, and 5901 with your desktop port number.

I also like RealVNC Viewer on Mac OS X. I think it’s more responsive than Screen Sharing for some reason:

https://www.realvnc.com/download/viewer/

I also like the RealVNC Viewer for iOS:

https://itunes.apple.com/us/app/vnc-viewer/id352019548?mt=8

Configure Wi-Fi:

Once I got remote access to my Raspberry Pi desktop via Real VNC, I was ready to configure the Wi-Fi. I have an Edimax Wi-Fi USB Adaptor, so it was as easy as plugging in the dongle, then selecting my SSID from the networks menu in the upper right corner and entering my Wi-Fi password:

Screen Shot 2016-02-23 at 10.13.57 PM

So now when I open a terminal and run ifconfig, I get an IP address in the wlan0 section (highlighted in blue):

pi@raspberrypi:~ $ ifconfig
eth0      Link encap:Ethernet  HWaddr b8:27:eb:b6:e3:d1  
          inet addr:10.0.0.34  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: fe80::9623:dfc0:2ffa:f179/64 Scope:Link
          inet6 addr: 2601:482:80:32fc:c97b:8b5:9688:fe55/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:202422 errors:0 dropped:119 overruns:0 frame:0
          TX packets:79399 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:39902617 (38.0 MiB)  TX bytes:54065781 (51.5 MiB)
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:200 errors:0 dropped:0 overruns:0 frame:0
          TX packets:200 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:16656 (16.2 KiB)  TX bytes:16656 (16.2 KiB)
wlan0     Link encap:Ethernet  HWaddr 74:da:38:68:fc:af  
          inet addr:10.0.0.17  Bcast:10.0.0.255  Mask:255.255.255.0
          inet6 addr: 2601:482:80:32fc:94cc:9549:a4b2:4a30/64 
cope:Global
          inet6 addr: fe80::a265:d345:8eb1:ec58/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3666 errors:0 dropped:3875 overruns:0 frame:0
          TX packets:274 errors:0 dropped:13 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:1119357 (1.0 MiB)  TX bytes:100060 (97.7 KiB)
Make a DHCP Reservation for the Wi-Fi IP Address:

After I got this IP address, I logged onto my router again and made a DHCP reservation for it. That ensures the wlan0 IP address won’t change whenever I restart the Raspberry Pi.