A new version of this series has been published. Please refer to the new index for updated articles and ordering. This article is kept for historical reference, but should be considered out of date.
Note: This article is part of a series. See the Index for more information.
Self-Promotion: I’ve recorded this series as a screencast for Pluralsight:
If you have a Pluralsight subscription, please consider watching it. Thanks!
Updates: Everything in this post seems to work the same with the new Jessie release of Raspbian.
Now that you’ve turned your inert Raspberry Pi into a working computer, it’s time to get moved in.
- A Raspberry Pi with the Raspbian Linux distribution installed as described in Part 2 of this series.
- You should be logged in as the “pi” user account.
Assigning a static IP address
Since this series is about turning the Raspberry Pi into a server, it’s important that it have a consistent IP address so that you can tell your router where to redirect traffic for things like web traffic or other services. There are two ways to accomplish this.
You can use your router’s configuration to assign a permanent “lease” to individual devices. In other words, you tell your router to always hand out the same IP address to a certain device when asked. There are definite advantages to letting your router handle the IP assignments. It centralizes the management of network resources in one place, rather than distributing that configuration across the various devices on the network. Also, there are certain UPnP functions that work better if the router is responsible for handling out the IP addresses.
This is the method I use, personally. Instructions for how to configure an individual router vary by manufacturer and model, so I won’t cover them in detail here, but the gist of it is that you assign an IP address to the hardware MAC address of the network card built into the Raspberry Pi.
To get the MAC address, type the following command:
The result should look something like this:
The MAC address appears on the first line, next to the label “HWaddr”. Follow the directions for your specific router, and assign a permanent IP lease to this MAC address. Make sure it’s not in the range that the DHCP server uses as it’s “pool”. For my router, everything from 100-255 is the DHCP pool, so anything below 100 is fair game for leasing or static assignment.
The second method is to configure the IP address directly on the Raspberry Pi itself. Here is where you will first start to see the difference in attitude from the Windows world to the Linux world. Windows is about convenience and ease of use for the average non-technical user. Linux is all about highly-knowledgeable users tweaking things exactly the way they want them. Linux settings are stored in plain text files in ordinary folders. The trick is mostly in knowing which settings go in which file, and where it lives.
Type the following command to edit the network configuration file.
sudo nano /etc/network/interfaces
“nano” is a text editor that is included in the Raspbian image, and is convenient for making configuration changes. You need “sudo” to run it with elevated privileges because regular users aren’t allowed to edit configuration files, and while it’s certainly above average, the “pi” user is just a regular user at the end of the day.
The default network interfaces will open in the editor, and should look like this.
There are a lot of magic words in this file, but the only part you need to be concerned with is the line that says “iface eth0 inet dhcp”.
Let’s break that down:
- “iface” means “interface”.
- “eth0” refers to the first (and only) ethernet port the Raspberry Pi has
(computers like to start counting things at 0)
- “inet” specifies TCP/IP networking, which is what this interface uses
- “dhcp” stands for “Dynamic Host Configuration Protocol”, which essentially means that someone else (your router) is responsible for assigning this computer’s IP address.
To assign a static IP address, you need to change “dhcp” to “static” and then add some lines that explicitly say what address should be used. Change the “iface” line to read as follows, substituting the highlighted values for your own network where appropriate.
iface eth0 inet static address 192.168.1.x netmask 255.255.255.0 gateway 192.168.1.1
If you don’t know what values to use for “netmask” and “gateway”, you can imitate what your primary computer is using. From a Windows command prompt on your primary computer, type “ipconfig”, and take the “Subnnet Mask” and “Default Gateway” values.
Hold down Ctrl, and press “x” to exit nano. It will prompt you to save your changes, press “y” to save them, and accept the default filename, which is the same as the file you loaded to begin with.
Before these changes will take effect, you’ll need to reboot the Raspberry Pi. Type the following and press enter to restart the machine.
Note: If you had just typed “reboot” without the “sudo” you would have been denied. Regular users are not allowed to reboot a Linux machine.
Once the machine has rebooted, type “ifconfig” to see the current interface configuration. The configuration for eth0 should have the IP address you assigned listed as the first item on the second row.
You are now at a point where you could disconnect the Raspberry Pi from its monitor and keyboard and do everything remotely. Actually, you could have done this a few steps ago, but it’s easier to connect when you know the IP address.
Find an SSH client program that you like, and use it to connect to the raspberry Pi. The particulars of configuring a connection will vary by program, but in Putty, all I need to do is provide the IP address of the computer I want to connect to and click “Open”.
At this point, you could disconnect the Raspberry Pi from your monitor and keyboard, hide it in a closet somewhere, and do the rest of your configuration remotely. I’d recommend leaving it hooked up, though, at least for now. You’ll be setting up a remote desktop a little later on, and it’s nice to have the visual feedback on the monitor to let you know things are working.
Update all the things
Now that the Raspberry Pi is situated on the network, it might be a good time to check for any updates to software that came as part of the Raspbian image.
Raspbian uses a program called “Advanced Packaging Tool” (APT) to install software packages by downloading them over the internet from well-known public repositories. Packages are aware of their own dependencies, so that installing a package will automatically install any other packages that it requires.
Type the following commands to update any currently-installed packages to their latest versions.
sudo apt-get update sudo apt-get upgrade
Let’s break that down:
- “sudo” runs with elevated privileges, which are required when installing or updating packages.
- “apt-get” is just one program in the apt suite, and the one you’ll use the most. It installs or updates software packages.
- “update” tells apt-get to update its list of available packages from the repositories it knows about.
- “upgrade” tells apt-get to upgrade currently-installed packages to their latest versions
The upgrade step will summarize what it’s going to change, and prompt you to confirm that this is what you want.
You can simply hit enter at this prompt, since “yes” is the default answer as indicated by the “Y” being capitalized in the prompt, and the “n” being lower-case. You’ll see this pattern a lot in Linux prompts.
New versions of the listed software packages will be downloaded and installed, which might take a while, depending on how many of them have been updated since the Raspbian image was last published.
When this is finished, type the following to update the Raspberry Pi’s own firmware and Linux kernel to the latest versions:
Note: I’ve seen some tutorials that do this in the opposite order, updating the firmware and then the packages. Here’s the thing, though. Look at the screenshot above, near the bottom. In this example, apt-get upgraded rpi-update itself. I’d rather make sure I have the latest rpi-update before running it.
You’ll get a progress report as the update proceeds, which should look like the this:
Reboot one more time to complete the update.
When this is complete, you will have a completely up-to-date Raspberry Pi.
You’ve reached another milestone, and I strongly recommend politely shutting down the Raspberry Pi (sudo shutdown –h now), and taking another backup image of the SD card, especially if the update/upgrade cycle took a long time. If you want to start over later, restoring from this image will skip over those updates, and save you some time.
You’ll probably want to repeat the update/upgrade/rpi-update cycle every now and then as part of regular maintenance. Packages get updated all the time, so you need to stay up to date.
In the next post, we’ll add web administration capabilities.