5

I am trying to setup static IPs on my 5 raspberry PIs and there is no clear answer as to which configuration to choose and why.

As per a comment on one of the answers to this question How do I set up networking/WiFi/static IP address on Raspbian/Raspberry Pi OS?

Readers should do their homework before adopting the static ip configuration advocated by this answer. For example, the author of dhcpcd discourages use of static ip!! In man dhcpcd.conf he states, For IPv4, you should use the inform ipaddress option instead of setting a static address. Ask yourself why you should follow the recommendation in this answer when the author of dhcpcd says otherwise?

The configurations below all do the same thing but it isnt clear which is preferred.

Set via request

interface wlan0
request 192.168.68.20

Set via static ip

interface wlan0 
static ip_address=192.168.68.20/24 
static routers=192.168.68.1 
static domain_name_servers=192.168.68.1

Set via inform

interface wlan0
inform 192.168.68.20

3 Answers3

6

Excellent question! Here's my answer; a quick summary & details for those interested:

Quick Summary

First/Best Choice: Don't use a static IP address

Your RPi system comes with a network manager (dhcpcd) designed to work on most networks without a single tweak; "it just works" as they say.

Second Choice: Use inform or request options

My network has dynamic-configured hosts on it that have had the same IP address for over 3 yeears! DHCP-assigned IP addresses tend to be "sticky". If you feel the need to make them more-or-less permanent, use the inform or request options. The author of dhcpcd, who knows a little about how this protocol works, recommends inform, but see the However... section in the details below.

Third Choice: Use the static value configuration per man dhcpcd.conf

If you don't understand DHCP, don't choose this option. Back it up, and take the First or Second Choice - really.

Some Details and Background

First choice:

The first choice should always be: Don't use a static IP address - unless you really need one. When do I need one? Well, hardly ever... Quite a few think they need a static IP, but they don't. Static IPs seem to bring out the "knob dicker" in far too many users!

If your network is set up properly, your zero-conf networking will make your RPi very easy to find. It is also very easy to set up:

  1. Verify you have a unique entry in /etc/hostname. Don't use the default raspberrypi (unless you're only ever going to have a single one); use something like raspi3B, or bookcase-raspi - or something like that. A hostname should mean something to you.

  2. Make sure you have an entry for 127.0.1.1 in /etc/hosts. It should include your chosen hostname (from above) - like this:

127.0.1.1 raspi3B
-- OR --
127.0.1.1 bookcase-raspi

Having done this properly, you can always find your RPi on the network by referring to it as follows:

hostname.local -- or -- simply hostname for example:
ping raspi3B.local -- or -- ping raspi3B ssh pi@raspi3B.local
https://raspi3B.local

If you can find your host by its hostname, why would you want to use a fixed IP? Why not let DHCP do its job? Manual network configuration is drudgery - it's why DHCP was invented. Don't ne a luddite :)

Second choice:

If you have an RPi, and you are running Raspberry Pi OS (nee raspbian), your default DHCP client and network manager is called dhcpcd. You should get to know it - it does an important job for you, so ignore it at your risk.

To configure a static address for your RPi, you should RTFM. The system manual for dhcpcd may be called up from a terminal window very simply: man dhcpcd. There is a separate manual for dhcpcd's configuration file, located at /etc/dhcpcd.conf. Access the "man page" for the configuration file as follows: man dhcpcd.conf; it is here that you will find the instructions for configuring a static IP.

In man dhcpcd.conf the section static value contains the information we need. This seems to be the least-read item of documentation in all of Linux-dom. It lays out exactly how to set up a static configuration as follows (IPv6 omitted for clarity):

interface eth0
   static ip_address=192.168.0.10/24
   static broadcast_address=192.168.0.63
   static routers=192.168.0.1
   static domain_name_servers=192.168.0.1

