Have you ever tried to log into your Raspberry Pi via SSH and were denied because the IP address couldn’t be found? Do you have to scan your network every time you connect to find your local IP address? If the IP address of your Raspberry Pi changes all the time, it’s because you’re using a dynamic IP. If you want your Raspberry Pi to have an IP address that doesn’t change automatically, assign it a static IP address. With a static IP, you can be sure that the same IP will work each and every time. In this article, I’ll show you how to configure a static IP address using a clean installation of Raspbian Jessie and Raspbian Jessie Lite, but this should work with newer versions of Raspbian Wheezy as well. However, since there are pros and cons to each type of IP, lets first talk about why you would want a static IP vs. a dynamic IP.
Static IP vs Dynamic IP
Dynamic IP’s are good to use if you are concerned about security. If a hacker gets access to your IP address, you will be less vulnerable to attack since your IP changes frequently. A dynamic IP can change every time you log in, or only at certain intervals. A program installed on your network router called the dynamic host configuration protocol (DHCP), automatically changes and assigns new dynamic IP addresses to computers on your network.
A static IP (as you could probably tell by the name) is one that doesn’t change. This makes them more reliable when using services that depend on a stable internet connection, like online gaming, VOIP, or remote access applications. With a static IP, you’ll be able to use the same IP address every time you connect to your Pi via SSH or remote desktop applications.
Setting up a Static IP on the Raspberry Pi
Before starting, make sure you have already set up and configured a way to access the command prompt. Check out our tutorials, How to Set Up WiFi on the Raspberry Pi and How to Set Up a Raspberry Pi Without a Monitor or Keyboard to see how to do that if you haven’t already.
In this tutorial we’ll set up static IP’s for both WiFi and ethernet connections. If you only need one or the other, just omit the code below for the connection you don’t need.
Find Out Your Network Information
The first step is to find out your default gateway IP. This is the local IP address of your network router. The computers on your network use it to communicate with the router and access the internet. If you already know what it is, just skip this step. If not, do continue…
Power up and log into your Raspberry Pi via WiFi or ethernet, then enter route -ne at the command prompt to see your network routing information:
Under the “Gateway” column, you can see your Default Gateway IP (10.0.0.1 in my case) for each interface (Iface) – ethernet (eth0) and WiFi (wlan0). Write down your default gateway IP, we will need it later.
Now we need to find out the IP addresses of your domain name servers. Your Pi sends the domain names you enter into your browser (i.e. www.google.com) to domain name servers, which then converts them to IP addresses (i.e. 22.214.171.124) that your Pi can use to directly access the website’s server. Enter cat /etc/resolv.conf at the command prompt to find the list of domain name servers your Pi uses:
Copy these IP addresses to a text editor on your PC or write them down for later.
Configuration of Network Settings
Now we’re ready to configure the network settings. By default the Pi is configured with a dynamic IP address. To give it a static IP, we need to add our desired static IP, our default gateway IP, and our static domain name servers IP addresses to the dhcpcd.conf file.
At the command prompt, enter sudo nano /etc/dhcpcd.conf to edit the dhcpcd.conf file:
Now, without changing anything else in the file, add this code to the end of the /etc/dhcpcd.conf file, replacing the IP addresses with your own IP addresses found above:
interface eth0 static ip_address=10.0.0.100 interface wlan0 static ip_address=10.0.0.99 static routers=10.0.0.1 static domain_name_servers=126.96.36.199 188.8.131.52 2001:558:feed::1 2001:558:feed::2
Replace the addresses in the code above with your own addresses:
- static ip_address= This is the static IP address you will use to SSH or remotely connect to your Pi. Take your default gateway IP (found in the steps above), and change the last number to any other number between 0 and 255.
- static routers= This is your default gateway IP address.
- static domain_name_servers= These are the IP’s we found in the /etc/resolv.conf file above. Seperate each IP with a single space.
For example, my default gateway IP address is 10.0.0.1. To get the static ip_address for my ethernet connection (eth0), I replaced the 1 with 100 to get 10.0.0.100. To get the static ip_address for my WiFi connection (wlan0), I replaced the 1 with 99 to get 10.0.0.99. These are the static IP’s I will use to log in to my Pi from now on.
The file should look like this (with your own IP addresses):
Once you’ve replaced the IP addresses in the example code with your own IP addresses, press Ctrl-X and Y to exit and save the /etc/dhcpcd.conf file. Now enter sudo reboot to reboot the Pi. Log in with your new static ethernet IP or static WiFi IP:
To check that everything is working correctly and we can access the internet, lets ping Google. Enter sudo ping www.google.com at the command prompt:
Press Ctrl-C to stop the pinging. If the connection is successful, you will see that packets have been sent and received. If your connection isn’t successful, you will get a “Network is unreachable” error:
You should probably test the connection by pinging Google with both eth0 and wlan0 (ethernet and WiFi) IP’s.
Watch the video version of the process here:
Another nice way to connect to your Pi is via a direct ethernet connection to your laptop or desktop. A direct connection is extremely fast and stable. If you connect to your Pi via SSH a lot, I would definitely recommend setting this up. Check out our article, How to Connect to a Raspberry Pi Directly with an Ethernet Cable to learn how.
Thanks for reading! Now that you have a static IP set up, your Pi’s connection to the internet will be a lot more reliable. Let me know if you have any questions about setting up your static IP in the comments, and I’ll do my best to help. And be sure to subscribe, we send a quick email out when we publish new articles.
Need an easy-to-use way to design circuits and layout PCBs?
Try EasyEDA, a free circuit design software that also offers low cost, high quality PCB manufacturing.