4

I've been trying for a few days now to set a static IP for my Pi but have so far been unsuccessful.

I have a Model B Revision 2.0 (revision code 000d). I'm running Jessie Lite released 2016-05-27.

Previously I used to set an IP in /etc/network/interfaces but I have now read from multiple sources that the correct way is to edit /etc/dhcpcd.conf. Some sources suggest only to edit dhcpcd.conf, some suggest editing both. I have tried both.

Currently I have the following...

/etc/network/interfaces

# 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.1.10
gateway 192.168.1.1
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255

/etc/dhcpcd.conf

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

# 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

With the above set, the Pi ends up with two IP addresses. Ouput of "ip addr":

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether b8:27:eb:fd:ea:5e brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.1.127/24 brd 192.168.1.255 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::ba27:ebff:fefd:ea5e/64 scope link
       valid_lft forever preferred_lft forever

I've also tried stopping DHCP with the following: sudo update-rc.d -f dhcpcd remove

But after a reboot it still ends up with two IPs.

Edit: Forgot to mention, this is a wired connection. No wireless.

Edit: I know that I shouldn't have to edit both files. The above configuration is the last in a long series of combinations tried for the sake of trying everything.

If I edit only the interfaces file, it get two IPs. If I edit only the dhcpcd.conf file, It gets one IP assigned by dhcp (not the static one I've set).

sudo update-rc.d -f dhcpcd remove does not stop dhcp after a reboot.

sudo systemctl stop dhcpcd.service crashes the Pi and has no effect after pulling the power and plugging in again.

How can I stop DHCP?

  • Note this is not solely dependent on the pi. I doubt most home routers will route to devices that arbitrarily assign themselves whatever IP address they feel like using, so you must ensure the same static IP is associated with the pi's ethernet MAC address in your router configuration, as dhcpd may end up working with it and against you otherwise. If you can confirm you have done this, I can offer you a bulletproof solution along these lines. – goldilocks Aug 2 '16 at 14:44
5

Setting a fixed IP address on a recent Jessie is easy:

nano /etc/dhcpcd.conf

and add at the bottom (i.e. below nohook lookup-hostname):

interface eth0
static ip_address=192.168.1.10/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1 8.8.8.8 4.2.2.1
static domain_search=yourlan
static domain_name=yourlan

No other file to be touched, based on a fresh installation.

The last two lines are optional and are only used if you need to specify your domain name, for domain name lookups in your lan. "yourlan" is a symbolic name of your domain, it could be something like "theshire.local" or similar.

  • Thanks. What's "yourlan"? – Dan McCoy Aug 1 '16 at 22:37
  • Shouldn't "wlan0" be "eth0"? – Dan McCoy Aug 1 '16 at 22:43
  • I've tried with the original interfaces file, and the dhcpcd.conf file as: interface wlan0 static ip_address=192.168.1.10/24 static routers=192.168.1.1 static domain_name_servers=192.168.1.1 8.8.8.8 4.2.2.1 But that didn't work. Back to one IP assigned by DHCP. I changed "wlan0" to "eth0" and left out the last two lines as I don't know what "yourlan" is. – Dan McCoy Aug 1 '16 at 22:48
  • I improved my example to more closely match your concrete requirements (eth0) and clearified the optionality of the last two lines. – kay_D Aug 2 '16 at 14:38
  • You can safely omit domain_search= and domain_name= entries. Unless you know that you need them. – SDsolar Jul 8 '18 at 3:14
2

If you are running a recent Raspbian /etc/network/interfaces should be as below. If you have changed it to the original .

# 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

In /etc/wpa_supplicant/wpa_supplicant.conf find your network and add the line ( id_str="A_name_of_your_choice" ) at the bottom

network={ ssid="ESSID" psk="Your_wifi_password" id_str="A_name_of_your_choice" }

Setup a Static IP Address

Edit /etc/dhcpcd.conf as follows:-

 Here is an example which configures a static address, routes and dns.
       A_name_of_your_choice static_eth0
       static ip_address=10.1.1.12/24
       static routers=10.1.1.1
       static domain_name_servers=10.1.1.1

       A_name_of_your_choice static_wlan0
       static ip_address=192.168.0.12/24
       static routers=192.168.0.1
       static domain_name_servers=192.168.0.1

       interface wlan0
       fallback static_wlan0

       interface eth0
       fallback static_eth0

In this way, you can add a static IP for a specific router . I believe this is a more dynamic way to do things. Any queries you can send a direct message on facbook,instagram my id grksumanth.

1
sudo update-rc.d dhcpcd disable

it worked for me

1

Execute

sudo nano /etc/network/interfaces 

and enter:

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

Test:

sudo ifdown wlan0
sudo ifup wlan0
0

If you follow How do I set up networking/WiFi/Static IP it should work.

In fact what you have will allocate 2 IP addrsses, as you have discovered. You have 2 options, disable dhcpcd and use static in interfaces OR put /etc/network/interfaces back to default (recommended).

  • That's what I've tried. It doesn't work. – Dan McCoy Aug 2 '16 at 5:43
  • If I only edit the interfaces file, I get two IPs. – Dan McCoy Aug 2 '16 at 5:43
  • If I only edit the dhcpcd.conf file, I get one IP assigned by dhcp (not the static one I've set). – Dan McCoy Aug 2 '16 at 5:45
  • Editing both as described in the question is a result of trying everything because nothing else is working. I'm aware that I shouldn't have to edit both. – Dan McCoy Aug 2 '16 at 5:46
  • @DanMcCoy If you try to use /etc/network/interfaces you are going to continue getting problems unless you disable dhcpcd. What does ifconfig show (preferably WITHOUT trying static) and after? No one can know what the correct settings for your network are, but if you post what DHCP sets this is a start. Frankly I still don't know why so many people keep making life difficult for themselves by using static. If you really MUST, set a static reservation in your router. – Milliways Aug 2 '16 at 6:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.