How to Use a Static IP Address and Setup SSH on a Raspberry Pi

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:

raspberry-pi-ifconfig

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:

raspberry-pi-route-n

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.

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

raspberry-pi-enable-ssh

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

5 comments

  1. Please use ip instead of ifconfig, route and arp, as these are obsoleted now. Instead use ‘ip address’, ‘ip route’ or ‘ip neighbour’.

    And to calculate ip-addresses and netmasks, use the commands ipcalc or sipcalc from corendponding named packages. They take SIDR-addresses or IP and netmask as argument and print out lots of usefill information.

  2. This is very helpful. How do you make a static address if you are on a wireless LAN? Is it the same process?

    • simon, it is exactly the same process, you only change the device you use. Use wlan0 instead of eth0.

      About installing ssh, it is easier to do that with aptitude instead of rasppi-config:

      $ sudo aptitude install ssh

      In the /etc/networl/interfaces I would indent the address, netmask and gateway lines by 8 spaces or one tab so it would be easier to read. Then it shows easier where they belong to.

      You would also want to add the package resolvconf and add these lines after gateway in the interfaces file:

      dns-nameservers 192.168.1.1
      dns-search foo.example.com

      The contents of /etc/resolv.conf should be used to fill in the values of these directives. If you have more than one DNS server, just add them with a space between their IP numbers.

  3. Hi this is a good discussion. I have a question that is not raspberry pi specific. I would like a suggestion for an alternative approach to what has already been discussed. Assuming any of the computers on this subnetwork knew the hostnames of all the other computers, then is there a way to discover the current DHCP address of a computer. Further is there a way to detect a computer leaving the subnet (i.e. it is reset or turned off) and then rejoining after it has reset or been turned on? Even further is there a way to discover a never before computer that has connected to the network by way of DHCP? I know that I should be able to write an application that uses a priori data that all computers know including a new one. This can’t be a new idea and I am hoping there is already a known solution.

Comments are closed.

Sponsored Stories