我有一个便携式设备,有时需要连接到互联网(进行更新),但大多数情况下会通过以太网电缆直接连接到笔记本电脑。我希望我的用户在连接到路由器时能够享受 dhcp ip 的即插即用优势,但在没有 dhcp 服务器时仍能够访问设备。
我基本上想这样做,但是使用 systemd-networkd:
auto lo iface lo inet loopback auto eth0 iface eth0 inet dhcp auto eth0:0 iface eth0:0 inet static address 192.168.0.130 netmask 255.255.255.0
我如何使用 systemd-networkd 实现这一点,有没有更好的方法来实现相同的结果?
答案1
我设法通过以下配置使其工作:
/etc/systemd/network/eth0.network
[Match]
Name=eth0
[Network]
DHCP=yes
DNS=8.8.8.8
DNS=8.8.4.4
[Address]
Label=eth0:0
Address=192.168.0.130/24
这有点棘手,因为“ifconfig”没有显示 DHCP 地址,但如果我查看 journalctl 或使用“ip addr show”,DHCP 地址就在那里。
# ifconfig
eth0 Link encap:Ethernet HWaddr xx:xx:xx:xx:xx:xx
inet addr:192.168.0.130 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::f0f7:36ff:fe00:1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2721 errors:0 dropped:92 overruns:0 frame:0
TX packets:947 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:218362 (213.2 KiB) TX bytes:115488 (112.7 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:5680 errors:0 dropped:0 overruns:0 frame:0
TX packets:5680 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:460080 (449.2 KiB) TX bytes:460080 (449.2 KiB)
但:
# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default
link/sit 0.0.0.0 brd 0.0.0.0
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
inet 192.168.0.130/24 brd 192.168.0.255 scope global eth0
valid_lft forever preferred_lft forever
inet 172.16.200.191/24 brd 172.16.200.255 scope global dynamic eth0
valid_lft 85430sec preferred_lft 85430sec
inet6 fe80::f0f7:36ff:fe00:1/64 scope link
valid_lft forever preferred_lft forever
答案2
您可以按如下方式配置dhclient.conf:
timeout 10;
lease {
interface "eth0";
fixed-address 10.0.0.10;
option subnet-mask 255.255.255.0;
}
interface "ep0" {
prepend domain-name-servers 10.0.0.1;
}
这会将您的 dhcp 配置为在 10 秒内未从 DHCP 服务器获取 IP 地址后超时,并在 /24 网络上提供地址 10.0.0.10。您还可以在配置中设置 DNS 服务器。以下是我从中获取此配置的手册页链接
http://www.freebsd.org/cgi/man.cgi?query=dhclient.conf&sektion=5&n=1