9

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?

5
  • 1
    That... really doesn't make sense. If you use a static address, then it's not plug&play anymore, since different LANs have different subnets and the static address wouldn't work in many of them. Commented Nov 27, 2015 at 11:06
  • I figured if the user is not using 192.128.0.x like most routers are configured. Then they probably also know how to find the dhcp ip thru the router. Once they have the ip they can connect to the device to change the static ip. The other reason i want a static ip is becaus i want the user to have the option to connect directly to a laptop with no dhcp server involved. Commented Nov 27, 2015 at 11:13
  • 2
    Could you try to clarify your question? What do you mean by "plug and play device", and is this the same device you want your users to connect to? Also, I don't understand what you mean when you say that you "don't want the users to connect.. without going through the hassle". So you want them to go through the hassle? In the end, I couldn't really figure out what it is you actually want to achieve (apart from the sentence that you wrote in the subject of your question).
    – vic
    Commented Nov 27, 2015 at 12:17
  • Is my question clear enough now? Commented Dec 2, 2015 at 16:25
  • I want something similar, but for a very different reason. I have an EC2 instance that has a secondary VIP assigned to it. It gets its primary IP from DHCP but I want to configure it to also be able to handle traffic it gets from the secondary ip. If I add the ip with ip addr add, then if systemd-networkd restarts I lose the secondary ip.
    – Thayne
    Commented Mar 24, 2021 at 18:06

2 Answers 2

10

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
1
  • Thanks! Looks exactly like what I was looking for. Will try tomorrow and report my results. Commented Dec 2, 2015 at 18:43
0

You can configure dhclient.conf as follows:

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;
}

This will configure your dhcp to timeout after 10 seconds of not getting an IP address from a DHCP server and give the address 10.0.0.10 on a /24 network. You can also set DNS servers in the configuration as well. Here's a link to the man page that I got this configuration from

http://www.freebsd.org/cgi/man.cgi?query=dhclient.conf&sektion=5&n=1

1
  • 2
    User asked about systemd configuration explicit. Systemd does not use dhclient.
    – RDP
    Commented Dec 2, 2015 at 18:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .