Setting up Static IP on Raspberry Pi

November 22, 2022

Internet Protocol (IP) addresses are used to identify a variety of devices that connect to the internet like computers or other network devices<a href=”#anchor1″>[1]</a>. An IP address is a 32-bit number, and it is a number you might see when setting up your home’s router. It’s a series of numbers separated by four periods.  The purpose of an IP address is so that a device can be identified by other devices that are also connected to the internet. The devices can communicate with one another and send information as long as the devices are properly identified with IP addresses.  One of the main reasons to use a static IP address is to make sure that specific computers can be found easily on your network. If you host a file server, you can reduce the chance of connection error by assigning a static IP address to the device.  Another benefit of using a static IP address is that you can create easy to remember IP addresses for various devices on your network. 

An IP address is a long string of numbers assigned to every device connected to a network that uses Internet Protocol as the medium for communication. it is the equivalent of the mailing address associated with your home or workplace.  The way Internet Protocol works is that information is transmitted over the network in discrete chunks called packets.  Each packet is mostly made up of whatever data the sender is trying to communicate, but also includes a header, consisting of metadata about that packet.  Other data stored in the packet header are the IP address of the device that sent the packet and the IP address of the device where the packet is heading.  Routers and other network infrastructure use this information to make sure the packets get to where they’re supposed to go.

Nobody types IP addresses into a browser search field. We use domain names like Network World, CNN or Twitter. The Domain Name System, or DNS, another part of the Internet protocol suite, makes sure that requests made using domain names reach the correct IP address.  DNS represents a user-friendly layer on top of the IP-address infrastructure. However, the IP address remains the fundamental way that internet-connected devices are found, and in some circumstances a domain name can correspond to multiple servers with different IP addresses.

IPv4 addresses are written in four parts separated by dots like this: 192.168.1.1. Each part written in conventional Base 10 numerals represents an eight-bit binary number from 0 to 255. Each of these four numbers separated by dots is written in standard decimal notation. But computers deal with numbers in binary using zeroes and ones, and each of the numbers in an IPv4 address represents an 8-bit binary number, which means that none of them can be higher than 255 or 111111 in binary. IPv4 addresses are 32-bit numbers, and the total number of possible addresses of that length is about 4.3 billion<a href=”#anchor2″>[2]</a>. That number that was in the early days of the internet, but started becoming a problem as internet-connected devices multiplied.

A static IP address is one that’s been assigned to a device and is guaranteed to remain constant. If your computer’s address is 192.168.1.101, it will stay that way as long as you want it to. Static IP addresses are important for devices that need to be easily found on the internet, like web servers. An ISP may charge a customer extra for an assigned static IP address. From the ISP’s point of view, they have a limited number of IPv4 addresses to hand out.  The downside of leasing a static address for the ISP is that the address is unavailable to anyone else.  Most of the ISP’s clients need an address  only when they’re actually accessing the internet. For those clients, ISPs assign dynamic IP addresses, basically handing out a new address to a device every time it connects to the network, and putting that address back into a pool of available addresses when the device disconnects. This helps conserve IP addresses. If an ISP has a million customers but only half are online at any given time, the ISP doesn’t need a million addresses in its pool.

For IPv4 networks, the process of assigning IP addresses dynamically is governed by the Dynamic Host Configuration Protocol, (DHCP), which, among other things, automates most of the process and ensures that no two devices are assigned the same address at the same time<a href=”#anchor3″>[3]</a>. IPv6 was designed to support stateless IP address autoconfiguration (SLAAC)<a href=”#anchor4″>[4]</a>, in which a device itself essentially grabs an address from the available pool when it connects to the network. However, there’s also DHCPv6, an updated version of the DHCP protocol that keeps more control in the hands of network providers.

We have been talking about IP addresses and running out of them as if there were one set of addresses for the entire planet.  That’s not really true. In fact, it’s not true for most devices that you use on a day-to-day basis and not all of the 4.3 billion IPv4 addresses are available to publicly connected devices.  A typical network connects to the public internet via a router, and it’s the router that is assigned an IP address by the ISP. From the perspective of the outside world, all traffic from devices on that local network are coming from that public IP address.  But inside the network, each device including the router has a local private IP address, usually assigned by the router via DHCP.  These addresses are private because they’re only used for directing packets within the local, private network, and can’t be seen by anyone outside the network. As a result, the same IP address can be used on an infinite number of private networks without causing confusion.  There are blocks of IP addresses specifically set aside for use on these private networks, addresses starting with 192.168 are quite common.

