User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4247
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Static IP address templates for dhcpcd.conf

Tue Mar 15, 2016 11:03 am

Since the move to Jessie, we've had to change the way we give our Raspberry Pi's a static IP address . Rather than setting it in /etc/network/interfaces, it has to be done in /etc/dhcpcd.conf. There are examples of simple configurations of dhcpcd.conf going around that look like this:

Code: Select all

interface eth0
static ip_address=192.168.1.101/24
static routers=192.168.1.1
This is all well and good, but we can end up with problems if we hook the Pi into a different network. For example if we take the Pi we configured like this to school, where the network is 10.0.0.0/16, or round to a friend's house where his router is using 192.168.0.0/24, we won't be able to talk to it at all unless we attach a keyboard and screen and reconfigure it.

However dhcpcd.conf offers more features than network/interfaces, and we can now change the static IP address in various circumstances.

The most useful of these has dhcpcd probe the network looking for the router. Depending on which router it finds, it then configures a static IP address to suit:

Code: Select all

######################################################
# TEMPLATE: A different IP address on each network
#
#           The arping address should be the router
#           or some other machine guaranteed to be
#           available. You need to know the addresses
#           of the servers. If none of the arpings find
#           an active machine then you will get a DHCP
#           allocation.
######################################################
interface eth0
arping 192.168.2.1
arping 192.168.0.254

profile 192.168.2.1
static ip_address=192.168.2.44/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1

profile 192.168.0.254
static ip_address=192.168.0.44/24
static routers=192.168.0.254
static domain_name_servers=192.168.0.254
We can add as many "arping" lines as we want so long as each has a "profile" section that matches it. If the Pi is then put on a network it doesn't recognise then we will get a dynamic dhcp address as normal. It won't be static but we can still talk to our Pi once we find out what its address is. (The Android app Fing is useful for that.)

Another common scenario is where the Pi is used without a router, and so it cannot get a dhcp address. Without any configuration this would mean that it got a "link-local" address in the 169.254.0.0/16 network. However the address it gets is random, which is useless in many cases. Instead you can tell dhcpcd to allocate a specified address when DHCP fails:

Code: Select all

######################################################
# TEMPLATE: A static IP address only when no DHCP
#
#           The profile name is arbitrary. Use "fred"
#           if you want. Not much we can put as
#           default servers, but set them up as
#           you usually would.
######################################################
interface eth0
fallback nodhcp

profile nodhcp
static ip_address=10.0.0.1/8
static routers=10.0.0.1
static domain_name_servers=10.0.0.1
You can mix and match these two templates if you want:

Code: Select all

interface eth0
arping 192.168.1.1
fallback mylan

profile 192.168.1.1
static ip_address=192.168.1.99/24
static routers=192.168.1.1
static domain_name_servers=192.168.1.1

profile mylan
static ip_address=192.168.0.99/24
These templates will work for Wifi as well as Ethernet of course. Just substitute "wlan0" for "eth0". The interface sections can only relate to one interface, so

Code: Select all

interface eth0
interface wlan0
is invalid (and will select only wlan0.) But I think the "profile" sections are shared between them. More research required here.

It is possible to tell dhcpcd to select based on the WiFi ssid. However I had problems with this that seem to have permanantly messed up my WiFi, so I don't recommend it.

User avatar
RaTTuS
Posts: 10104
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Static IP address templates for dhcpcd.conf

Tue Mar 15, 2016 11:21 am

if you have 2 RPi's and just want them to talk to each other via the Ethernet link
what would you suggest putting into the dhcpd.conf file?
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4247
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Static IP address templates for dhcpcd.conf

Tue Mar 15, 2016 11:44 am

Just use the fallback method to select a configuration if there is no DHCP server.

Code: Select all

interface eth0
fallback twopi

profile twopi
static ip_address=192.168.1.101/24
With different IP addresses for each Pi, of course.


NB: All of these templates are for adding to the end of dhcpcd.conf. The existing contents set up the DHCP and such stuff as your hostname, so you want to leave that in place.

Tyrael_Archangel
Posts: 23
Joined: Mon Feb 29, 2016 10:47 pm

Re: Static IP address templates for dhcpcd.conf

