Introduction
In this post, it will be shown how it is set a static IP for the Raspberry.
Requirements
Panel PC:Â Â Â Â Â Â Panel PCÂ Â Â Â
Description
By default, the Raspberry Pi is automatically set to obtain an IP address from a wired or wireless network. Then why do we want a static IP if the Raspberry automatically sets already one? The answer is that we need to have the same exact IP, not an automatic one. The reason for being automatic makes that the IP can be different each time the network process starts. If it is automatic it is impossible to communicate to a certain IP address because it is changing each time it starts the network.Â
This static address is needed so that any traffic destined for the Raspberry will be able to find it on the network at any time. An analogy for this would be a telephone number, if you want to communicate with someone using the mobile phone, it is impossible if he is changing his number every day. This is why it has a static number, so this way it can be contacted at any time.
This automatic method of obtaining an IP address is called DHCP or Dynamic Host Configuration Protocol. It is sometimes referred to as a dynamic IP. Your router will normally distribute these IP addresses but it isn’t guaranteed that it will get the same IP address every time. This can cause problems if you are trying to connect to your Raspberry Pi remotely.
Implementation
Now we'll see how to set a static IP address
The first step is going to the terminal prompt and writing the following command:
cat /etc/network/interfaces
As it is been said, by default it gets automatically an IP address, this is configured on this interfaces file, in the following line:
iface eth0 inet dhcp
So this eth0 (Ethernet0) configuration needs to be changed in order to give a static IP. So it is needed to access that file and change the configurations to achieve it. This is done using the following command:
sudo nano /etc/network/interfaces
This will open this document in the same terminal. Now it is time to remove the line that sets the IP address automatically for a static IP address. So:
Remove the line that reads
iface eth0 inet dhcp
And add the following lines
auto eth0 iface eth0 inet static address 192.168.1.25 netmask 255.255.255.0 gateway 192.168.1.1
Finally, you need to save the file by pressing CTRL+X and selecting Y to save the changes.
With this, we have configured the eth0 to have a static IP.Â
If we now use the command to see our interfaces we will see that the eth0 has now the IP we have assigned to. This is done using the command:
ifconfig
Now the Raspberry has an exact "Telephone Number" (IP) to be communicated with it at any time. To apply these changes it must be done a reboot to the Raspberry.Â
Â
See also
2nd step to create a TCP client on a Touchberry Pi 3. Creating a TCP Client using Node.js
1st step to create a TCP client on a Touchberry Pi 3. Setting an static IP Address