Raspberry Pi Zero Setup

The Raspberry Pi Zero (Zero) is awesome. It has a 1Ghz single-core CPU, 512MB RAM, and mini HDMI all in a USB flash drive-sized package. It can, however, be more difficult to get setup than other Raspberry Pis. This guide will give you a few tips to help get your Pi up and running more quickly. It also assumes that you already have Raspbian on the Micro SD card you intend to use in the Pi. If you need help getting Raspbian installed, you can find more information here.

Setting Up Network Connections

Unlike the other Pi models, the Zero does not have ethernet. This, combined with it’s lack of full-sized USB, makes initial setup tricky. My first instinct was to connect to the Zero via SSH. Afterall, I didn’t plan to use it with a GUI or plan on needing to interact with it directly anyway. That plan fell by the wayside as soon as I realized that the Zero would not be able to connect to the network at all, without my inputing the Wi-Fi password. In short, no SSH. Unfortunately, booting it up connected to my monitor did not work either, because I could not get the Pi to power my keyboard, even with an extra power supply plugged into the MicroUSB hub.

All was not lost though. Fortunately, you can mount the MicroSD card with Raspbian already installed on it to your PC, search its directories, and create and edit files. After mounting the MicroSD card, you will need to edit two files in order to connect to your Wi-Fi: /etc/network/interfaces and /etc/wpa_supplicant/wpa_supplicant.conf.

Editing /etc/network/interfaces

NOTE: As of Raspbian Stretch, you do not need to edit this file. Unless you are running an older version, skip to the next section.

Open /etc/network/interfaces with administrator privileges in your text editor of choice (e.g., sudo nano /etc/network/interfaces). Look for wlan0 in the file and edit the corresponding block of code so it looks like this:

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

Note that if you are planning to setup a static IP address for your Zero, you may want to change “dhcp” in the third line to “static.” I did not do this with my Zero, as I used my router to reserve an IP address for the Zero. If you do want to set a static IP on the Zero itself, you will need to make a few more changes as well. Prior to making those changes, you need to find some information about your network. First run this command in the terminal:

sudo ifconfig

You need to copy the following information, which should be on the same line: inet addr, Bcast and Mask.

Next run the following command:

netstat -nr

Copy the non-zero gateway and destination addresses you see.

With the above information, we can edit the interfaces file starting with the line that begins “iface wlan0.” Replace the text in brackets (including the brackets) with the information you copied down previously. Note that for the address line, you may not want to use the last three numbers you wrote down from the ifconfig output. You want to assign an address that your router will not use later. Therefore, it may be beneficial to pick a larger number, like 210 (e.g., if inet addr is 192.168.1.102, enter 192.168.1.210 into the address line).

iface wlan0 inet static
address <inetAddr>
netmask <Mask>
network <destinationAddress>
broadcast <Bcast>
gateway <gatewayAddress>

Make sure to save the changes you made prior to exiting your text editor. We are now finished editing this file.

Editing /etc/wpa_supplicant/wpa_supplicant.conf

To begin our next set of edits, open /etc/wpa_supplicant/wpa_supplicant.conf. As with our last file, open it with administrator privileges in a text editor. Add the following to the end of the file, making sure replace everything in the angle brackets with the correct information for your network:

network={
  ssid="<yourNetworkName>"
  psk="<yourNetworkPassword>"
  proto=RSN
  key_mgmt=WPA-PSK
  pairwise=CCMP
  auth_alg=OPEN
}
Enable SSH

Raspbian no longer has the SSH server enabled by default. Fortunately, enabling it is fairly straightforward. With the MicroSD card still in another computer navigate to the boot partition. Open the text editor of your choice to create an empty file named ssh (no file extension!) and save it. That’s it. Here is an example of how to accomplish this from the terminal.

touch <pathToBootPartition>/boot/ssh

Now you are ready to boot your Zero for the first time!

Replacing the Pi User Account

Leaving the default user account information unchanged on your Zero is not a good idea. Every Pi, not just the Zeros, have the same default username, Pi, and the same default password, raspberry. To say that leaving this information unchanged makes any Zero that accesses the Internet vulnerable is an understatement. Fortunately, you can create a new user and remove the default user with a few brief commands. If you haven’t done so yet, go ahead and connect to your Zero via SSH or directly, if you are planning to use it with its own monitor and keyboard.

Adding A New User

To add a new user, just enter the following commands, again replacing everything in the angle brackets:

sudo adduser <userName>

You will be greeted with a few prompts after executing the adduser command. Make sure that you make a strong password for this account. You can leave the rest of the prompts blank, if you would like.

Next, we will see what groups the user pi is a member of and add our newly created user to those same groups. The output of the first command lists all of the groups to which pi belongs. You will enter each of these groups separated by commas into the appropriate portion of the second command.

groups pi
sudo usermod -a -G <group1>,<group2>,...,<groupN> <userName>

You can now login as the new user. If you are logging into the Zero using SSH, and you use authentication keys, you will need to send the keys to the new user you created before you can connect to the new account via SSH. Go ahead and do what you need to do and login to the new user account now.

Deleting Pi User Account

This is pretty straight forward. CAVEAT: Before running this command, be sure to copy any files you may have stored in the home directory of the pi user account and want to keep, to the new user’s home folder. This command will not only delete the pi user but pi’s home folder as well! When you are ready, type the following command into the terminal:

sudo deluser -f pi

If you get a message saying that the pi user is being used by a process, run the following command before rerunning the previous command:

sudo pkill <processNumberFromCantDeleteMessage>

You are now ready to start exploring everything you can do with a Zero. If you are looking for some extra security when browsing the web at the coffee shop, you can learn more about OpenVPN here. If you have questions, or setup tips that I didn’t discuss here, please leave your feedback in the comments. Have fun!

comments powered by Disqus