The job of the router is to alter the origin and destination IP addresses in each packet’s headers as needed as it passes between the private network and the public internet, a process known as network address translation, or NAT.  There are several methods for doing this. One way is to associate each device on the internal network with a network port that is listed in the packet header. That port information determines the final destinations of incoming packets that have all been addressed to the public-facing IP address assigned to the router.  This is specific to IPv4 addresses, and the explosion in local networks has been a big factor in preventing a total IPv4 address drought even as network-connected devices multiply in every home.  IPv6 addresses, on the other hand, are so plentiful that it’s assumed that these kinds of private networks will be unnecessary after universal IPv6 adoption. However, if you want to set up a private internal IPv6 network that connects to the internet via IPv4, there are also private IPv6 address ranges you can use.

IP addresses are hierarchical. In general, the numbers to the left tell you what network the device with that IP address is on, whereas the numbers to the right identify the specific device. However, the Internet Protocol doesn’t define where the dividing line is.  In addition, some of the bits in an address may be used to identify a subnetwork, or subnet.  Routers determine what parts of an IP address refer to networks, subnets, and devices by use of a subnet mask. In IPv4, a network mask is an 8-bit number like a standard IP address, though with all of its ones on the left and all of its zeroes on the right.  The dividing line between the ones and zeroes defines the divide within an IP address in the address space the subnet mask refers to.  IPv6 subnetting is much simpler, and mostly involves just slicing digits off of addresses to denote a range of subnetted addresses.  Keep in mind that subnet information isn’t contained in packet headers or known by the source and destination devices. Instead, it is used by routers and other infrastructure that use the IP addresses to determine how to deliver packets to the right devices on the right physical networks. Through subnetting,  a network admin could take a sequential block of IP addresses and distribute them across three separate physical subnetworks. The packet doesn’t need to know about those subnetworks. the router will use its lookup tables to figure out where to send the packet when the time comes.

By default in Raspberry Pi OS, which is a Linux-based operating system, your Raspberry Pi’s IP address is reconfigured automatically each time you reboot it, so it may change.  This is not ideal when you need a reliable address at which to connect to the Raspberry Pi from another device, such as when using it as a server. So it’s better to set a static IP address on Raspberry Pi.  As discussed earlier,  a public IP address is used to identify your local network on the wider internet. This may change every time your router connects to the internet, although you may be able to make it static depending on your internet service provider.  It is only required if you intend to connect to a device from outside your network, which we won’t cover here.  Instead, we are looking at the private IP addresses used to identify each device on your own local network. While it may be possible to reserve a certain address for your Raspberry Pi in your wireless router’s settings for the same purpose, here we’ll be showing you how to set a static IP from the Raspberry Pi itself.

For many projects with the small computer, it’s either useful or necessary to provide Raspberry Pi with a static IP address. But before we talk more specifically about the use of such a static IP address with Raspberry Pi, we’ll first outline the differences between addressing a computer in a private (local) network or on the publicly accessible internet. Private and public IP addresses are not to be confused with each other.  For many projects with the small computer, it’s useful or necessary to provide Raspberry Pi with a static IP address. But before we talk more specifically about the use of such a static IP address with Raspberry Pi, we’ll first outline the differences between addressing a computer in a private (local) network or on the publicly accessible internet. Private and public IP addresses are not to be confused with each other.

To be able to reach Raspberry Pi on the same address in your own LAN, you have to provide it with a static, private IP address. A static private IP address is essential for your Raspberry Pi if you want to set it up as a server in the LAN. If the Raspberry Pi server is also to continue being available outside of the local network, then you have to assign it another static address via which the server can be accessed on the internet. For example, an internet connection with a static public IP address or a DDNS service.  If the Raspberry Pi is used as a server accessible over the internet, then the public IP address of your internet connection comes into play. Most internet access is available via dynamic IP addresses that are changed every time they start and at the latest every 24 hours. This changes the public IP address with which the Raspberry Pi server can be reached.  As soon as the server receives a new IP address, it can only be traced in the LAN.  The solution is the application of dynamic DNS (DDNS). This links your dynamic public IP address with a domain name. Every time, as soon as your IP address changes, a program redirects the new address to the domain name and makes it permanently available on the internet. Now if you link a server on Raspberry Pi with the domain name, it’s permanently accessible online.  There are both free and paid DDNS services. Before you decide on one for yourself, you should first test which DDNS providers your router supports, and whether it supports any at all. Find the DDNS support tutorial for whichever individual router your DDNS server is intended to operate on.

