Static IP on wired LAN
I have an Arch Linux machine connected to a LAN by ethernet. The router runs OpenWRT.
DHCP assigns IPs to this machine like 192.168.1.*
- quite typical. I want the IP to always be 192.168.1.10
.
Ideally, I would like to do this through configuration on the machine only, without configuring anything on the router. The machine should know to request 192.168.1.10
from DHCP, rather than relying on DHCP to choose the correct IP address. It doesn't have to be through DHCP either (I don't know that much about how LAN IPs are assigned).
2 answers
There are a couple of ways of handling this (on PC's in general. I do use Linux, but not Arch, and this will work).
The first way is to statically assign an IP address. While you SHOULD be able to assign a static IP address in the DHCP range, it is better to assign it outside the range (the range is configured in the DHCP server - in this case your OpenWRT router). To statically assign an IP address, you need to specify the IP address, the Netmask, the Gateway, and the nameservers.
Here is a link on how to do this in Arch: https://ostechnix.com/configure-static-dynamic-ip-address-arch-linux/
I would argue the better way is to dynamically assign a static IP address. This is not strictly speaking what you asked, but is very relevant. The idea here is to map the MAC address of the Arch Linux LAN Interface to an IP, and have the DHCP server assign it dynamically. The nice thing here is you don't need to make any changes to your Arch box, and if you shift to another network, nothing needs to change to get connectivity.
https://openwrt.org/docs/guide-user/base-system/dhcp_configuration outlines how to do this on OpenWRT.
You can find your Arch box's IP address with a command like
ip addr show
The MAC address is the bit after the words link/ether for the appropriate device. You will need this information to tell OpenWRT's DHCP server how to identify this box (or, strictly speaking, this particular network interface).
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
matthewsnyder | (no comment) | Sep 6, 2023 at 17:47 |
I did this with NetworkManager.
-
nmtui
to open the TUI interface and then select "Edit..." and the ethernet connection- You can also use the equivalent GUI or CLI commands, but I find them harder to use
- Next to
IPv4 Configuration
, clickShow
to see connection details. - Go to
Addresses
,<Add...>
and put in the IP that you want.- I've seen people say you need to put in the Gateway as well but I didn't and it works anyway
- Everything else can be left as is
You may need to disconnect and reconnect.
But ultimately this set up will cause your computer to ask the router for a specific IP (the one you put in). It's then up to the router to decide whether to allow it. I've seen two common reasons why it would decide not to:
- The old IP has not expired yet, so you either have to wait (usually a few minutes or hours) or you have to flush it through the router's interface (for OpenWrt, see https://linux.codidact.com/posts/289617). Restarting the router might work as well.
- The router's range of allowed DHCP addresses doesn't cover your desired address. I'm not really sure if this is always necessary, in the past I've seen some routers grant the IP even though it's outside the DHCP. But if it doesn't work, it's worth trying to expand the DHCP range to include you desired IP.
In theory, there could also be an issue where some other device has already taken the IP you want. There are ways to solve that (which I'll leave to a separate question) but in practice this seems to be very rare.
0 comment threads