Wed Mar 23, 2016 9:28 pm

Is there a way to dhcpcd.conf so that it tries all the arping profiles and if none are found it just sets a specified static address instead of attempting to get a DHCP address? (kinda like the fallback profile, but not needing DHCP to fail before it sets it)

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4247
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Static IP address templates for dhcpcd.conf

Wed Mar 23, 2016 11:59 pm

You could try putting the "static" lines from the original simple version immediately after the arping lines.

So long as you have a keyboard and screen attached and stick to eth0, I don't think you can break anything by experimenting.

The documentation on how it all works is woeful (especially if you are not a DHCP guru) and the code is unintelligible at first glance. So I still don't understand the order it does things in. There are other capabilities mentioned in the developer's email list that are not mentioned in the docs that may or may not now be obsolete, such as using the MAC address as the profile name which might be useful in some circumstances.

But as a first approximation, each time it tries to allocate an address it starts at the top of the file and works down executing each line. Non-matching interfaces and profiles make it start ignoring lines, but I don't have a handle on when it stops ignoring lines. Which means a file that only has "interface" or "profile"s in works as you might expect but once you start mixing them I have no idea what it does.

Tyrael_Archangel
Posts: 23
Joined: Mon Feb 29, 2016 10:47 pm

Re: Static IP address templates for dhcpcd.conf

Thu Mar 24, 2016 2:59 am

Thanks for the advice on this rurwin, when I get a chance I'll play around with this and let you know how it goes. Would you happen to know if there is a command I could run after modifying the conf file to force dhcpcd to reload and re-configure the interface based on the new conf file?

beta-tester
Posts: 1208
Joined: Fri Jan 04, 2013 1:57 pm
Location: de_DE

Re: Static IP address templates for dhcpcd.conf

Thu Mar 24, 2016 2:39 pm

rurwin wrote:...The most useful of these has dhcpcd probe the network looking for the router ...
oooh... thank you for pointing to this. very useful information.
{ I only give negative feedback }
RPi Model B (rev1, 256MB) & B (rev2, 512MB) & B+, RPi2B (1GB), 64GB microSDXC1 class 10, HDMI 1920x1080, keyboard-mouse-combo (wireless), PiCamera, ethernet-cable, 5V/1.2A power supply, Wifi dongle (rt5370)

Tyrael_Archangel
Posts: 23
Joined: Mon Feb 29, 2016 10:47 pm

Re: Static IP address templates for dhcpcd.conf

Thu Mar 24, 2016 6:55 pm

So have I have done some experimenting with your suggestions and I cant get this to work the way I would like.

If I put the static lines right after the arping lines, that address is always assigned even if the arping is found.

if I put the static lines after the arping profiles, dhcpcd crashes (I assume that it is treating them as part of the last profile and would therefore have conflicting options).

if I add the nodhcp option and a fallback profile, then dhcpcd configures a link-local address, tries to select the correct arping profile, then crashes before it completes.

Any other things I could try to get this working?

Our Pis need to have a known static address so that we can find them as there are too many devices on our network to make using dhcp for them a viable option, but having profiles setup for when they are moved onto lab networks where the normal addresses will not work would be very valuable. any suggestions?

paulv
Posts: 546
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Static IP address templates for dhcpcd.conf

Fri Apr 08, 2016 8:21 pm

Great stuff and very timely for my wireless Access Point project.
Thanks rurwin!

I want to be able to use this AP in several locations, without knowing beforehand what the router setup is.

[update]
I really like the possibilies this offers, now that it has been explained to me in more detail.
However, for the sake of non-expert users (same here) following this, I made the above statement about my AP but need to retrakt that.
After some tinkering and even more thinking about the ramifications, I'm not going to use static addresses on the Ethernet side (eth0) of my AP. It's OK to do all that on my own SOHO network, and I do, but the risks to screw-up a foreign network (apartment complex, hotel,...) by assigning a static IP address are simply too great.

I'll get the IP address assigned through the DHCP server from the router and lacking a console, I'll use the clever trick to send the IP address as audio at boot time. If I really need it.
viewtopic.php?f=91&t=11857&p=949015

