How to configure a static IP address on a Raspberry Pi? Step-by-step guide

Last update: 29/08/2022
How to configure a static IP address on a Raspberry Pi? Step-by-step guide

Many projects that run on a Raspberry Pi board require a fixed IP or static as they are also known, so it is necessary to be able to configure it to ensure optimal performance.

Mainly The IP addresses of these devices are dynamic.but in many cases this is needed be fixed and not changeespecially when trying to access the same board from different devices.

It is important to note that to achieve this, it is necessary to apply it to the Raspberry Pi's private IP address, to the computer that locates it and the public IP address of the networkTherefore, here we show you how to carry out this procedure.

What are the benefits and uses of having a static IP address on your Raspberry Pi single-board computer?

All devices that use a network are identified by a unique IP address on the network, That is how, through that IP address The devices can be connected easily. These are usually of two types: dynamic or static, the dynamics, as the name suggests They will change every time the device restarts.While the statics They remain fixed despite the restarts.

In this manner static IPs They are much more reliable in cases where services that depend on a stable internet connectionThat is why they are generally used for projects of web servers, VPN servers, for video game consoles, VoIP or for applications of remote desktop on Raspberry PiThanks to these IPs, you'll be able to use the same address every time you connect to your network. Raspberry Pi.

When this isn't configured, and therefore you're left with your Dynamic IP, You're simply going to have problems every time you try to access your files or turn it on. the lights in your house or connect to your serverbecause the IP address has changed and you will not be able to establish any communication with your board.

Therefore, being able to count on a static IP address This will allow you to have services that are directed specifically to the IP address. Therefore, it's an essential part if you want to convert your Raspberry Pi as a web server or VPNTo help you understand this a little better, these IP addresses are used by the Internet sites, mail servers, public FTP servers, web page servers, DNS serversbecause they constantly need to be connected to the Internet and this will allow them to be located on the network.

What should I consider before assigning a static IP address to my Raspberry Pi?

Taking into account everything mentioned above in the post, you now need to know what you need to know before assign a static IP address to your single-board computerPlease note that this procedure must be performed with great attention and care, as a single mistake will cause everything to go wrong. Therefore, the first thing you are going to do is connect to your network to know the values ​​you have to enter beyond the IP address.

Once you have successfully connected and verified that you have internet access, you will need to run “ip addr” in the terminal to obtain the data displayed on the screen:

What should I consider before assigning a static IP address to my Raspberry Pi single-board computer?

  • As you can see in the image above, the network interfaces of the board are visible, and if you can see some connected to the network, some values ​​of that connection are shown. In this case, the ones that really matter are those of wth0 and wlan0In the case of Eth0, it is the network cable and wlan0 of the WiFi network. While the value of inet It will indicate the IP address that is currently assigned to it, followed by /24.
  • After this, you only need to know what the gateway and DNS to have everything correct and thus avoid problems when assigning assigning a static IP address to the Raspberry Pi. Therefore, to find out what the gateway is, it will be necessary to run the command ip router show In the terminal.

What should I consider before assigning a static IP address to my Raspberry Pi single-board computer?

  • It is also necessary know the DNS valueThis value will be obtained by consulting the contents of the file /etc/resolv.confThe easiest way to access the content is running the command cat/etc/resolv.conf In the terminal.

What should I consider before assigning a static IP address to my Raspberry Pi single-board computer?

From there you can take the DNS where it says nameserver. This is important if you want to browse the internet, so take this opportunity to configure everything. After this, you'll have all the necessary information, knowing the IP address of the device. Raspberry Pi You'll be able to get an idea of ​​which one you can assign without causing any problems. In these cases, it's always best to use high numbers.But being careful not to reach the limit.

That is why it is recommended to only change the last number of the IP address On the other hand. Keep in mind that this number must always be less than 254, since The maximum is 255 and 254 are reserved. In this case, the one assigned interface eth0 the IP 192.168.1.139Therefore, a new IP address will be assigned; in this case it will be: 192.168.1.250As you can see, the only number that is modified is the last one, and by a much higher number to avoid problems.

All other data should be left as is. In this case, it will be established as Set a fixed IP address for the eth0 interface Since it's the one with the network cable, it will offer better performance and fewer problems when establishing communication with the Raspberry Pi. Now, if only the board is connected via WiFi, then it is necessary to select the wlan0 interfaceAfter this, everything will be ready to begin this procedure.

Learn step by step how to set up a static IP address on your Raspberry Pi mini computer from scratch

Once you have everything ready to carry out the configuration of a With a static IP address on your mini-computer, the next thing you need to do is begin the procedure.But first you should know that there are several ways to do this.

That's why we're going to show you the different ways you can configure your static IP address on your Raspberry Pi so you can carry out different types of projects:

