Have you ever tried logging in to 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 address. If you want your 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 tutorial, I’ll show you how to configure a static IP address using a clean installation of Raspbian Jessie and Raspbian Jessie Lite. There are pros and cons to each type of IP though, so let’s first talk about why you would want a static IP over a dynamic IP.
Static IP vs. Dynamic IP
Dynamic IP’s are good to use if you’re concerned about security. If a hacker gets access to your IP address, you’ll 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 it more reliable when using services that depend on a stable internet connection, like online gaming, VOIP, or remote desktop applications. With a static IP, you’ll be able to use the same IP address every time you connect to your Pi.
Setting up a Static IP on the Raspberry Pi
Before starting, make sure you’ve 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). The “Iface” column lists the names for each connection – ethernet (eth0) and WiFi (wlan0). Write down your default gateway IP, we’ll need it in a minute.
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 convert the domain names to IP addresses (i.e. 126.96.36.199). Your Pi then uses the IP address to access the website’s server.
Enter cat /etc/resolv.conf at the command prompt to find the list of domain name servers:
Copy these IP addresses to a text editor on your PC or write them down for later.
Configure the Network Settings
Now we’re ready to configure the network settings. By default the Pi is configured with a dynamic IP address. To assign it a static IP address, you need to add your static IP, default gateway IP, and domain name servers 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 at the bottom of the dhcpcd.conf file, replacing the IP addresses with your own IP addresses found above:
interface eth0 static ip_address=10.0.0.100 static routers=10.0.0.1 static domain_name_servers=188.8.131.52 184.108.40.206 2001:558:feed::1 2001:558:feed::2 interface wlan0 static ip_address=10.0.0.99 static routers=10.0.0.1 static domain_name_servers=220.127.116.11 18.104.22.168 2001:558:feed::1 2001:558:feed::2
- static ip_address: This is the static IP address you’ll 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 resolv.conf file above. Separate 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. I’ll use these IPs 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 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 the Pi has access to the internet, let’s 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’ll see the packets that 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 ethernet and WiFi static IP’s.
You can watch me set this up step by step in this video:
Now that you have a static IP set up, your Pi’s connection to the internet will be a lot more reliable. But another really useful way to connect to your Pi is with 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! Let me know in the comments if you have any questions about setting up your static IP, and I’ll do my best to help. And be sure to subscribe! We send out a quick email each time we publish new articles.