Linux 如果不是从 DHCP 获取的,则设置静态 ip(脚本)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12727175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:30:23  来源:igfitidea点击:

Set static ip if not obtained from DHCP (script)

linuxipdhcpifconfig

提问by Bartlomiej Grzeskowiak

I work on embedded device with linux on it. I want to use DHCP client first, but if there will be no answer from DHCP Server I want to set static-default IP. I suppose it shouldn't be complicated, but I haven't found strict answer.

我在带有 linux 的嵌入式设备上工作。我想先使用 DHCP 客户端,但如果 DHCP 服务器没有响应,我想设置静态默认 IP。我想它不应该很复杂,但我还没有找到严格的答案。

I'm thinking about 2 solutions (Unfortunately I can test them in few days):

我正在考虑 2 个解决方案(不幸的是我可以在几天内测试它们):

  1. I set static IP with ifconfig, then I call udhcpc. If udhcpc will not obtain new IP, old one will stay.

  2. I can also first call udhcpc, wait a while and check if IP is obtained. But this is not nice for me. I wouldn't like to add any wait routines into startup.

  1. 我使用 ifconfig 设置静态 IP,然后调用 udhcpc。如果 udhcpc 不会获得新 IP,则旧 IP 将保留。

  2. 我也可以先调用udhcpc,稍等片刻,查看是否获取到IP。但这对我来说并不好。我不想在启动中添加任何等待例程。

BR Bartek

BR巴特克

I use udhcpc - something like:

我使用 udhcpc - 类似于:

udhcpc -n -f -i eth0 
if ifconfig | grep -A1 eth0 | grep inet 
    then 

采纳答案by Maurizio Lo Bosco

dhclient should support fallback via lease declaration have a look at the dhclient.confman page.

dhclient 应该通过租约声明支持回退,请查看dhclient.conf手册页。

Add something like this to your dhclient.conf

将这样的内容添加到您的 dhclient.conf

timeout 10;
lease {
interface "eth0";
fixed-address 10.0.0.10;
option subnet-mask 255.255.255.0;
renew 2 2022/1/1 00:00:01;
rebind 2 2022/1/1 00:00:01;
expire 2 2022/1/1 0:00:01;
}

or you can assign a second IP to the interface like /etc/network/interfaces

或者您可以为接口分配第二个 IP,例如 /etc/network/interfaces

auto lo
iface lo inet loopback
iface eth0 inet dhcp

auto eth0:1
iface eth0:1 inet static
address 10.10.10.2
netmask 255.255.255.0

回答by Angelo Babudro

Although an old question, it might be worth noting here that Gentoo Linux has had this functionality for a long time (I've been using it since 2004). Gentoo's network config file (/etc/conf.d/net) allows for fallback IP addresses to be easily specified for any interface in the event that DHCP fails, e.g.:

虽然是一个老问题,但在这里可能值得注意的是,Gentoo Linux 已经有这个功能很长时间了(我从 2004 年开始使用它)。Gentoo 的网络配置文件 (/etc/conf.d/net) 允许在 DHCP 失败的情况下为任何接口轻松指定后备 IP 地址,例如:

modules="dhclient"
config_eth0="dhcp"
config_eth1="dhcp"
dhclient_eth1="nogateway"
fallback_eth0="apipa"
fallback_eth1="192.168.10.10/24"
fallback_routes_eth1="default via 192.168.10.1"

The solution Maurizio provided to use an alias like eth0:0 is fine in many (probably most) situations, but not all. I've run into one piece of software that does not consider eth0:0 to be a suitable substitute for eth0 when it is undefined due to no answer from DHCP, even though it is the same port. So a static fallback address is slightly superior to the alias solution.

Maurizio 提供的使用像 eth0:0 这样的别名的解决方案在许多(可能是大多数)情况下都很好,但不是全部。我遇到了一个软件,它不认为 eth0:0 是 eth0 的合适替代品,因为 eth0 由于没有来自 DHCP 的答复而未定义,即使它是同一个端口。所以静态回退地址略优于别名解决方案。