Using dhcp and static ip on same nic on linux with systemd-networkd
I managed to make this work with the following configuration:
/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
It is a little tricky as "ifconfig" does not show DHCP address for me, but if I look at journalctl or use "ip addr show" the DHCP address is there.
# 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)
But:
# 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