Setting a static IP address for your Raspberry Pi will ensure that the device can always be accessible at the same IP address on your network, since it will never change. This is ideal for a Raspberry Pi being used to host a service such as a gaming server, web server, etc. It makes it more convenient when applying network configurations, since you know that the Raspberry Pi can always be found at the same, predictable IP address.
The opposite of a static IP address would be a dynamic IP address. A dynamic IP address is handed out from the DHCP server (typically the router in most home networks), and has a tendency to change frequently. If you ever tried to check the IP address on your Raspberry Pi before, and noticed that it changes after system reboot, then you have the device configured to receive a dynamic IP address.
In this tutorial, you will see how to set a static IP address on a Raspberry Pi. This can be done via both command line and desktop GUI on the Raspberry Pi OS, without installing any extra software. Follow along with our step by step instructions below to get started.
You can also configure your DHCP server (or router) to assign your Raspberry Pi device a static IP address. This means that your system would still use DHCP, but the server or router will reserve the same IP for the MAC address of your Raspberry Pi’s network interface. Instructions for this will vary, depending on your network environment and DHCP server.
In this tutorial you will learn:
- How to set a static IP address on Raspberry Pi via command line
- How to set a static IP address on Raspberry Pi via desktop GUI
|Category||Requirements, Conventions or Software Version Used|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to set static IP on Raspberry Pi step by step instructions
We will cover two methods below, both command line and GUI. If you are currently remotely connected to your Raspberry Pi, such as with a VNC connection or an SSH connection, then you may lose the connection to your Raspberry Pi, at least temporarily, when configuring a new IP address. Therefore we recommend being physically in front of the Raspberry Pi when carrying out this type of configuration.
Set Static IP Address via Command Line
Follow the steps below to set a static IP address on your Raspberry Pi via commmand line:
- Before we get started with setting a static IP address, we should check our current network information to find our default gateway, DNS server, and what type of IP address and subnet our network uses. This information is normally handled automatically by the DHCP server or router, but we will need to manually specify it later. Here are the commmands we can use to retrieve all of the relevant information:
Find current IP address of Raspberry Pi:
$ hostname -I
Find the default gateway (the IP address of the router, on home networks):
$ ip r | grep default
Find the IP address of the DNS server:
$ cat /etc/resolv.conf
Take note of the information you retrieved using these commands. It will be useful later. As is the case in our screenshot above, it is common to have a default gateway and DNS server at the same IP address, though it may be different on your system.
- Next, edit the following file with root permissions. We will use
nanobut feel free to use a different editor of your choice:
$ sudo nano /etc/dhcpcd.conf
- Scroll to the bottom of this file and add the following lines, replacing each value with that for your own configuration:
interface eth0 static ip_address=192.168.198.20/24 static routers=192.168.198.2 static domain_name_servers=192.168.198.2
We have used interface
eth0above. If, instead, you need to configure a static IP address for the wireless interface, then use
wlan0. We have chosen an IP address that is on the same /24 network as the router / default gateway so that the Raspberry Pi will be able to access it, and then filled out the default gateway and DNS info with the same that we retrieved earlier.
- After making your edits to the file, save changes and exit the file. Then reboot your Raspberry Pi for the changes to take effect:
$ sudo reboot
- When the device reboots, you can check to make sure the changes were successful:
$ hostname -I 192.168.198.20
Set Static IP Address via Desktop GUI
- Get started by right clicking on the network interfaces icon in the task bar at the top of your screen (the default location for the bar in Raspberry Pi OS). Then, click on Wired and Wireless Network Settings.
- In the Network Preferences window that shows up, select the interface you want to configure, then type your desired IP address, the IP address for your default gateway and DNS server, then click on Apply, followed by Close.
In this tutorial, we saw how to set a static IP address on a Raspberry Pi system. This included a command line method and GUI desktop method, depending on whether you use your device as command line only or usually have a monitor connected. It is always a good idea to be physically next to the Raspberry Pi when configuring network settings, since a slight mishap could lead to losing connection.