Give a fixed local IP to your Raspberry Pi.
ⓘ This article may have been partially or fully translated using automatic tools. We apologize for any errors this may cause.
As you may know, each device connected to your box has a local IP address which allows it to be identified within your network.
However, these IPs can change each time the machine is restarted, and it is very annoying when you want to access the Raspberry from a computer, for example in SSH.
In this tutorial we will see how to give a fixed local IP to our Raspberry Pi and thus avoid looking for it each time.
What do we need ?
We will only need a working Raspberry Pi connected to the internet, with Raspbian installed.
Please note, we are talking about a local IP address, used to access the Raspberry from your network and not directly from the internet. We will be doing a tutorial on this specific topic in the coming weeks.
Assign a static IP to the Raspberry Pi
First connect to your pi, either physically or by SSH, and open a terminal there.
We will start by finding the current local IP address of your Raspberry Pi. To do this, type one of the following two commands (adapt according to your case):
#Si la Raspberry Pi est connectée à votre box en ethernet ip route | grep eth0 #Sinon si la pi est connectée en wifi ip route | grep wlan0
You should get a return that looks something like this:
default via 192.168.0.1 dev wlan0 src 192.168.0.101 metric 303 192.168.0.0/24 dev wlan0 proto dhcp scope link src 192.168.0.101 metric 303
Note this return to the side so that you can return to it easily.
Once done, open the file
nano (or some other text editor, whatever), go to the end and add the lines below, replacing:
eth0if you are connected via Ethernet.
192.168.0.101/24by the following address
srcin the previous command, followed by
/XXafter the first IP.
192.168.0.1by the following address
interface wlan0 static ip_address=192.168.0.101/24 static routers=192.168.0.1
What does it all do? Well quite simply, in our example, it tells the Raspberry Pi that we want:
- Whether its IP address for the Wi-Fi connection in the local network is static, that is to say always the same, and whether
192.168.0.101, with the subnet mask
- That it must use the machine at address as a router
192.168.0.1, that is to say your box.
For information, you can quite choose another address than the following one
src, the only constraints are to stay in the correct subnet mask (
/24 means to modify only the last group) and to choose an address which is not already in use by another machine. We
have chosen to use the current address of the Raspberry Pi since we are sure that no other machine has it. uses.
All you have to do is restart your Raspberry Pi to apply the change and check that it still has internet access.
After each restart, the IP of your Raspberry Pi will always remain the one you have defined.