The Raspberry Pi is a tiny, affordable computer that is great for learning programming and electronics. By default, most Raspberry Pi devices connect to the network via DHCP, which means they get assigned a dynamic IP address that can change. For servers and other use cases, having a static IP address that never changes is desirable.

In this comprehensive guide, I‘ll walk through how to set up a static IP on both the Ethernet and Wi-Fi interfaces of the Raspberry Pi running Raspberry Pi OS (formerly Raspbian).

Prerequisites

Before we get started with the static IP configuration, let‘s go over some prerequisites:

  • A Raspberry Pi – any model will work
  • Raspberry Pi OS installed on an SD card
  • Access to the terminal on the Pi either via monitor & keyboard or SSH
  • Know the details of your network like the router IP, DNS servers, etc.

If you need help with any of the above, check out my Raspberry Pi setup guide.

Now let‘s get to setting that static IP!

Static IP on Ethernet

Setting a static IP on the Ethernet port of the Raspberry Pi is done by modifying a configuration file called dhcpcd.conf.

  1. First, use the sudo nano /etc/dhcpcd.conf command to open up the file in the nano text editor.

  2. Go to the bottom of the file and add the following:

interface eth0
static ip_address=192.168.1.100/24  
static routers=192.168.1.1
static domain_name_servers=192.168.1.1 1.1.1.1

Let‘s break down what each line means:

  • interface eth0 – Configures the eth0 Ethernet interface
  • static ip_address – Specifies the static IP address and subnet mask
  • static routers – Sets the router/gateway IP address
  • static domain_name_servers – Lists DNS servers to use
  1. Save the file by pressing Ctrl+X then Y and finally Enter.

  2. Reboot your Pi with sudo reboot for the changes to take effect.

Once your Pi starts back up, the Ethernet interface should now have the static IP you specified! Verify it with ip addr show eth0.

Static IP on Wi-Fi

The process for setting a static IP on Wi-Fi is very similar to Ethernet.

  1. Again edit sudo nano /etc/dhcpcd.conf

  2. Add the following block for your Wi-Fi details:

interface wlan0    
static ip_address=192.168.1.75/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1 1.1.1.1

As you can see, the only difference is specifying wlan0 instead of eth0.

  1. Save and reboot once more.

Your Raspberry Pi should now have consistent static IPs on both networks! Verify with ip addr show wlan0 after rebooting.

Changing the Hostname

While hostnames don‘t matter much for home usage, setting a custom hostname can make things easier to manage if you have multiple Pis on the network.

To change the hostname:

  1. Edit the /etc/hostname file with sudo nano /etc/hostname
  2. Change the content to your desired hostname, like mypi
  3. Save and reboot

Use the hostname command to double check it changed.

Testing Connectivity

At this point we have set custom static IPs and hostnames, but do our network settings actually work? Let‘s test it out.

On another system on the same network, try pinging and SSHing with the new IPs and hostnames we configured.

ping mypi
ssh pi@192.168.1.75

If you get a response, then our static networking is working perfectly!

Troubleshooting Issues

If you encounter issues getting connected or with the Internet not working properly, here are some things to check:

  • Verify the correct IP & subnet – Make sure the IP and subnet mask match your network segment.
  • Check router config – Some routers block access to certain ports by default. Verify SSH/HTTP ports open.
  • Ping gateway – Try pinging the router/gateway IP from the Pi after assigning the static IP. If that fails, connectivity is broken somewhere.
  • Flush DNS cache – Sometimes a stale DNS cache can cause issues. Run sudo systemd-resolve --flush-caches
  • Reboot router – As a last resort, reboot your router in case there are issues in its config.

Following the standard networking troubleshooting steps methodically can help track down most connectivity issues with your new Raspberry Pi static IP setup.

Final Thoughts

Getting a static IP configured on a Raspberry Pi may sound intimidating, but as you can see it‘s actually quite straightforward by modifying the dhcpcd.conf file.

With your Pi now having consistent IP addresses, you open up projects around web servers, VPN access, remote management, and more. No more dealing with a dynamic address that changes constantly!

Some additional ideas for building on a static IP:

  • Host a Raspberry Pi web server with Apache or Nginx
  • Set up OpenVPN for secure remote access
  • Configure dynamic DNS for easy remote access
  • Attach peripherals like sensors that can be monitored

I hope this guide has demystified the process of assigning static IPs on the Raspberry Pi. Let me know if you have any other networking questions!

Similar Posts