A static IP address does not change as opposed to a dynamic IP address. Your Raspberry PI always needs a static IP address if you want to access it remotely or if you want to set up a web server or media server. If the IP address of your Raspberry Pi server changes, it will be inaccessible.
A static IP address is especially very important when you plan to run any server software on your Raspberry Pi. This article will guide you on how to configure a static IP address on the network interface of your Raspberry Pi.
Configuration Prerequisites
- A Raspberry Pi 3 or Pi 4 single-board computer.
- A good quality microSD card, preferably with 32 GB capacity.
- Network connectivity
- A power supply adapter.
- A computer or laptop for remote access or access via SSH.
Assign a static IP address via command line with DHCP client daemon (DHCPCD)
I recommend trying the configuration on a fresh install of the latest release of Raspberry OS like the Raspbian Jessie or Jessie Lite. The latest Raspian operating systems have a DHCP client daemon (DHCPCD) to communicate with DHCP servers. The DHCP daemon configuration file allows you to change the private IP address of a computer. The steps below will assign a static IPv4 address to the Raspberry Pi.
Step 1: Check whether DHCPCD is activated using the following command:
sudo service dhcpcd status
If DHCPCD is not activated, you can activate it using the following command:
sudo service dhcpcd start
sudo systemctl enable dhcpcd
Step 2: Editing the dhcpd.conf file.
To edit the activated DHCPCDS, open the configuration file /etc/dhcpcd.conf from your favorite command-line editor by running the following command in the terminal window.
sudo nano /etc/dhcpcd.conf
When the file is open, you can now configure the static IP address. If your Raspberry Pi is connected to the Internet over Wi-Fi, you will use the “interface wlan” command. If the Internet connection is via a network cable or Ethernet, then the command to use will be “interface eth0”.
Interface eth0
interface eth0 static ip_address= static routers= static domain_name_servers
Interface wlan0
interface wlan0 static ip_address= static routers= static domain_name_servers=
Description
interface: Defines which network interface you are applying the configuration for. The interface can be either eth0 for an Ethernet network connection or wlan0 for a Wi-Fi network connection.
static ip_address: Defines the IP address you want to set your Raspberry Pi device to.
static routers: Defines your gateway IP address, i.e., IP address or your router.
static domain_name_servers: Defines your DNS IP address. This configuration accepts multiple IP addresses, and you can add them separated with a single space.
Example 1: Ethernet (eth0) Connection
The following is a complete command of the configuration where Ethernet cable is used for the Internet connection.
interface eth0 static ip_address=192.168.0.7/24. static routers=192.168.0.1. static domain_name_servers=192.168.0.1
Assign the desired IPv4 address under static ip_address followed by the suffix ‘/24/’, an abbreviation of the subnet mask 255.255.255.0. For example, if you want your static IPv4 address to be 192.168.0.7, use the command static ip_address=192.168.0.7/24.
Your next step is to specify your domain name server and your gateway. They both refer to your router IP address. The gateway and domain name server is useful when your Raspberry Pi wants to connect to an address outside of the subnet mask. In our example above, this means a connection outside of the range 192.168.0. In our example above, the IPv4 address 192.168.0.1 is used as both the DNS server and the gateway.
Example 2: Wi-Fi connection (wlan0)
interface wlan0 static ip_address=192.168.0.200/24. static routers=192.168.0.1. static domain_name_servers=192.168.0.1
Assign the desired IPv4 address under static ip_address followed by the suffix ‘/24/’. For example, if you want your static IPv4 address (wlan0) to be 192.168.0.200, then use the command static ip_address=192.168.0.200/24.
Your next step is to specify your domain name server and your gateway. In our wlan0 example above, the IPv4 address 192.168.0.1 is used as both the DNS server and the gateway.
Step 3: Save your changes.
When you have confirmed your configurations, you can save your changes with ‘Ctrl + O” and press the enter key.
To exit the editor, press Ctrl+x.
Step 4: Restart your system for the changes to take effect.
sudo reboot
Step 5: Check your IP address using the ifconfig command.
ifconfig
You can also check whether the Raspberry Pi is accessible in the network using the ping command.
ping raspberrypi.local
If your configuration is successful, you will see that you can reach your new IP address using the ping command.
Recap
A static IP address is necessary if you want to configure your Raspberry Pi as a server. If you also access your Raspberry Pi via SSH very often, then you should assign a static address using the same simple procedure. This guide showed you how to configure a static IP address on the Ethernet network and Wi-Fi network interface using the command-line with the DHCP client daemon.
Moreover, suppose you are running your Raspberry Pi operating system in a desktop environment. In that case, you can easily configure a static IP address for either eth0 or wlan0 network interface from the graphical desktop environment. You can access the wireless & wired network settings in the right top corner to input your configuration. The graphical network settings will allow you to manually or automatically configure the network settings if you know the IP address you want to use for your Raspberry Pi.
Remember, you will need to reboot your Raspberry Pi system for the changes to take effect whether you configured your static IP address using the command-line or through the wireless & wired network settings in the graphical desktop environment.