To get access to the Pi on my W10 box, I already use PuTTY, but will now only use the pi_hostname.local addressing instead of the static IP address. If you do too, you need to ask raspi-config (Advanced Options) to enable the SSH part and I suggest you change the RPi hostname.
Last edited by paulv on Sat Apr 09, 2016 11:20 am, edited 1 time in total.

mattmiller
Posts: 1954
Joined: Thu Feb 05, 2015 11:25 pm

Re: Static IP address templates for dhcpcd.conf

Fri Apr 08, 2016 9:09 pm

Just found this post - looks very useful and what's been needed for a while :) Ta :)
Matthew

diveguy84
Posts: 7
Joined: Mon May 30, 2016 8:29 pm

Re: Static IP address templates for dhcpcd.conf

Sun Jun 05, 2016 11:27 pm

Is there a way to add multiple Static IPs to dhcpcd.conf?
I would like to start using:

Code: Select all

interface eth0
arping 192.168.0.1

profile 192.168.0.1  
static ip_address=192.168.0.2/24
static routers=192.168.0.1  
I tried :

Code: Select all

interface eth0
static ip_address=192.168.0.2/24
static routers=192.168.0.1

interface eth0:0
static ip_address=192.168.0.3/24
static routers=192.168.0.1
But only the eth0: wan0: and lo: interfaces display.

As shown below I can use cmdline so I could setup some type of script but I would like to use dhcpcd.conf or some other correct config file:

Code: Select all

[email protected]:/home/pi# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr b8:27:eb:ed:ac:77  
          inet addr:192.168.0.2  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::4077:6ed1:deb7:ad51/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:698 errors:0 dropped:0 overruns:0 frame:0
          TX packets:513 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:690340 (674.1 KiB)  TX bytes:64559 (63.0 KiB)

[email protected]:/home/pi# ifconfig eth0:0
eth0:0    Link encap:Ethernet  HWaddr b8:27:eb:ed:ac:77  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

[email protected]:/home/pi# ifconfig eth0:0 192.168.0.3
[email protected]:/home/pi# ifconfig -a
eth0      Link encap:Ethernet  HWaddr b8:27:eb:ed:ac:77  
          inet addr:192.168.0.2  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::4077:6ed1:deb7:ad51/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:721 errors:0 dropped:0 overruns:0 frame:0
          TX packets:540 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:691980 (675.7 KiB)  TX bytes:67657 (66.0 KiB)

eth0:0    Link encap:Ethernet  HWaddr b8:27:eb:ed:ac:77  
          inet addr:192.168.0.3  Bcast:192.168.0.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

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:348 errors:0 dropped:0 overruns:0 frame:0
          TX packets:348 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:31584 (30.8 KiB)  TX bytes:31584 (30.8 KiB)

wlan0     Link encap:Ethernet  HWaddr b8:27:eb:b8:f9:22  
          inet6 addr: fe80::3888:5783:8211:a2d4/64 Scope:Link
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:111 errors:0 dropped:111 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:41556 (40.5 KiB)  TX bytes:0 (0.0 B)
I verified the 2nd IP can be pinged from a different machine in the private network as well.

diveguy84
Posts: 7
Joined: Mon May 30, 2016 8:29 pm

Re: Static IP address templates for dhcpcd.conf

Tue Jun 07, 2016 12:45 pm

Found what I needed. Added a line to /etc/rc.local before the exit 0 lime of:

Code: Select all

ifconfig eth0:0 192.168.0.3
This works ok because even though rc.local is run at every switch of run level. Because in practice I do not plan on switching run levels other than booting and shutdown. And adding the extra interface during shutdown will have no impact.

martiPi
Posts: 6
Joined: Wed Jan 14, 2015 5:11 pm

Re: Static IP address templates for dhcpcd.conf

Wed Jul 20, 2016 6:08 pm

As a beginner at this malarky :? I am assuming that when multiple routers / static IPs have been assigned within dhcpcd.conf, that the relevant SSIDs / PSKs need to be added to wpa_supplicant.conf as additional {networks}
Would that be correct?

DirkS
Posts: 9221
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Static IP address templates for dhcpcd.conf

Thu Jul 21, 2016 4:13 am