So we see now that in addition to the IP address, there are other necessary items of information handed over to your RPi host during DHCP: broadcast address, router (gateway) and DNS. Everything your RPi host needs to find its way around the network. So a proper static config includes all of these items. Miss one of them, and the wheels come off... you're wasting your time posting a question here (or worse - somewhere else) wondering why your RPi isn't working... "it worked yesterday" is the most frequently whined complaint heard here.

But if you got to this point in man dhcpcd.conf, you've missed the most important statement in the manual re static confoguration. The author of dhcpcd says this:

For IPv4, you should use the inform ipaddress option instead of setting a static address.

That's a clear enough statement. It's a correct statement. It's correct because of the way the DHCP protocol is designed - see RFC2131 for details. In brief summary, the Internet Standard for DHCP states that static-configured hosts do not communicate with the DHCP server. Now, as an example, suppose that your ISP one day decides to use a new DNS server address, and you are running a static configuration. That's right - now your RPi may not be able to resolve host names on the Internet. Since you didn't read the manual carefully, you're back to wasting time - patching up self-inflicted wounds. This is only one handicap - there are others.

Using a static configuration means also that you must make an IP address reservation on your router/gateway/DHCP server. Since a static-configured host does not communicate with a DHCP server, the job of preventing address collisions falls on your to-do list. Believe it or not, this requirement escapes most users of the static configuration.

However...

While the author's admonition to use the inform option instead of a static configuration is spot-on, there is currently a bug in the implementation of the dhcpcd software distributed by the Raspberry Pi organization. This bug creates a tsunami of log messages when the inform option is enabled - I measured 8/second on a RPi 0W. A patch has been submitted to the upstream sources, the RPi maintainers were notified o/a 2 April, 2022 and you can find detailed instructions for patching your copy of dhcpcd here. Unfortunately, the patch has not found its way through the Raspberry Pi Organization's software maintenance system.

For this reason, you may prefer to use the request option instead of the inform option. There are differences between request and inform, but they won't be apparent in most use cases - refer to RFC 2131 for details.

Third choice:

Yeah - that's right - static configuration is the end of the line - the worst choice for almost all use-cases, but preferred by knob-dickers 10:1. If you think that you've found the use-case that requires a static configuration, I think you're probably wrong. But if you're smart enough to recognize that use case, then you're almost definitely well-informed; enough to not be asking the question here, saying "it worked yesterday"!

If you think you've detected a harsh or belligerent tone in this answer, I'll apologize for that now. I enjoy what I do here on SE; I have learned a lot here answering questions and helping users solve problems. But I've answered this question too many times to enjoy it. If that shows, I'll apologize, but the answer will be the same until DHCP is overhauled or replaced with something else.

Seamus
  • 21,900
  • 3
  • 33
  • 70
  • Thanks, Seamus. It would be very helpful if you gave us a pointer to that inform bug, so we can check whether it has been addressed yet. – keshlam Oct 02 '22 at 19:18
  • Also: I note that the dhclient.conf file can have an alias () clause, which supposedly can be used to set up a fixed incoming address alongside the one assigned by dhcpd. How does that compare to inform/request? – keshlam Oct 02 '22 at 19:39
  • Just for context: I'm trying to set up the NodeRed bridge to let the Pi interact with SmartThings. I don't know how to do that without nailing down the addresses at each end of the wire. Unfortunately I can't easily set static addresses in the DHCP server -- my core router is a Cisco box running a version of their firmware which predates their supporting that feature. I can, however, have it leave a range of addresses unclaimed, which I hope will still allow DHCP to set the other parameters. – keshlam Oct 02 '22 at 19:44
  • @keshlam: Yes, the patched dhcpcd correcting the inform bug has finally been released. – Seamus Oct 02 '22 at 21:19
  • @keshlam: Sorry, I don't know what dhclient is... if it's for another DHCP client app, my initial thought would be that two DHCP clients on a single host is usually not a good idea. – Seamus Oct 02 '22 at 21:23
  • @keshlam: If your router doesn't support fixed IP assignments (which seems odd), I'm not sure the inform option will get you where you want to be. You *might* have better luck using the request option. The risk here is that if your RPi host is down for some time, the DHCP server *may* assign your chosen IP address to another client/host. – Seamus Oct 02 '22 at 21:36
  • @keshlam: Your case may be one of those where a fixed IP address is the only viable option. If you elect to go this way, just remember that your RPi host won't communicate with the DHCP server at all. That means that everything you ordinarily get from the DHCP server will now have to come from you: IP address, DNS server, default route, etc. Further questions would probably be best handled in a new question. – Seamus Oct 02 '22 at 21:42
  • 1
    'k. Just for closure, adding inform to the end of dhcpcd.conf does seem to have successfully moved the Pi into the range of addresses not being managed by DHCP. Tnx! – keshlam Oct 02 '22 at 21:47
