Give a fixed IP to your Raspberry

You have been many (OK only 2 !) wondering how to set a static IP as Raspbien. So today we'll see together the method which is very simple.

Donner une IP statique à votre Raspberry

The network under Rasbian configuration happens in a file : /etc/network/interfaces. By default, Here's what you should have (It also allows you to have default values) :

Auto lo
iface lo inet loopback iface eth0 inet dhcp
allow-hotplug wlan0 iface wlan0 inet manual wpa-roam/etc/wpa_supplicant/wpa_supplicant.conf iface default inet dhcp

Then, in detail, This file contains the following information :

  • Auto lo : will start the interface automatically during the boot sequence
  • iface lo inet loopback : definition of the loopback interface (you know what 127.0.0.1 essential)
  • iface eth0 inet dhcp : interface eth0 (the RJ45 of the Raspberry) will be configured by sending a DHCP request over the network. Dynamic IP so ..
  • The next block to configure hot wifi via the wpa_supplicant configuration file. In DHCP.

Now to set up a static IP, the principle is simple :

  • Remove the eth0 and wlan0 block you want to change
  • The configuration of the interface : IP, mask and gateway.
  • Restart the machine

For example, your local network behind your router is 192.168.1.x . The address of your router is 192.168.1.1. Therefore, the mask of your network 255.255.255.0 (more info on the netmask on Wikipedia here). And you want your address to be 192.168.1.34

Here's what you'll put in your file /etc/network/interfaces for example the ethernet interface :

iface eth0 inet static address 192.168.1.34
netmask 255.255.255.0
gateway 192.168.1.1

Now the contents of your complete file :

auto lo iface lo inet loopback
iface eth0 inet static address 192.168.1.34
netmask 255.255.255.0
gateway 192.168.1.1
allow-hotplug wlan0 iface wlan0 inet manual wpa-roam/etc/wpa_supplicant/wpa_supplicant.conf iface default inet dhcp

You can now restart the Raspberry and your IP will be the one that you configured. Alternatively, you can restart only the interface :

ifdown eth0 ifup eth0

If you have a DNS problem (google ping does not for example) You can add this line to the block of the interface, After the line gateway :

DNS-nameservers 8.8.8.8

Now for me I'm not a fan of static IP. All my machines are on DHCP. I locked the DHCP range and I use leases based on my MAC addresses. Clearly it will not guarantee an intrusion on the network (change its MAC address is possible) but this is useful for devices that travel.

Some time ago, on holiday in the Sun (Yeah the yellow thing in the sky which gives heat, Remember) no way in the hotel to have Internet. The problem was with my fixed IP forced on the comp, I didn't have the right to use the wifi. At the time I had been obliged to return to DHCP. That is why now I prefer to stay in DHCP do not have to redo the config from / the notebook(s) et tablette 😉

So you have an opportunity to do not modify your configuration and to always have the same IP you.

You can play now for your configuration.