martiPi wrote:Would that be correct?
Yes

ScottS
Posts: 4
Joined: Fri Jul 22, 2016 6:58 pm

Re: Static IP address templates for dhcpcd.conf

Sat Jul 23, 2016 9:37 pm

Does any one have a working example or template for a static IPv6 address. The documentation is not clear and I have tried a number of variations with no success.

I have found a number of post related to this but none with a functional solution. As of now I have settled on using the exit hook function but it is not a clean solution and does have some issues as well.

/etc/dhcpcd.exit-hook
#/bin/bash
/sbin/ip -6 addr add 2001:4900:1420:100::41/64 dev eth0

User avatar
DougieLawson
Posts: 34112
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Static IP address templates for dhcpcd.conf

Sun Jul 24, 2016 4:06 pm

I do it by setting

Code: Select all

# Generate Stable Private IPv6 Addresses instead of hardware based ones
#slaac private
slaac hardware
That uses your /64 prefix and an EUI-64 address build from the 48-bit MAC address for the hardware.

MAC: b8:27:eb:ab:cd:ef
EUI64 : ba27:ebff:feab:cdef
IPv6: 2001:4900:1420:100:ba27:ebff:feab:cdef
We muck about with bit six of the 64 byte suffix, but it's a predefined and predictable IPv6 address.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

ScottS
Posts: 4
Joined: Fri Jul 22, 2016 6:58 pm

Re: Static IP address templates for dhcpcd.conf

Mon Jul 25, 2016 6:36 am

I understand that a EUI-64 address is predictable but what I need to do is define an arbitrary static address or even an alias.
I'm also trying to sort out if it is an issue with Raspbian,, Debian, DHCPcd or my config.

Again this works but based on the documentation, I should be able to do this within /etc/dhcpcd.conf.

cat /etc/dhcpcd.exit-hook
#/bin/bash
/sbin/ip -6 addr add 2001:XXXX:xxxx:100::41/64 dev eth0
/sbin/ip -6 addr add 2001:XXXX:xxxx:100::beef/64 dev eth0
/sbin/ip -6 addr add 2001:XXXX:xxxx:100::dead/64 dev eth0

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4247
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Static IP address templates for dhcpcd.conf

Mon Jul 25, 2016 9:07 am

The author of DHCP doesn't have much truck with static IP addresses.
Roy Marples on dhcpcd-discuss email list wrote:Well, to be blunt, the problems you are describing sound of your own making.
In todays world there is little need of static IP configuration. I have many Pi
like devices (mainly OpenWRT based units) and yes I do have a Pi. None of them
have a static IP and dhcpcd runs with a stock config. Here I run a DHCP server
with static IP allocations to the units that need to be up 100% or are
extremely brickable.

Sometimes I take these units out of the production loop (for upgrade,
maintenance, etc) and place them into an ad-hoc network where there is no DHCP
server. I still access these units by hostname and it works fine - thanks to
IPv4LL (provided by dhcpcd) and mDNS (provided by something else).

I really don't recall the last time I accessed any of my units by IP address
alone :)
So, while v4 addresses have always been supported, I wouldn't be particularly surprised if getting a static v6 address was difficult.

ScottS
Posts: 4
Joined: Fri Jul 22, 2016 6:58 pm

Re: Static IP address templates for dhcpcd.conf

Mon Jul 25, 2016 12:30 pm

rurwin:
Thanks for the pointer to the dhcpcd-discuss list. Looking there I found a thread about this issue/feature.

Reading the thread "dhcpcd and static IPv6 addresses" and then the commit logs it appears that "static ip6_address=XXXX:XXXX:XXXX:XXXX::XXXX/64" should be a valid config option as of 2016-04-29 08:28:28 in dhcpcd-6.10.2. However the Raspbian image published 2016-05-27 it appears to have 6.7.

Later today I'll try to see what is involved in upgrading the installed version and do some more testing.

References:
dhcpcd-discuss: Re: dhcpcd and static IPv6 addresses
http://roy.marples.name/archives/dhcpcd ... /1211.html
dhcpcd commit logs:
http://roy.marples.name/projects/dhcpcd/timeline/

