How to assign a fixed IP address and a domain name to a Raspberry Pi

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.

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.

 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 
  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

 sudo reboot

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

 ssh pi@raspberrypi.local

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 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 pi@domoticz.local 

ssh domoticz.local

Now you can access the Domoticz interface in a browser


domoticz.local domoticz

Much easier to hold an IP right?

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

1 Comment
  1. This is great and I can now easily access my rpi from my computer but what about if I am using my rpi as a hotspot. I want to be able to type examplename.com rather than 192.168.42.1 to go to the website that is hosted on the rpi. its good if I connect to it through ethernet but the address does not work when connecting to the rpi via wifi(rpi as hostspot). I can type 192.168.42.1 though and get to the website.

Leave a Reply

DIY Projects
%d bloggers like this: