大网数据广告活动
服务器运维问答

Using dhcp and static ip on same nic on linux with systemd-networkd

· 52CE 我爱测

I have a portabel device that sometimes needs connection to internet(to update) but will mostly be used directly connected to a laptop via ethernet cable. I want my users to have the plug and play benefits from dhcp ip when conne…

问题原文

原帖链接 · Hampus Andersson · CC BY-SA 3.0

I have a portabel device that sometimes needs connection to internet(to update) but will mostly be used directly connected to a laptop via ethernet cable. I want my users to have the plug and play benefits from dhcp ip when connected to a router but still be able to reach the device when no dhcp server is involved.

I basically want to do this but with 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

How do i achieve this with systemd-networkd and is there a better way to achieve same result?

社区高分解答

原帖链接 · Simon Schürg · CC BY-SA 4.0

You can enable DHCP on a network interface and also set additional IPv4/IPv6 addresses.

This is an example configuration: /etc/systemd/network/eth0.network

[Match]
Name=eth0

[Network]
DHCP=yes

[Address]
Address=192.168.0.130/24

It results in:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:8d:c0:32 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 10.0.2.15/24 brd 10.0.2.255 scope global dynamic eth0
       valid_lft 86357sec preferred_lft 86357sec
    inet6 fe80::a00:27ff:fe8d:c04d/64 scope link 
       valid_lft forever preferred_lft forever

192.168.0.130 is the static IPv4, 10.0.2.15 the IPv4 from DHCP.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-12-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。