Assign a fixed IP address, a domain name to a Raspberry Pi [Exclusive Guide]

11:30 am October 26, 20168010

assign a fixed IP address to my Raspberry Pi can be very useful if you have many materials (computers, accessories, home automation, projects DIY ESP Easy , box automation…) 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.

open a Terminal under Raspbian or connect in SSH to your Raspberry and then open the configuration of the interfaces file.

 sudo nano/etc/network/interfaces 

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 /etc/wpa_supplicant/wpa_supplicant.conf iface default inet dhcp wpa-conf 

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

 sudo reboot 

or raise the layer network

 sudo /etc/init.d/networking restart 

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

 ssh [email protected] 

Similarly if you do home automation, you can access your Domoticz box from a browser by entering the URL

 raspberrypi.local:8080 

raspberrypi.local domoticz

Cool right!

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 .

 sudo nano/etc/hosts 

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 O (or Y).

now open the file hastname and replace raspberrypi by the same domain as before. [

 sudo nano/etc/hostname 

save and run this command to apply the changes

 sudo etc/init.d/hostname.sh

it remains more to restart the Raspberry

 sudo reboot 

once rebooted, try to connect by SSH. For example

 ssh [email protected] ssh domoticz.local 

or access the Domoticz interface in a browser


domoticz.local domoticz

much easier to hold an IP right?