5

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?

Dan McCoy
  • 173
  • 1
  • 2
  • 5

5 Answers5

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.

kay_D
  • 151
  • 3
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

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
1
sudo update-rc.d dhcpcd disable

it worked for me

snight
  • 21
  • 1
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).

Milliways
  • 62,573
  • 32
  • 113
  • 225