1

I have connected a USB network interface to my RPi 4 running Raspbian 10 Lite (headless) and I am trying to assign a fixed IP address to it. The onboard eth0 interface should continue to use DHCP as per default.

I tried the following, all of which failed:

  1. Add these lines to /etc/dhcpcd.conf + reboot:
interface eth1
static ip_address=192.168.60.10/24
  1. Add /etc/network/interfaces.d with the following content + reboot:
interface eth1
static ip_address=192.168.60.1/24
  1. Add the above lines directly to /etc/network/interfaces and reboot.

In all cases, eth1 remains unconfigured (no IP address), as witnessed by the output of ifconfig eth1:

eth1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 00:e0:4c:39:bf:d8  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

The only thing that seems to work is sudo ifconfig eth1 192.168.60.10/24, but then the result is not persistent, and adding this command to /etc/rc.local feels like a hack.

What is the correct way to do this?

eth1 should automatically get this fixed IP address when the USB network interface is plugged in.

EDIT: After some experiments triggered by suggestions from @seamus, I realized that dhcpcd does assign the requested address to eth1, but only when a carrier is detected, i.e. when eth1 is physically connected to another device using an ethernet cable. This answers my original question.

There is an alternative way to do this, also suggested by @seamus: instead of static, use inform in dhcpcd.conf. This has the added advantage that a dhcp server running on the 192.168.60.0/24 subnet is informed of the fixed IP, avoiding potential conflicts. In my case, there is no dhcp server, so this is irrelevant.

There is one remaining issue: connections to other hosts on the 192.168.60.0/24 subnet are not routed to eth1. Since this is a different question, not about assigning a fixed IP, I posted a separate question: How to setup point-to-point ethernet connection to PLC in addition to standard network

ygramoel
  • 143
  • 1
  • 6
  • You may find that things work much better when you read and follow suggestions in the documentation. For example, man dhcpcd.conf says "For IPv4, you should use the inform ipaddress option instead of setting a static address." See these other Q&A: 1, 2, 3 – Seamus Jun 17 '21 at 15:49
  • @Seamus You are right: inform 192.168.60.10/24 works; thanks. I don't understand how: according to the dhcpcd documentation, inform sends a message to the DHCP ser4ver, but there is no DHCP server on the network connected to eth1. It is intended to be a point-to-point connection, where the other host also has a fixed address. Also, after reading your references, I still don't understand why static does not work. There is no need for DNS on eth1, and the gateway is only reachable via eth0. – ygramoel Jun 17 '21 at 18:13
  • @Seamus Yes it is; this is for the same project. I felt that this question is answered (even if only in the comments) and that the other question is really a different issue, so I didn't want to mix the two. – ygramoel Jun 18 '21 at 07:31
  • I would still like to understand why the static address did not work. Any link to an explanation? – ygramoel Jun 18 '21 at 07:33
  • Maybe - but on the other hand, there is no need for a gateway address on eth1, because there is no gateway. I have worked with fixed IP addresses in similar configurations many years ago before the introduction of dhcpcd (i.e. by adding the fixed IP in /etc/network/interfaces) , and as far as I remember, this worked well: the interface did get the requested IP address, and routing worked without further intervention. – ygramoel Jun 19 '21 at 11:34
  • Sure, I can try that. What do I need to do to properly configure my static address in dhcpcd.conf, for a network without DHCP server? I still have no clue what was wrong with my original configuration. – ygramoel Jun 21 '21 at 07:03
  • The example you refer to - like the other explanations you referred to earlier - assumes that the configured interfaces (eth0 and wlan0 in that case) are intended for internet access. In that case, it makes perfect sense to configure DNS and a default gateway. That is however not my situation. In my case, there is no internet access via eth1, and there is no gateway and no DNS on the network connected to eth1. – ygramoel Jun 23 '21 at 20:25
  • What I want to achieve is a network with just two fixed IP addresses (one for the RPi and one for the PLC). This is something I have done a lot before the time of dhcpcd, typically to connect to a microcontroller or some other hardware device that is not intended to work on a general network. I am absolutely not the only person in the world doing something like that; it was quite common, at least in the kind of places where I used to work. – ygramoel Jun 23 '21 at 20:26
  • . Configuration in /etc/network/interfaces used to work fine, and automatically created the necessary entries in the routing table. My impression is that dhcpcd was not designed to handle that case, so requires "tricks" (like using inform instead of static) plus manual configuration for routing. – ygramoel Jun 23 '21 at 20:26
  • @Seamus I didn't realise that you are only notified if I mention you, will remember that. Your input has been very helpful; if you post the 'inform' solution, I will accept it. I will do a little more testing, and try to contact Roy Marples if that doesn't help. – ygramoel Jun 24 '21 at 15:12
  • @Seamus Results of my testing are interesting; I edited my question above to include them. – ygramoel Jun 24 '21 at 15:48
  • Your results are interesting, and it seems you're making good progress. It never occurred to me that the lack of a carrier might be the problem as I assumed it was always connected :-P BTW, that's covered up front in man dhcpcd, but is somewhat hidden under the noup option in man dhcpcd.conf! – Seamus Jun 25 '21 at 06:36
  • BTW, I'm going to delete a few of my comments here as they are now superfluous & possibly confusing. – Seamus Jun 25 '21 at 06:37

2 Answers2

1

Please refer to the system manual for dhcpcd - man dhcpcd.conf says:

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

Also see these other Q&A: 1, 2, 3

Seamus
  • 21,900
  • 3
  • 33
  • 70
0

The correct answer, if you don't want an interface to have a gateway is to tell dhcpcd not to assign one to an interface.

If you want an interface to not install any default routes (often used in conjunction with a static IP address) specify

nogateway

See Prevent dhcpcd from configuring a gateway on an interface in How to set up networking/WiFi.

Incidentally unless you use Predictable Network Interface Names results will be unreliable as there is a race condition in interface enumeration.

Milliways
  • 59,890
  • 31
  • 101
  • 209