You may also like...

  • Bencici

    For my part I never managed like that suddenly I am in the settings “Berries DHCP” directement sur la Freebox 😛 suffit d’avoir l’adresse MAC du RPi

  • If it is directly on the machine (and not through a session ssh so) one can simply restart the NIC rather than restart the entire machine:

    $ sudo ifdown eth0

    $ sudo ifup eth0

  • Schöpp

    Hello,

    I have a problem and I do not know how to solve.
    Since 2/3 days I can't access a my Raspberry in ssh.
    So I got co top in HDMI to see, and when I plug my ethernet cable no Passover is sent/received by my raspberry.
    When that I perform an ifup eth0 error message is as follows :

    /etc/network/interfaces:2: misplaced option
    ifup: couldn't read interfaces file “/etc/network/interfaces”
    I'm afraid that my eth0 interface is “fucking” but I understand very well everything worked so far….

    Pls need help !

    • Hello
      What is the content of the/etc/network/interfaces file ? It was he who poses a priori problem

  • Schöpp

    The content of the file is the same as written above :

    Auto lo
    iface lo inet loopback
    iface eth0 inet dhcp

    allow-hotplug wlan0
    iface wlan0 inet manual
    WPA-roam /etc/wpa_supplicant/wpa_supplicant.conf
    iface default inet dhcp

    There is obviously no error in the file. I don't even have the memory of having been there before so I think I could have done the mods.
    Even the LEDs work, the yellow LED lights up well as soon as I plug my cable to the RJ45 port, I tried with different cable still the same problem.

    I saw on another forum that some also had the same worries but no solution has been found :/

    • What are the rights on the file ?
      The message says that the ifup command can not access this file …
      ifup: couldn’t read interfaces file « /etc/network/interfaces »
      The filesystem is not corrupt ?

    • And there was a trick that not going to the line 2 especially
      /etc/network/interfaces:2: misplaced option
      May be a hidden character ? Tries to remake a new file

      • Schöpp

        Problem solved !

        Indeed he y' had an error with the file/etc/network/interfaces.
        An unfortunate “i” which has been removed and which at the time made up the error message “couldn't read…”
        What I do not understand this is how this could happen but the key is that it works again !
        Thanks anyway for your quick and clear assistance ! 🙂

  • JB

    Hello, How to change the file interfaces from the GUI. I have no rights and can not change them.

    • Hello
      Open a command line and start editing with an editor such as vi or nano with front sudo to root privileges

      Example :
      sudo vi /etc/network/interfaces

      • helio72

        J’ai lancé la lignesudo vi…” j’edite le fichier interfaces mais je ne peux rien modifier.
        Peux tu m’aider ? d’autre part comment editer une ligne en tant que root . (je suis un débutant de 80 years old).
        Thanks in advance

        • Hello
          Quel est le message d’erreur lors de l’édition ? Quels sont les droits du fichier interfaces (en faisant un ls -l) ?

  • Abuche

    Hello,

    With the/etc/network/interfaces file, a fixed ip is not recognized
    A corrupted file system .. How to check the State and fix this system
    Launch startx does start after CTRL D
    🙂

    • Hello
      To force the file system control at the next startup, It is necessary to put in a terminal as root and use the following command :
      shutdown -rF now
      Another possibility is to create (still, as root) the following file :
      touch /forcefsck

  • Joseph Sajam

    Bonjour Chris j’ai un problème et j’aurai aimé avoir un coup de main, voila j’éssaye depuis presque un mois de partager ma connexion de typeppp”, donc modem 3g, avec ma console via le port ethernet, j’ai donc suivi pas mal de tutos sur “ifconfig”, “iptables”, “route”, I also tried with NAT, but it did not work. So first I would like to put everything “zero” regarding files “interfaces”, “network.conf” and also “iptables” and then I'd like you to help me reconfigure my raspberry and my console aves a fixed IP to be able to share my connection. Thank you to answer me even if you do not agree.

    • Hello,
      I'm no expert but it should not make a bridge between Ethernet and interface l.interface 3g for there to be routing between lesdeux ? In the way of virtual box between the host machine and the VM interface

      • Joseph Sajam

        Thank you for allowing me to respond regarding the bridge between the two interfaces so that there is a route I'll search the web, I will come back to you a return.

  • Dorian192

    Hello. Thank you for this great tutorial that solved my problem. It is rare to find comprehensive tutorials (with details) on a specific topic like this.

  • silia

    Hello, Thank you for this tutorial
    par contre j’ai suivi toutes ces etapes là, et j ‘ai loué une adresse ip statique pour ma carte raspberry pi ; mais je n’arrive toujours pas à me connecter sur internet ( atravaers le cable intranet) :/
    merci pour votre aide d’avance

    • L’ip fixe sera pour ta box de ton fai (chez free c’est deja une ip fixe je crois). Ici on parle d’une ip fixe locale attribuee par ta box ou ton routeur

      • silia

        merciii pour l’intérêt que tu porte à ma question; mais au fait c’est de ça que je parle aussi d’une adresse ip fixe queje dois attribuer a ma carte raspberry pi afin de pouvoir acceder a distance a un serveur apache que je vais installer la dessus 🙂

        • Yes that's what I understood. You have to make the difference between a local fixed IP on your internal network behind the box of your provider and the fixed ip of your box is visble from internet.
          From the outside you have to know the ip your e box. It will be the IP that you bought or that of a provider like no-ip or dyndns. You'll enter your box with sth like http://ip.de.ma.box:num.port
          On your box you must redirect traffic to the external port num.port wearing the apache server on the IP of your raspberry (local ip)
          This article does explaining how to give a fixed ip your feet but without making it visible from outside

          • silia

            d merciii agreement but the fact is that there is a way to share the conection between my pc and pi

          • Yes by making a bridge between the interface connected to the Internet and the Raspberry. It will also be a little DHCP service

Handpicked links

disk Page Caching using disk: enhanced Database Caching 129/505 queries in 0.227 seconds using disk Served from: pihomeserver.fr @ 2017-07-04 21:26:54 by W3 Total Cache -->