Redirect to a static IP address

In the case of these microcomputers that are used to carry out different types of projects, it is necessary to have a fixed IP, since it will be needed when wanting turn your motherboard into a server or any other project that requires a static IP address.

In the case of redirect to a static IP addressIt is important to mention that there is a difference between redirecting the computer to a public network and a local private network. Therefore, it is essential not to confuse what they are private IP addresses with public ones, But to help you understand all this better, we'll explain each of these redirects.

Redirect a LAN with a private IP address

In the local networks the router It is primarily responsible for distributing data between different devices, but it must also handle the allocation of IP addresses, specifically from DHCP server integrated into it. In the case of DHCP It is responsible for automatically assigning IP addresses to each of the corresponding devices. While devices such as... Raspberry Pi, mobile devices or Smart TV They obtain an IP address with which they can communicate with other devices.

All units will be uniquely identified through the MAC address. These private IP addresses They are known as such because in a private network IPs are assigned in a individual with DHCP and that pairing devices with the same IP address will only be allowed within your local network, but depending on the DHCP This private IP address can be changed for each device. This way, in order to Access Raspberry Pi In a local network that always has the same address, it is necessary to provide it with a fixed private IP address.

Fixed addresses They are used for remote equipment maintenance with the SSH protocol. If you have the SSH programYou'll be able to use it to remotely control your Raspberry Pi from any computer. This will eliminate the need to connect a separate monitor and input device. However, if the motherboard only has one private dynamic IP address, it will have to be searched for and accessed each time a connection is desired. SSH clientWhereas with private fixed lines this is done directly.

In light of all this, it's very important to have a static, private IP address if you want to use your microcomputer as a server within your local network. Furthermore, if you're creating a project where you require that your Raspberry Pi To make it easily accessible outside the local network, you must ensure you assign it another static IP address so it is accessible from the internet. For this, it is recommended to use public static IP addresses or DDNS servers.

Access your microcomputer on the Internet using public IPs or DDNS

If what you need is for you Raspberry to be accessible as a server on the Internet, then you will need a public IP address of your Internet connectionIt's important to keep in mind that almost all internet connections have a dynamic IP address, which changes every time the computer is restarted, and most often changes every 24hConsequently, the public IP address also changes, which is the one used to access the board.

This means that if you wish to use the Raspberry Pi As a server, this will cause you problems, because when the server receives a new IP address You can only access it from within the local network. If you are outside the local network, you cannot know the address used to access your server. Therefore, the best and most practical solution is to have a static IP address, The easiest way to do this is to use an internet connection with a public static IP address.

However, it's worth mentioning that this isn't an easy task, as these services are usually expensive and not all internet providers offer them. As an alternative, you can use a Dynamic DNS (DDNS)), this will allow the public dynamic IP address is linked to a domain nameSo every time the address changes, the program directs the new address to the domain name, and in this way it can be made permanently available on the Internet.

This means that if the server on Raspberry Pi It connects via the domain nameTherefore, it will always be available online. Many solutions can currently be found. Free and paid DDNS, But keep in mind that before purchasing one, you should ensure that the DDNS providers are compatible with your router.

Assign a private static IP address

Most routers usually allow within the local network that can be assigned a assign a fixed IP address to individual devices, That is why they are very appropriate when it comes to assigning a static IP address to Raspberry Pi. In the case of configuring a static IP address for Raspberry Pi, it will vary depending on the router you have, although it will almost always be the same.

The first thing to do is access the router's user interface from your preferred browser and there you will manually configure it to link the MAC address of Raspberry Pi with a IPv4 address from your local network. There you will find a checkbox which you should select. Selecting it will activate the assigned IP address so that it can be used automatically.

Associate static IP with DHCPCD

In the main operating systems for Raspbian they are currently Raspbian Jessie and Jessie Lite, which have a domain that can be used as a DHCP client (DHCPCD) to communicate the router with the DHCP server.

Therefore, with the DHCP client domain configuration file, it will be possible change the computer's private IP address and thus establish it as fixed for an extended period of time. With this in mind, here we will explain how you can assign it to the Raspberry Pi a fixed 32-bit IPv4 addressTo do this, you only need to follow each of the following steps. Before starting this procedure... Assigning a static IP address to Raspberry PiYou'll have to check if DHCPCD sis found activated.

To do this, you will need to add the following command to the terminal:

  • Sudo service dhcpcd status

If for some reason it is not activated, then you will have to activate it as follows:

  • Sudo service dhcpcd start
  • Sudo systemctl enable dhcpcd

Next, you must ensure that the file configuration /etc/network/interfaces to be in its original state. For this, the configuration interface can be changed from “iface” to “manual”. Once this is enabled, you can then begin configuring DHCPCD.

