Assign a fixed IP address to your Raspberry Pi can be very useful if you have many materials (computers, accessories, home automation, ESP Easy DIY projects, home automation box…) on its local network.
For this tutorial, I used a Raspberry Pi 3 under Raspbian, but the procedure is the same regardless of the Linux distribution.
- 1 Assign a fixed IP address to the Raspberry Pi
- 2 Access the Raspberry Pi using its domain name
Assign a fixed IP address to the Raspberry Pi
Open a Terminal under Raspbian or connect in SSH to your Raspberry and then open the configuration of the interfaces file.
the configuration file looks like this
# interfaces(5) file used by ifup(8) and ifdown(8) # Please note that this file is written to be used with dhcpcd # For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf' # Include files from /etc/network/interfaces.d: source-directory /etc/network/interfaces.d auto lo iface lo inet loopback iface eth0 inet manual allow-hotplug wlan0 iface wlan0 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf allow-hotplug wlan1 iface wlan1 inet manual wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
Assign a fixed IP to the interface wlan0 (wireless)
for this tutorial, we’re going to change the configuration of the interface (wlan0) WiFi. Modify the file by adjusting the following configuration to your needs
iface wlan0 inet static address 192.168.1.23 netmask 255.255.255.0 gateway 192.168.1.1 wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf iface default inet dhcp
Assign a fixed IP to the interface eth0 (Ethernet)
you can do exactly the same thing for the Ethernet interface. Here is an example of configuration it will adapt to your configuration.
iface eth0 inet static address 192.168.1.24 netmask 255.255.255.0 gateway 192.168.1.1
Add a permanent road
If your IP is not on the same subnet, it is possible to add a permanent route with the option
up route add – net IP_RPI/24 gw IP_GATEWAY, giving for example:
- the Raspberry has an IP 192.168.2.2
- the gateway (gw) is the IP of an internet box or a router (by for example 192.168.1.1).
up route add -net 192.168.2.2/24 gw 192.168.1.1
For more information: http://www.linux-france.org/~mdecore/linux/doc/memo2/node24.html
This configuration works fine if you plug your Raspberry Pi on another computer that is sharing its internet connection. For example, I use my Mac Mini as internet bridge time to update distribution Raspbian for images too old home automation software that do not support the WiFi of the ft3.
Apply the new configuration
Save with Ctrl + X then O (or Y if you’re not in french).
to apply the new configuration, you can restart
or raise the layer network
sudo /etc/init.d/networking restart
Access the Raspberry Pi using its domain name
Raspbian assigns a domain name to the Raspberry. Default is raspberrypi. Not need to know the IP address of the Raspberry to access so we didn’t. For example, SSH simply enter
Similarly if you do home automation, you can access your Domoticz box from a browser by entering the URL
Change the default domain name
Now, we would like to be able to change the default domain name. Why not give the Raspberry name of the home automation for example box. It’s very simple, just change it in the configuration file host .
The configuration file looks like this
127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback ff02::1 ip6-allnodes ff02::2 ip6-allrouters 127.0.1.1 raspberrypi
Replace raspberrypi by domoticz for example or any other name (without space, special characters or accented characters). Save with Ctrl + X then Y.
Now open the file hastname and replace raspberrypi by the same domain as before.
Save and run this command to apply the changes
It remains more to restart the Raspberry
Once rebooted, try to connect by SSH. For example
Now you can access the Domoticz interface in a browser
Much easier to hold an IP right?