How to Give Your Raspberry Pi a Static IP Address

If you want to set up a server (i.e., media server, or web server, or Docker) on your Raspberry Pi system, setting a static IP address is very important. If the IP address changes, your Raspberry Pi server will be inaccessible, defeating the purpose of having a server. So, it is essential to configure a static IP address on your Raspberry Pi system if you are planning to run some kind of server software on it.This article shows you how to configure a static IP address on the Ethernet and Wi-Fi network interface of your Raspberry Pi system running the Raspberry Pi OS.

Things You Will Need

To follow this article, you will need the following:

1) Raspberry Pi 3 or Raspberry Pi 4

2) Micro-USB (Raspberry Pi 3) or USB Type-C (Raspberry Pi 4) power adapter

3) 16 GB or 32 GB microSD card with Raspberry Pi OS flashed

4) Network connectivity on the Raspberry Pi

5) Laptop or desktop computer for VNC remote desktop access or SSH access

NOTE: If you do not want to access your Raspberry Pi system remotely via SSH or VNC, you will need to connect a monitor, a keyboard, and a mouse to your Raspberry Pi system, as well. I will not need any of these, as I will be connecting to my Raspberry Pi system remotely via VNC or SSH. My setup is called the headless setup of Raspberry Pi.

If you need any assistance with flashing the Raspberry Pi OS image on the microSD card, check out the article How to Install and Use Raspberry Pi Imager at linuxhint.com.

If you are a Raspberry Pi beginner and you need assistance installing Raspberry Pi OS on your Raspberry Pi system, check out the article How to Install Raspberry Pi OS on Raspberry Pi 4 at linuxhint.com.

Also, if you need any assistance with the headless setup of Raspberry Pi, check out the article How to Install and Configure Raspberry Pi OS on Raspberry Pi 4 Without External Monitor at linuxhint.com.

Configuring Static IP Addresses Graphically

If you are running Raspberry Pi OS in a desktop environment, you can configure a static IP address for your desired network interface very easily from the graphical desktop environment.

Right-click (RMB) on the network icon and click Wireless & Wired Network Settings.

Make sure that interface is selected in the dropdown menu.

Click the empty dropdown menu, as marked in the screenshot below.

Select the network interface you want to configure.

eth0 – the wired ethernet network interface.

wlan0 – the wireless (Wi-Fi) network interface.

Suppose you want to configure the wlan0 Wi-Fi network interface of your Raspberry Pi system.

After selecting the network interface you want to configure, you should see the following window.

Type in your desired static IP address, gateway (Router) address, DNS server address, etc.

If you want, you can also type the IP address in CIDR notation, like 192.168.0.110/24. Here, 24 is the subnet mask length. 24 is equivalent to the subnet mask 255.255.255.0.

To add multiple DNS server addresses, simply separate them with a space, as shown in the screenshot below.

If you only know the IP address you want to set and nothing else about your home or office network, then selecting Automatically configure empty options will request the network configuration from a DHCP server, and will only configure the network information that you did not specify here.

If you know all the required network information, I recommend that you uncheck Automatically configure empty options, as this eliminates the need for a DHCP server on the network.

If you do not need IPv6, check the Disable IPv6 option.

Once you are done, click Apply.

Then, click Close to exit the Network Preferences window.

For the changes to take effect, reboot your Raspberry Pi system with the following command:

$ sudo reboot

Once your Raspberry Pi boots, your desired static IP address should be set on your desired network interface.

You can confirm this with the following command:

$ ip a

Configuring Static IP Addresses via Command-Line

If you are running the minimal version of the Raspberry Pi OS (without any graphical desktop environment) on your Raspberry Pi, then you will not have access to any graphical tools to configure static IP addresses, as shown in an earlier section of this article.

Worry not! From the command line, configuring a static IP address on the wired (eth0) or wireless (wlan0) network interface of your Raspberry Pi system is very easy. In this section, I will show you how to do just that.

First, open the dhcpcd.conf configuration file with the nano text editor as follows:

$ sudo nano /etc/dhcpcd.conf

To configure a static IP address for the wireless (Wi-Fi) network interface (wlan0), add the following lines at the end of the file.

interface wlan0
static ip_address=192.168.0.110/24
static routers=192.168.0.1
static domain_name_servers=8.8.8.8 4.4.4.4
static domain_search=
noipv6

NOTE: Make any necessary adjustments to the configuration (i.e., change IP address, change router/gateway address, change DNS servers) depending on your desired network configuration.

To configure a static IP address for the wired Ethernet network interface (eth0), add the following lines to the end of the file.

interface eth0
static ip_address=192.168.0.111/24
static routers=192.168.0.1
static domain_name_servers=8.8.8.8 4.4.4.4
static domain_search=
noipv6

NOTE: Make any necessary adjustments to the configuration (i.e., change IP address, change router/gateway address, change DNS servers) depending on your desired network configuration.

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the dhcpcd.conf configuration file.

For the network configuration changes to take effect, reboot your Raspberry Pi with the following command:

$ sudo reboot

Once your Raspberry Pi boots, your desired static IP address should be set on your desired network interface.

You can confirm this with the following command:

$ ip a

Conclusion

This article showed you how to set up a static IP address on the wired and wireless (Wi-Fi) network interface of your Raspberry Pi system running the Raspberry Pi OS. I have shown both the graphical method and the command-line method of configuring a static IP address in Raspberry Pi.



from Linux Hint https://ift.tt/3mwkM93

Post a Comment

0 Comments