4

I got a Raspberry Pi running Raspbian, connected via Ethernet on my home LAN (on the ISP's default router). The router is configured to give addresses from 192.168.0.10 onwards with dhcp. I wanted to give a static ip to the Pi, so I assigned 192.168.0.9 to it by editing /etc/network/interfaces as follows:

auto lo
iface lo inet loopback

# auto eth0
# allow-hotplug eth0
# iface eth0 inet manual
iface eth0 inet static
address 192.168.0.9
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.1.255
gateway 192.168.0.1

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

After rebooting and running ifconfig I see correctly(?) my ip to be 192.168.0.9:

eth0      Link encap:Ethernet  HWaddr b8:27:eb:d2:e5:5b
          inet addr:192.168.0.9  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::ba27:ebff:fed2:e55b/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:17019 errors:0 dropped:16 overruns:0 frame:0
          TX packets:1707 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2183986 (2.0 MiB)  TX bytes:241230 (235.5 KiB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:264 errors:0 dropped:0 overruns:0 frame:0
          TX packets:264 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:21840 (21.3 KiB)  TX bytes:21840 (21.3 KiB)

Yet, my router shows the raspberry to have an ip taken from dhcp (192.168.0.10) and the weirdest thing is that I can access the Pi with ssh on both 192.168.0.10 and 192.168.0.9 ips. Any idea why that happens? How can I set the Pi to have only the static address I give to it?

Edit: For future reference: I found out that the problem is a bug of the last update of raspbian and others experience it as well (https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=111709)

5
  • 2
    ifconfig is deprecated. Use the ip addr command and provide its output.
    – Daniel B
    Commented Oct 7, 2015 at 9:47
  • 1
    Or Give your raspberry a static IP from your router, and don't forget to restart the router! to clean the lease
    – Narzan Q.
    Commented Oct 7, 2015 at 9:49
  • 1
    The router handles DHCP, not the Pi. You need to set a static IP on the router for your Pi's MAC address. If you can't do that due to restrictions by your ISP on the router, buy a new router.
    – AStopher
    Commented Oct 7, 2015 at 11:53
  • It's weird it's responding with two addresses at the same time. Is it possible your wireless isn't using 192.168.0.10? Does the DHCP entry for the Pi have the same mac address as what appears in ifconfig eth0? Commented Oct 7, 2015 at 12:39
  • Oh and by the way: Whether or not the DHCP lease is still lingering is totally irrelevant. A regular computer drops packets not directed at its IP addresses.
    – Daniel B
    Commented Oct 8, 2015 at 9:06

4 Answers 4

1

How to set static IP address on Rasperry Pi Raspian

Don't use /etc/network/interfaces to set static IP. Use /etc/dhcpcd.conf instead.

Restore your /etc/network/interfaces to the original file, or undo your changes:

sudo nano /etc/network/interfaces

Replace your changes with manual setting in /etc/network/interfaces:

iface eth0 inet manual

Configure dhcpcd:

sudo cp /etc/dhcpcd.conf /etc/dhcpcd.conf.bak
sudo nano /etc/dhcpcd.conf

Add your static profile options to the bottom of /etc/dhcpcd.conf:

interface eth0
static ip_address=192.168.1.100/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

Remove leases:

sudo rm /var/lib/dhcp/*

Reboot:

reboot

Another option is to disable dhcpcd. After you disable dhcpcd, you can use /etc/network/interfaces instead to set static IP.

Configure /etc/network/interfaces:

sudo cp /etc/network/interfaces /etc/network/interfaces.bak
sudo nano /etc/network/interfaces

Replace manual setting with static settings in /etc/network/interfaces:

auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
network 192.168.1.1
broadcast 192.168.1.255
gateway 192.168.1.1

Configure dhcpcd:

sudo cp /etc/dhcpcd.conf /etc/dhcpcd.conf.bak
sudo nano /etc/dhcpcd.conf

Add the option to the bottom of /etc/dhcpcd.conf:

denyinterfaces eth0

Or you can disable the dhcpcd service:

systemctl disable dhcpcd.service

Remove leases:

sudo rm /var/lib/dhcp/*

Reboot:

reboot

Source:

4
  • 1
    “Don't use /etc/network/interfaces to set static IP.” — I wholeheartedly disagree. This may be a viable workaround for the bug the OP is experiencing, but it’s not good advice in general.
    – Daniel B
    Commented Jul 7, 2016 at 5:36
  • 1
    @Daniel B, it is not a bug. Raspian's own /etc/network/interfaces file even says to use /etc/dhcpcd.conf instead: "For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'". It is Raspian's recommended way of setting static IP.
    – XP1
    Commented Jul 7, 2016 at 5:48
  • This really should be the accepted answer. Commented Feb 2, 2019 at 21:24
  • +1, this is the correct approach on modern Rasbian (and Debian) systems.
    – Zane Claes
    Commented Apr 5, 2020 at 16:30
1

First some background.

Debian and hence Raspbian have multiple ways of configuring a network. Two of the most commonly used are.

  1. "ifupdown", the traditional soloution which works well for static network setups like desktops and servers but doesn't really work very well for devices that roam around.
  2. network manager, the tool preffered by the gnome folks and now used by default on Debian desktop installations. Handles dynamic situations well but is rather heavy and is difficult to work with if you don't have a GUI running and policykit working correctly.

The raspberry pi foundation chose to use neither of these and instead use dhcpcd5 for network configuration in their raspbian images. dhcpcd5 is packaged in Debian but the raspberry pi foundation raspbian images are the only Debian-based system I know of to use it by default.

I can understand why they wanted to move away from ifupdown to something that better handled roaming devices but I'm not really sure why they chose dhcpcd over network manager. Maybe they found it more lightweight or easier to integrate into their desktop setup or something.


Anyway due to the choices the Raspberry pi foundation made, if you configure an IP address through /etc/network/interfaces then ifupdown sets up your static address as you expect, then dhcpcd comes along and adds a dhcp address as well.

There are two possible soloutions to this.

  1. Use dhcpcd configuration to set up the static address.
  2. Get rid of dhcpcd5 and configure your network soley using ifupdown.

Which is the better option probablly depends on what you want to use the Pi for.

1

There are three problems with your configuration: first, DHCP leases have an expiration time which is generally set at 1hour, but can also be much longer. Since static addresses are completely unknown to the DHCP server, there is no way it can possibly know that your Ethernet MAC address is now associated with two IP addresses.

So far, if someone searched for your RPI via its name, the connection would have been made to the old IP address, where there is no one to reply. For this reason, from now on connections to your RPI via its name will be impossible, unless you activate SAMBA or Bonjour services on it.

This is why I prefer reserved addresses, where the DHCP is configured to give always the same IP address to the given MAC address, but negotiation of DNS servers and machine name occur as if a new lease was being negotiated.

Second, you have not configured DNS servers in your static stanza: add the following line,

dns-nameservers 8.8.8.8 8.8.4.4

(notice the plural, servers, and the lack of punctuation between the two IP addresses). If you don't like Google DNSes, replace with whatever suits you.

Third, you have a wrong broadcast address, given your network and netmask: both in your /etc/network/interfaces stanza, and in the output of ifconfig (which, by the way, is obsolete, you should be using ip from the iproute2 suite), it can be seen to be 192.168.1.255. It should be instead 192.168.0.255. When these computations are complex, use ipcalc:

ipcalc 192.168.0.0/24
Address:   192.168.0.0          11000000.10101000.00000000. 00000000
Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
Wildcard:  0.0.0.255            00000000.00000000.00000000. 11111111
   =>
Network:   192.168.0.0/24       11000000.10101000.00000000. 00000000
HostMin:   192.168.0.1          11000000.10101000.00000000. 00000001
HostMax:   192.168.0.254        11000000.10101000.00000000. 11111110
Broadcast: 192.168.0.255        11000000.10101000.00000000. 11111111
Hosts/Net: 254                   Class C, Private Internet

I am not positive that this is the root of your problem, but it will surely add to it in the near future, as your broadcasts (including ARP traffic) will be neglected by all other machines on the LAN.

1
0

as Narzan pointed out, your router most likely still has the lease for 192.168.0.10.

It is not clear that rebooting you router will be of any use, try to log in the router and

  1. clear IP associate to b8:27:eb:d2:e5:5b if possible.
  2. clear DHCP lease table (this will of course change all other devices IPs).
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .