DHCP still assigning an IP after setting a static address on the Raspberry Pi
I just picked up a new raspberry pi 3 and ran into a weird problem: after I assigned a static IP to eth0, eth0 was assigned both the static IP, and an IP from dhcp. I don't use debian (the distro that raspbian is based off) often, so this might be a trivial problem for folks that do. I wanted to set a static IP on eth0. Easy enough. I just cracked open /etc/network/interfaces
and changed the contents from 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
to 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
auto eth0
iface eth0 inet static
address 192.168.3.3
netmask 255.255.255.0
gateway 192.168.3.1
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
Notice the changes to eth0 that assign it a static IP. This did work, but it's still getting a IP with dhcp and assigning both IPs to eth0.
This can be seen by running the command:
ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:e4:19:4c brd ff:ff:ff:ff:ff:ff
inet 192.168.3.3/24 brd 192.168.3.255 scope global eth0
valid_lft forever preferred_lft forever
inet 192.168.3.12/24 brd 192.168.3.255 scope global secondary eth0
valid_lft forever preferred_lft forever
inet6 fe80::ba27:ebff:fee4:194c/64 scope link
valid_lft forever preferred_lft forever
That's odd. Some googleing around yielded this post on StackOverflow:
http://raspberrypi.stackexchange.com/questions/32516/multiple-ip-addresses-being-assigned
So it looks like at some point in the recent past, static IP configuration was changed from /etc/network/interfaces
to /etc/dhcpcd.conf
. I should have probably noticed that the interfaces file states at the top:
# For static IP, consult /etc/dhcpcd.conf and 'man dhcpcd.conf'
So let's change the right file. I reverted the changes in /etc/network/interfaces
back to the original and updated /etc/dhcpd.conf
to the following (the last 4 lines are the important part, but I'm including the entire file for completeness):
# A sample configuration for dhcpcd.
# See dhcpcd.conf(5) for details.
# Allow users of this group to interact with dhcpcd via the control socket.
#controlgroup wheel
# Inform the DHCP server of our hostname for DDNS.
hostname
# Use the hardware address of the interface for the Client ID.
clientid
# or
# Use the same DUID + IAID as set in DHCPv6 for DHCPv4 ClientID as per RFC4361.
#duid
# Persist interface configuration when dhcpcd exits.
persistent
# Rapid commit support.
# Safe to enable by default because it requires the equivalent option set
# on the server to actually work.
option rapid_commit
# A list of options to request from the DHCP server.
option domain_name_servers, domain_name, domain_search, host_name
option classless_static_routes
# Most distributions have NTP support.
option ntp_servers
# Respect the network MTU.
# Some interface drivers reset when changing the MTU so disabled by default.
#option interface_mtu
# A ServerID is required by RFC2131.
require dhcp_server_identifier
# Generate Stable Private IPv6 Addresses instead of hardware based ones
slaac private
# A hook script is provided to lookup the hostname if not set by the DHCP
# server, but it should not be run by default.
nohook lookup-hostname
interface eth0
static ip_address=192.168.3.3/24
static routers=192.168.3.1
static domain_name_servers=192.168.3.1
And rebooted. Ta-da:
ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether b8:27:eb:e4:19:4c brd ff:ff:ff:ff:ff:ff
inet 192.168.3.3/24 brd 192.168.3.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::3ac0:7800:5e38:f1db/64 scope link
valid_lft forever preferred_lft forever