To do this, you must add the following command to the /etc/dhcpcd.conf configuration file:

  • Sudo nano /etc/dhcpcd.conf

Once you reach this point, everything will be ready to begin configuring the static IP address. If your microcomputer is connected to the internet via network cable or Ethernet, you will need to run the command with “interface eth0”. Now, if your motherboard is using a wireless network, then you should use the command to interface wlan0. Now, in order to assign the IP address to the board, you will need to use the command static ip_address=” followed by the desired IPv4 address and clearly the suffix “/24”.

This is the abbreviation for subnet mask 255.255.255.0. If for some reason you want to link the computer with the IPv4 address 192.168.0.4, The command you will need to use is the following: static ip_address=192.168.0.4/24It is important to make sure this address not being used for any other purpose.

Next, you'll need to specify your gateway port address and the domain name server address, which is usually your router. In the case of mini pcs They are used as a gateway address when you want to send something to an IP address that is outside the subnet mask. The following command we will show you uses it. IPv4 address 192.168.0.0 as a gateway and DNS server.

Taking the network cable for the Internet connection as an example, this command would look like this:

interface eth0 static ip_address=192.168.0.4/24 static routers=192.168.0.1 static domain_name_servers=192.168.0.1

Here you will be able to adapt this command line to IPv4 addresses that you use in your router or that you want to use on the board. Then you must save the changes with “Ctrl + O” and press the key Enter. Then press “Ctrl + X” to close the configuration file.

If for any reason some of the devices are restarted, the newly assigned addresses will remain valid:

  • sudo reboot

This way, with a ping command you can check if the Raspberry Pi is available on the network using its new address:

  • ping raspberrypi.local

If the combination of The IP has been successfulThen you will be able to access the new address via a ping.

From the terminal

Other methods that can be used for this are from the terminalSince it is sufficient to accomplish this, you will only need to follow each of the steps that we will explain below.

To assign a static IP address to the Raspberry Pi, you only need to modify the file /etc/dhcpcp.conf with the following command:

  • sudo nano /etc/dhcpcd.conf

The following line must be added to the end of this file; this will depend on the data obtained in the first step. In the interface line You will have to write the interface name to which it is to be assigned static IPIf it is a network cable, the interface will be eth0 And if the connection is via WiFi, then the interface will be wlan0.

Next, you will need to add a line called static ip_address=, with the IP address you want to assign to the Raspberry Pi ending in /24Now, on the next line, write static routers=192.168.1.1 where 192.168.1.1 This will be the gateway IP address. Now, on the last line, we will place static domain:name_servers= And the DNS servers you have will always be separated by a space.

In this case, they would look like this:

interface eth0 static ip_address=192.168.1.250/24 static routers=192.168.1.1 static domain_name_servers=212.231.6.7 46.6.113.34

From the terminal

Lastly, it is necessary Restart the Raspberry Pi Pi with sudo reboot To finalize all of this, the microcomputer's static IP address is now ready for when you want to connect from another computer. From this point on, you can start installing things to do from another computer, such as... Torrent server or web server.

From the desk

Finally, we're going to show you how to do this from your desktop. Keep in mind that you must already have... static IP that you want to use in your Raspberry Pi.

Next, carefully follow each of the steps that we will show you:

  • The first thing to do is go to the top right of your screen, there you will find the icon of the "Grid". Depending on whether you are connected to the internet or not, you will see a different icon. Keep in mind that you must be connected to a network to perform this procedure. Right-click on the icon and select the option from the menu that appears. “Wireless & Wired Network Setting”.

From the desk

  • In the new window that appears on your screen, you will need to fill in each of the fields with the corresponding information. From the drop-down menu in the upper left corner of your screen, select "Interface". In the one at the top right you must mark eth0Now in the picture of the IPv4 You must enter the static IP address that you want to assign to the Raspberry Pi.
  • Now you must write the following into the router: Gateway IP address, and in DNS servers will be placed the DNS separated by a space, leaving everything else empty.
  • To finish, you need to click on "Apply" and then in "Close". Next, you should reboot the raspberry pi and check that everything is correct.

From the desk

  • After the microcomputer restarts, you will have to execute the ip addr on a terminal. If everything is correct, the assigned IP address will appear in the net value Next, you should test your internet browsing to verify that everything is working correctly. If everything is working correctly, then you've succeeded. have set a static or stable IP address on your Raspberry Pi, so you can start using it as an internet server.

From the desk

Ebooks of IPAP
Ebooks IPAP

🔥JOIN🔥 THE NEW IP@P COMMUNITY! SIGN UP HERE!

Themes

Author: Nely Zambrano

I have extensive experience in the technology field; my specialties are graphic design, social media, and digital marketing. Windows and Android are my areas of expertise.

Related