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

13
  • 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
    Commented Jun 17, 2021 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
    Commented Jun 17, 2021 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
    Commented Jun 18, 2021 at 7:31
  • I would still like to understand why the static address did not work. Any link to an explanation?
    – ygramoel
    Commented Jun 18, 2021 at 7: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
    Commented Jun 19, 2021 at 11:34

2 Answers 2

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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