I have a Raspberry Pi 4 and a Raspberry Pi 0. The RPi0 is connected via the data USB port to the RPi4 (and otherwise doesn't have any connections), and the RPi4 has an Internet connection so is remotely accessible. I am trying to set up the RPi0 as a USB ethernet gadget: on the RPi4, I have Nginx set up as a reverse proxy, and the goal is so I can use the connection to the RPi4 to access a web panel hosted on the RPi0. This requires a specific IP address, 10.0.0.1
, to be assigned to the usb0
interface on the RPi4, so that the panel on the RPi0 will be accessible to Nginx at http://10.0.0.2
. When I do this by running the command ip addr add 10.0.0.1/255.255.255.0 dev usb0
on the RPi4, it works great, but this does not persist across restarts. By default, it seems like the address 192.168.7.2
is assigned to usb0
until I add 10.0.0.1
with that command.
I'm on Raspbian GNU/Linux 10 (buster), and I want to make this configuration persist across restarts.
So far, I've tried these (all attempted on the RPi4):
- Adding to
/etc/network/interfaces
-- seemed to do absolutely nothing. I also tried creating a file ininterfaces.d
folder which also didn't work.
This is what I added:
allow-hotplug usb0
iface usb0 inet static
address 10.0.0.1
netmask 255.255.255.0
gateway 10.0.0.1
dns-nameservers 8.8.8.8
- Adding to
/etc/dhcpcd.conf
-- also did nothing. Then I tried some guide that told me to set dhcpcd to start on startup, which broke networking altogether until I disabled it again.
I added this to the very end of that file:
interface usb0
static ip_address=10.0.0.1/24
static routers=10.0.0.1
static domain_name_servers=1.1.1.1 1.0.0.1
- Adding to
/etc/dnsmasq.d
the following:
Added in a new file,
dhcp-range=usb0,10.0.0.2,10.0.0.2,2m
listen-address=10.0.0.1
- Various guides to add a script containing the
ip addr add 10.0.0.1/255.255.255.0 dev usb0
command to be run at startup -- also doesn't seem to work. I also would prefer to do it the "correct" way anyways.
Most other guides I've seen suggest using NetworkManager but I am using the lite distro without a desktop so I don't think this is an option.
How do you do this? Is there a trick to this that is specific to Raspbian?
ip addr add ...
to root'scrontab
under an@reboot
specification. – Seamus Aug 08 '21 at 06:24@reboot ip addr add 10.0.0.1/255.255.255.0 dev usb0
– ieatpizza Aug 08 '21 at 06:32sudo crontab -e
; in the editor, add the line@reboot sleep 10; /sbin/ip addr add 10.0.0.1/255.255.255.0 dev usb0 >> /home/pi/netchange.txt 2>&1
. This should cover all contingencies & log any errors to the named file in pi's home directory – Seamus Aug 08 '21 at 07:39/etc/dhcpcd.conf
take all of your changes out of/etc/network/interfaces
. One or the other, not both. If you want to do it in/etc/network/interfaces
you have to adddenyinterfaces usb0
to/etc/dhcpcd.conf
. – Dougie Aug 08 '21 at 17:37