Raspberry pi has both static and dhcp ip address

11,632

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

Solution 2

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.

Share:
11,632

Related videos on Youtube

Cantfindname
Author by

Cantfindname

Updated on September 18, 2022

Comments

  • Cantfindname
    Cantfindname over 1 year

    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)

    • Daniel B
      Daniel B over 8 years
      ifconfig is deprecated. Use the ip addr command and provide its output.
    • Narzan Q.
      Narzan Q. over 8 years
      Or Give your raspberry a static IP from your router, and don't forget to restart the router! to clean the lease
    • AStopher
      AStopher over 8 years
      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.
    • Ryan Babchishin
      Ryan Babchishin over 8 years
      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?
    • Daniel B
      Daniel B over 8 years
      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.
  • XP1
    XP1 almost 8 years
    The router is not the problem. The problem is that dhcpcd is not configured on Raspian. See my answer: Raspberry pi has both static and dhcp ip address: http://superuser.com/questions/983378/raspberry-pi-has-both-‌​static-and-dhcp-ip-a‌​ddress/1097873#10978‌​73
  • XP1
    XP1 almost 8 years
    The problem is that dhcpcd is not configured on Raspian. See my answer: Raspberry pi has both static and dhcp ip address: http://superuser.com/questions/983378/raspberry-pi-has-both-‌​static-and-dhcp-ip-a‌​ddress/1097873#10978‌​73
  • Daniel B
    Daniel B almost 8 years
    “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.
  • XP1
    XP1 almost 8 years
    @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.
  • dwlz
    dwlz about 5 years
    This really should be the accepted answer.
  • Zane Claes
    Zane Claes about 4 years
    +1, this is the correct approach on modern Rasbian (and Debian) systems.