1

I agree that How do I set up networking/WiFi/static IP address on Raspbian/Raspberry Pi OS? has the full answer. However, that answer is long and has many other important information.

TL;DR

TL;DR means "Too long, didn't read." This answer is a summery of information provided elsewhere.

Set via request

This is for the range of IP addresses assigned by the DHCP server. Most DHCP servers assign IP addresses from a defined range, say between 192.168.0.100 to 192.168.0.249. The request option politely asks the DHCP server to assign a specific address, say 192.168.0.120, within that range. The DHCP server may ignore the "request" if that IP address is already assigned to another device.

Set via inform

This option is for setting a static IP address outside the DHCP server range. Continuing with the example range of 192.168.0.100 to 192.168.0.249, setting the static IP address of 192.168.0.20 would be outside the DHCP server range.

Note, there is a bug that may fill up your Pi's MicroSD card with log entries if you use this option. See huge syslog filled with dhcpcd lines and Why does dhcpcd flood my logs with with useless messages when I use the `inform` option?

Set via static ip

This is similar to the inform option, but it is more primitive. That is, it works even when there is no DHCP server at the other end. For example, if you connect two Raspberry Pies directly with an Ethernet cable, and neither of the Pies have a DHCP server installed, you can use this option to assign static IP addresses to both the Pies.

What is a DHCP Server?

For most home network the DHCP server is the WiFi Router. You have to go into the "Advanced Setup" of the router using the web interface to see it. For example:

enter image description here

Your router's setup may look different.

Note, if you plan to use a mobile phone as a WiFi access point, then none of these options may work. A mobile phone's WiFi AP has a DHCP server but it does not follow the rules of a standard DHCP server and does not have any "out of range" addresses for static IP address assignments and does not honor any "requests".

A Router based option

You can reserve a specific IP address within the dynamic address range from within the DHCP server in the router. For this you will need to know the MAC address of the Ethernet (or WiFi) interface of the specific Pi you are trying to reserve an IP address for. For example:

enter image description here

My router allows scanning for MAC addresses and that may help you identify the Pi's Ethernet (or WiFi) interface's MAC address.

Note, if you do this, you will have to select IP addresses within the dynamically allowable IP address range, say 192.168.0.100 to 192.168.0.249.

Hope this helps

user68186
  • 494
  • 6
  • 15
0

@Seamus, I don't know if my use case requires static IP addresses, but it's something that I'm willing to consider a personal requirement.

One of my Pi's is set up with various monitors. One of these looks for unknown IP addresses twice a day. I have a text file with the IP addresses of all the devices on my network. (And it's fairly long.) My monitor uses nmap to find all active IP's, and sends me an email if one (or more) active IP isn't in that list. I set this up to protect against someone using my home network without my knowledge, but there were too many legitimate addresses to do it manually. (It took me a couple of weeks to figure out what some of the addresses belonged to.)

In fact, it was this monitor that pointed out that my Pi had 3 addresses: 1 wireless and 2 hardwire. Which led me to this post, to figure out what was going on. I had set up all my computers (Windows, Raspberry pi, Ubuntu) with fixed addresses. I guess I'll switch to having the router assign addresses based on MAC addresses.

bdixon
  • 13
  • 3