Note:
Reading of the history on dhcpcd-discuss and has caused some heave edits to this post.

Tschoran
Posts: 1
Joined: Tue Mar 07, 2017 1:28 pm

Re: Static IP address templates for dhcpcd.conf

Tue Mar 07, 2017 1:34 pm

what should dhcpcd.conf look like if i want dhcpcd to select the ip configuration based on the ssid?

ScottS
Posts: 4
Joined: Fri Jul 22, 2016 6:58 pm

Re: Static IP address templates for dhcpcd.conf

Tue Mar 07, 2017 2:21 pm

You may be best to start a separate thread about this. But you would want to use some thing like this:

ssid WLAN1
static ip_address=192.168.0.10/24
static routers=192.168.1.1
static domain_name_servers=192.168.0.1

ssid WLAN2
static ip_address=192.168.2.10/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1

This assumes that you have already configured the supplicant elsewhere.

User avatar
emgi
Posts: 357
Joined: Thu Nov 07, 2013 4:08 pm
Location: NL

Re: Static IP address templates for dhcpcd.conf

Tue Mar 07, 2017 6:59 pm

On one of my Pi's I'm running Jessie 2017-01-11.
Setting a fxed ip is extensively documented and I can confirm it works.
However, I'm struggling to also set fixed values for the domain name and the search list.
I got the domain name inserted by adding it to /etc/resolvconf.conf:

Code: Select all

domain = mydomain.local
That I found this was a coincidence, the prescribed variable 'domain_name' doesn't do the job.
The same approach is unsuccessful for the search list and the various man pages are inconsistent at this point.
What I would like is a line like this in /etc/resolv.conf:

Code: Select all

search = mydomain.local
It shoudn't be that hard if you where it's done. Can anyone point me to the correct documentation?

/Leo

Tyrael_Archangel
Posts: 23
Joined: Mon Feb 29, 2016 10:47 pm

Re: Static IP address templates for dhcpcd.conf

Tue Mar 07, 2017 8:58 pm

emgi wrote:On one of my Pi's I'm running Jessie 2017-01-11.
Setting a fxed ip is extensively documented and I can confirm it works.
However, I'm struggling to also set fixed values for the domain name and the search list.
I got the domain name inserted by adding it to /etc/resolvconf.conf:

Code: Select all

domain = mydomain.local
That I found this was a coincidence, the prescribed variable 'domain_name' doesn't do the job.
The same approach is unsuccessful for the search list and the various man pages are inconsistent at this point.
What I would like is a line like this in /etc/resolv.conf:

Code: Select all

search = mydomain.local
It shoudn't be that hard if you where it's done. Can anyone point me to the correct documentation?

/Leo
Not sure what you mean by search list but to add domain name to a static profile in /etc/dhcpcd.conf we use:

Code: Select all

static domain_name=mydomain.local
and this works like a charm on our network. In regards to the documentation, I agree that dhcpcd's documentation is poor (similar to the maintainer's attitude towards static configurations). Getting our profiles generated was a pain and took a fair bit of trial and error.

User avatar
emgi
Posts: 357
Joined: Thu Nov 07, 2013 4:08 pm
Location: NL

Re: Static IP address templates for dhcpcd.conf

Wed Mar 08, 2017 2:57 pm

Not sure what you mean by search list
The search list is another parameter in /etc/resolv.conf which contains the domain suffix(es) to check when an incomplete domain name is used as input. You typically use this within the local domain by entering a hostname without the fqdn.

I tested your parameter in /etc/dhcpcd.conf. It does indeed work after a reboot.
My tests focused on running

Code: Select all

resolvconf -u
Actually that should give the same result since resolvconf is run at boot?

Killertechno
Posts: 139
Joined: Wed Jan 02, 2013 8:28 am

Re: Static IP address templates for dhcpcd.conf

Tue Apr 17, 2018 3:02 pm

About templates.... (a bit offtopic), is there a way to keep dynamic and static IP at same time?
I need dynamic to be used in dhcp network while active static IP as default for maintenance.
There were some examples using /etc/network/interfaces but I have no idea about doing this via dhcpcd.conf.
Thanks.

Return to “Raspbian”