Like most Linux systems, the Raspberry Pi allows you to connect to it over SSH and although it is possible to make a SSH connection to a Pi with a dynamically allocated IP address, it is much easier to configure the device with a static IP address. By default, the Raspberry Pi is set up to get its IP address dynamically using DHCP. This is great for many situations, however where the Pi is acting as a server (which it does when you connect over SSH) then having a fixed IP address means you always know the address of your Pi without having to log in and run ipconfig to obtain the current address.

The IP address is configured in the file /etc/network/interfaces and we will need to edit this file to change the configuration from dynamic to static. But before we do that, we need to discover what address range is being used on your network. A typical home network uses one of the following ranges of IP address: 192.168.1.1 – 192.168.1.255 or 10.1.1.1 to 10.1.1.255. There are other variations as in fact all the addresses between 192.168.0.0 – 192.168.255.255 and 10.0.0.0 – 10.255.255.255 are designated for use on private networks.

To discover your current IP address use the following command:

In the eth0 section, the second line displays the Internet address, the Broadcast address and the Netmask. Note down the Internet address and the Netmask for later on.

Most networks have a default gateway (often the router or modem from your Internet Service Provider) where all traffic is routed if it can’t be resolved locally. This is normally configured via DHCP when the IP address is assigned, but for a static address it needs to be defined manually. To discover the current default gateway type:

The default gateway is listed on the line with the G flag, in my case 192.168.1.1 which is actually the router from my ISP. Note down the default gateway for later on.

Before editing the /etc/network/interfaces file, you need to decide what IP address you want to use as the permanent address for your Pi. On your network the .1 or .254 addresses are probably taken by your router, modem or wireless access point. There will also be a range of addresses assigned for DHCP, normally by your router or modem. If you don’t know what that range is and you don’t know how to use your router’s web interface to check, then the easiest thing to do is pick an address far away from the dynamic one your Pi currently has. For example, my Raspberry Pi has an IP address of 192.168.1.164. Since there are a few PCs and the odd tablet in my house I could take a guess that the DHCP address range defined by my router starts at 192.168.1.160, which it does. Therefore a good IP address for my Pi would be somewhere far from that range, say 192.168.1.20. By picking an address away from the DHCP range and away from .1 or .254 then I am limiting the chances of there being an address clash. Of course, the correct way to do this is discover how your router or modem is configured and then plan your network accordingly, don’t ever tell a real network engineer that you did it by guessing!

If you want a better picture of your network, try installing the nmap package and use nmap -sn 192.168.1.* to find which devices are on your network. Where 192.168.1.* is the first three parts of the network address you are using plus an asterisk.

Once you have picked an IP address edit the /etc/network/interfaces file:

Find the line which reads iface eth0 inet dhcp and replace it with:

But put the address, netmask and gateway that you noted down previously. Press “Ctrl + X” to leave the nano editor and type “Y” to confirm that the file should be saved. Press ENTER to keep the current filename (i.e. /etc/network/interfaces). Now reboot.

SSH

Now that your Pi has a static IP address you can configure the SSH server. This is done via raspi-config.

From within raspi-config, select option 8 (Advanced Options) and then option A4 (SSH). When asked “Would you like the SSH server enabled or disabled”, select “Enable” and finally select “OK”.

You can now connect to your Pi using SSH. If you have another Linux machine, simply use the ssh command. For Windows try the popular PuTTY program or alternatively use the SSH extension for Chrome.

Image credit: Regular Raspberry Pi

Gary has been a technical writer, author and blogger since 2003. He is an expert in open source systems (including Linux), system administration, system security and networking protocols. He also knows several programming languages, as he was previously a software engineer for 10 years. He has a Bachelor of Science in business information systems from a UK University.

Our latest tutorials delivered straight to your inbox