Many routers support the ability to provide individual devices within the local network with a static IP address. Routers allows you to manage multiple devices and handle all of the router assignments remotely. Various other routers also support similar functions, and so can be used for linking Raspberry Pi with a static IP address.  A static IP address for Raspberry Pi is set up somewhat differently for each router but follows similar principles.  Usually, you open the user interface of the router in your browser.  Link the MAC address of Raspberry Pi with your LAN’s IPv4 address via the manual IP configuration. Most of the time, a checkbox exists for this in the router interface. This enables you to always automatically use the IP address assigned to you.

The current Raspbian operating system has a DHCP client daemon (DHCPCD) that can communicate with the DHCP servers from routers. The configuration file of a DHCP client daemon allows you to change the private IP address of a computer and set it up in the long term. The following instructions will assign a static IPv4 address with 32 bits, not to be confused with an IPv6 address, which has 128 bits available, to the Raspberry Pi.  Before you begin assigning a private IP address for Raspberry Pi, check whether DHCPCD is already activated and if not,  activate it:

<pre>

  <code>

    sudo service dhcpcd status

    sudo service dhcpcd start

    sudo systemctl enable dhcpcd

  </code>

</pre>

Now make sure that the configuration of the file <b>/etc/network/interfaces</b> has the original status. For this, the <b>iface</b> configuration needs to be set at <b>manual</b> for the interfaces.  For the editing of the activated DHCPCDs, start by opening the configuration file <b>/etc/dhcpcd.conf</b> and running the following command:

<pre>

  <code>

    sudo nano /etc/dhcpcd.conf

  </code>

</pre>

You’ll now set the configuration of the static IP address. If your Raspberry Pi is connected to the internet via an Ethernet or network cable, then enter the command <b>interface eth0</b>.  If it is connected via Wi-Fi, then use the <b>interface wlan</b> command.

To assign an IP address to Raspberry Pi, use the command <b>static ip_address=</b> followed by the desired IPv4 address and the suffix <b>/24</b> (an abbreviation of the subnet mask 255.255.255.0). For example, if you want to link a computer with the IPv4 address 192.168.1.100, then you need to use the command <b>static ip_address=192.168.1.100/24</b>. Make sure that the address used here is not yet used anywhere else and make sure it isn’t in the range of available IP’s in the address pool of a DHCP server.

You still then need to specify the address of your gateway and domain name server (usually both are the router). Raspberry Pi turns to the gateway address if an IP address to which it wants to send something is outside of the subnet mask.  In the example, this would mean outside of the range 192.168.1). In the following command, the IPv4 address 192.168.1.1 is used as an example as both the gateway and DNS server. The complete command looks like this in our example (where a network cable is used for the internet connection):

<pre>

  <code>

    interface eth0

    static ip_address=192.168.1.100/24

    static routers=192.168.1.1

    static domain_name_servers=192.168.1.1

  </code>

</pre>

The command lines above match the IPv4 addresses that you want to use for your Raspberry Pi, or where your router is assigned. Save the changes and close the file. Restart to activate the newly assigned static IP address in the network.  

Now use a ping command to check whether the Raspberry Pi is accessible in the network with its new IP address.  If the connection of the IP address was successful, you’ll see that you can reach it under the new IP address with a ping.

Static IP addresses for Raspberry Pi are sometimes necessary.  There are basically two different IP addresses that are relevant for Raspberry Pi (and projects using it): the private IP address of the Raspberry Pi within the local network, and the public IP address of its internet connection.  A static, private IP address is primarily necessary if you want to use Raspberry Pi as a server. But if you access the minicomputer via SSH more frequently, you should assign a static address in the same way.  Assigning a fixed public address that allows your Raspberry Pi to be accessed via the internet is somewhat more complicated. This is necessary, for example, when attempting to make your server installed on Raspberry Pi remain constantly available online. Most internet access is available only via a dynamic public IP address. Since a static address isn’t offered by all internet providers, a DDNS service presents the best solutions. But for this, you have to know which forms of dynamic DNS are supported by your router.

<p id=”anchor1″>[1]<a href=”https://en.wikipedia.org/wiki/Internet_Protocol” target=”_blank”>  https://en.wikipedia.org/wiki/Internet_Protocol</a>    “Internet Protocol”</p>

<p id=”anchor2″>[2]<a href=”https://en.wikipedia.org/wiki/IPv4″ target=”_blank”>  https://en.wikipedia.org/wiki/IPv4</a>    “IPV4″</p>

<p id=”anchor3″>[3]<a href=”https://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol” target=”_blank”>  https://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol</a>    “DHCP”</p>

<p id=”anchor4″>[4]<a href=”https://en.wikipedia.org/wiki/IPv6″ target=”_blank”>  https://en.wikipedia.org/wiki/IPv6</a>    “IPV6″</p>