Set static IP on Raspberry Pi

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.

DID YOU KNOW?
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
Set static IP on Raspberry Pi
Set static IP on Raspberry Pi
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Raspberry Pi
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – 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:

  1. 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
    
    Finding the IP address, default gateway, and DNS server of the Raspberry Pi
    Finding the IP address, default gateway, and DNS server of the Raspberry Pi

    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.



  2. Next, edit the following file with root permissions. We will use nano but feel free to use a different editor of your choice:
    $ sudo nano /etc/dhcpcd.conf
    
  3. 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 eth0 above. 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.

  4. 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
    
  5. 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

  1. 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.
    Right clicking on the Raspberry Pi task bar to access networking settings
    Right clicking on the Raspberry Pi task bar to access networking settings
  2. 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.

    Filling out static IP address information in Raspberry Pi's GUI
    Filling out static IP address information in Raspberry Pi’s GUI



Closing Thoughts

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.



Comments and Discussions
Linux Forum