Ever had the misfortune that the IP address of your pi just changed? If you have you probably have to scan your network again with a scanning tool of your flavor.
If the IP address of you Raspberry Pi changes all the time, it’s because you’re using a dynamic IP address. Now if you want your Pi’s ip to remain the same you’ll need to assign a static IP address. With a static IP you’ll never have to worry again that the Pi’s IP will change.
We’ll show you 2 way’s to configure a static IP address in this tutorial using a clean install of the Raspberry Pi OS.
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.
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.
Static IP – Cmdline.txt
Before you fire up your pi you can change the cmdline.txt file in the boot drive.
As this file is available in the boot drive this is the way to go to set up a static ip address in windows for a headless pi without any hassle.
So before you unplug your new sd card from your PC, open up the cmdline.txt file from the /boot/ drive. At the end of the line, after ‘rootwait’ we put the ip address.
In the example above you’ll need to replace the x and the y with the needed ip.
This is when using a /24 of 255.255.255.0 subnet. when using another subnet we’ll have to add the subnet to.
the configuration is as follows: ip=”client-ip”:”server-ip”:”gw-ip”:”netmask”:”hostname”:”device”:”autoconf”.
The client-ip is the IP you want the pi to have, the server-ip is left empty as this is unused.
The gw-ip, or GateWay is most likely the IP of your router, in most cases this is 192.168.0.1 or 192.168.1.1. Unless you configured your network for include more than one ‘range’ the netmask is 255.255.255.0, this is a /24 network.
The hosname, device and autoconfig speeks for itself.
Static IP – DHCP
A second way of setting a static IP address on a pi is using its DHCP client. 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.
sudo nano /etc/dhcpcd.conf
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 informarion from your network.
interface eth0 static ip_address=192.168.xxx.yyy static routers=192.168.0.1 static domain_name_servers=126.96.36.199 188.8.131.52
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 192.168.0.1. To get the
static ip_address for my ethernet connection (eth0), I replaced the 1 with 200 to get 192.168.0.200.
If you want to set a static IP address for your wireless replace the eth0 with wlan0.
For example my config is the following:
# Inform the DHCP server of our hostname for DDNS. hostname # Use the hardware address of the interface for the Client ID. clientid # Persist interface configuration when dhcpcd exits. persistent # Rapid commit support. # Safe to enable by default because it requires the equivalent option set # on the server to actually work. option rapid_commit # A list of options to request from the DHCP server. option domain_name_servers, domain_name, domain_search, host_name option classless_static_routes # Respect the network MTU. This is applied to DHCP routes. option interface_mtu # Most distributions have NTP support. #option ntp_servers # A ServerID is required by RFC2131. require dhcp_server_identifier # Generate SLAAC address using the Hardware Address of the interface #slaac hwaddr # OR generate Stable Private IPv6 Addresses based from the DUID slaac private interface eth0 static ip_address=192.168.1.50/23 static routers=192.168.0.1 static domain_name_servers=192.168.1.11 192.168.1.12 184.108.40.206 fd51:42f8:caae:d92e::1
Once we filled in all the required info lets save and exit nano: Ctrl-O Y, Ctrl-X Y and reboot: