Home » Linux » Raspberry Pi » Raspberry Pi Static Ip

Setting a Static IP Address on a Raspberry Pi [With Screenshots]

If you followed our article on how to SSH to your Raspberry Pi so that you can control it over a network, you might be tired of having to run the commands to find out what its current IP address is on your network.

Most networks assign IP addresses dynamically, which means each device on the network is assigned an IP address from a pool of available IP addresses. The address for a specific device may change over time if it is rebooted or the address is automatically reassigned for some reason. This is called DHCP – Dynamic Host Configuration Protocol.

Giving your Raspberry Pistatic IP address on your network means that it will always have the same address for you to connect to.

This article assumes you are running Raspberry Pi OS.

Step 1 – SSH to Your Raspberry Pi (or Attach a Screen & Keyboard)

If you don’t have a screen and keyboard attached to your Raspberry Pi, you will need to SSH to get things set up.

It’s highly recommended that you set a static IP address using a screen and keyboard just in case you make a mistake – you may not be able to reconnect if the network configuration isn’t valid.

Step 2 – Find Out Which Interface is Connected

The ip command can tell us all we need to know about the current state of the network on your Raspberry Pi.

Run the following in your terminal to see what interfaces are connected:

ip addr show

Which will output something that looks like this:

ip addr show
ip addr show

Here you can see the connected network interfaces. Each entry in the above output represents an interface – each entry taking the format

number: name: details

The interface named lo is the LOOPBACK interface, used for the computer to communicate with itself. It’ll always be there on most systems.

Below is the only other connected network interface wlan0, the wireless network connection on this Raspberry Pi. We can tell it is connected as it says UP in the block of information next to it!

If the wired ethernet connection were connected rather than the wireless, you’d see eth0 instead of wlan0. It’s possible but unlikely on a default installation of Raspberry Pi OS that your network interfaces will have different names than those shown here.

Step 3 – Finding your Network and Gateway

You will probably want to assign your Raspberry Pi an IP address on the network it’s already connected to. To do so, we will need to know what network addresses we should use to talk to other devices already there.

Finding the Network

This current IP address assigned to your Raspberry Pi is visible in the previous step’s output.

inet 192.168.1.75/24

The IP address following inet will tell you the current IP. Yours will be different but will be for a local IP address range.

To find out more about IP addresses, check out our article on IP Addresses and Subnets.

Finding the Gateway

For your Raspberry Pi to talk to the outside world, it will also need to know the address of your gateway – your internet modem/router. Run the following to do so:

ip r show

This will output the current network routes configured on your system:

ip r show
ip r show

We are interested in the default route on the first line. This tells us that by default, traffic is routed via 192.167.1.254 – now we know the gateway’s address.

Finding Your DNS Servers

You will also need to know what DNS servers you are currently using (if any) if you wish to access the internet. These can be found by running:

cat /etc/resolv.conf

Which will output the contents of the resolv.conf file as generated by your system.

cat /etc/resolv.conf
cat /etc/resolv.conf

Here you can see the default nameserver assigned is your router – 192.168.1.254.

Step 4 – Finding an Available IP Address

This step is dependent on the brand and configuration of the router you have.

When DHCP assigns an IP address automatically, your router will choose one from a range of IP addresses defined in its configuration. We should not assign the Raspberry Pi a static address that falls into that range, as otherwise, it may conflict if another device receives that address automatically.

You’ll have to check your router configuration or user manual to find out what this range is so that you can make sure you avoid using an address from it.

Step 5 – Assigning the IP Address to the Connected Interface

My network has a DHCP range of 192.168.100 to 192.168.200, from which addresses will be automatically assigned, so I will assign my Raspberry Pi the address:

192.168.1.201

This is to make sure that it doesn’t conflict with an existing IP (or the router’s IP address, which is 192.168.1.254).

Assigning a Static IP Address Temporarily

To assign a static IP address until the next reboot, just run:

sudo ip address add 192.168.1.201/24 dev wlan0

Replacing 192.168.1.201 with the IP address you wish to assign and wlan0 with the interface’s name to assign it to.

To find out more about IP addresses and the format they are displayed above, check out our article on IP Addresses and Subnets.

Assigning a Static IP Address Permanently

The network configuration will need to be edited to assign an address permanently. Edit the relevant configuration file by running:

sudo nano /etc/dhcpcd.conf

The configuration file(s) which you will need to edit will differ depending on your Linux distribution – this method is tested on Raspberry Pi OS in its default state

Add the following lines to the end of the file:

# Static IP Address
interface wlan0
static ip_address=192.168.1.201/24
static routers=192.168.1.254
static domain_name_servers=192.168.1.254 8.8.8.8 

Note that:

  • Lines beginning with # are comments and are not read as configuration entries
  • interface is the name of the connected interface and tells the configuration that the configuration lines following it will be applied to that interface – for this example, it is wlan0
  • static ip_address is the static IP address you wish to assign to this interface – 192.168.1.201 in this case
  • static routers is the IP address of the gateway/router on your network. 192.168.1.254 is the IP address of the router for this example.
  • static domain_name_servers are the DNS servers we wish to use for this network. As seen above, mine is 192.168.1.254 as my router acts as the DNS server. I’ve added a second DNS server separated from the first by a single space. 8.8.8.8 is Google’s DNS server, which will act as a second/fallback DNS server.

To apply these changes, save the document by pressing CTRL + X and pressing Y to save. Then, reboot.

sudo reboot

Your Raspberry Pi now has a Static IP!

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

1 thought on “Setting a Static IP Address on a Raspberry Pi [With Screenshots]”

  1. I gave this a shot on ubuntu server 20.04, adapting it a bit where needed, and I just couldn’t get it to stick between restarts. Ended up using netplan with success so far.

    Either way, great guide! I had fun tinkering, and I’ll back back in a few weeks when I grab a pi.

    Reply

Leave a Comment