17

I have two interfaces on my computer, eth0 and eth1.

Both subnets have a DHCP server on them. How could I use dhclient to renew IP address for both interfaces?

If I do:

dhclient -r && dhclient eth0

It renews the IP on eth0. Then, if I want to just do:

dhclient eth1

It fails with error:

dhclient(22421) is already running - exiting.

I can do:

dhclient -r && dhclient eth1

But the dhclient -r part releases the lease and I loose the IP address on eth0 as well.

For now, the only way I've managed to do what I want is to run dhclient for eth0, brutally kill it, and run it on eth1: I end up with a renewed IP on both interfaces.

I also tested with dhclient -x instead of dhclient -r, no luck.

Surely, this is not the way to go!

EDIT:

Two answers suggested doing dhclient -r eth0 eth1 && dhclient eth0 eth1 This, indeed, limits the broadcast calls to these interfaces. But the first DHCP server to answer will be the only one used, hence resulting in a new IP on a single interface and not both. See here, we can see DHCPDISCOVER calls on both interfaces, but the DHCPREQUEST is only done on eth0, first interface through which a DHCP server was found:

Listening on LPF/eth0/0a:00:27:00:00:00
Sending on   LPF/eth0/0a:00:27:00:00:00
Listening on LPF/eth1/84:8f:69:d3:d4:a1
Sending on   LPF/eth1/84:8f:69:d3:d4:a1
Sending on   Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 5 (xid=0x170fcb67)
DHCPDISCOVER on eth1 to 255.255.255.255 port 67 interval 4 (xid=0x115a24c3)
DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x170fcb67)
DHCPOFFER from 192.168.1.1
DHCPACK from 192.168.1.1 (xid=0x170fcb67)
bound to 192.168.1.4 -- renewal in 1710 seconds.
slm
  • 363,520
  • 117
  • 767
  • 871
Noé Malzieu
  • 807
  • 3
  • 10
  • 15

3 Answers3

12

Either

dhclient -r && dhclient

or

dhclient -r eth0 eth1 && dhclient eth0 eth1

Edit 1:

Next try: You can probably get rid of the "dhclient(22421) is already running - exiting" error by making one of the instances use different files. Use PATH_DHCLIENT_PID and PATH_DHCLIENT_DB variables (or the eqivalent command line options, see the man page) to separate them.

Hauke Laging
  • 88,146
  • 18
  • 125
  • 174
  • The problem is that with this, dhclient sends the packets to discover dhcp server as broadcast on both interfaces indeed, but stops with FIRST received server. I want to use first server received via eth0 for eth0 ip, and first server received via eth1 for eth1 ip. – Noé Malzieu May 29 '13 at 23:41
  • If I was not clear: it sends DHCPDISCOVER on both interfaces but send DHCPREQUEST only on the first interfaces on which it receives an answer from a dhcp server – Noé Malzieu May 29 '13 at 23:46
  • @NoéMalzieu You should put that information in your question at any rate. Kind of funny: You got a useless answer "worth 11K rep"; that doesn't happen every day :-) – Hauke Laging May 29 '13 at 23:51
  • @HaukeLaging - who's answer are you calling useless 8-). – slm May 29 '13 at 23:53
  • @NoéMalzieu See my edit. – Hauke Laging May 30 '13 at 00:01
  • @HaukeLaging This works! I actually went a bit further with: `dhclient -r && dhclient eth0 --no-pid && dhclient eth1 --no-pid` The `--no-pid` option disables the writing of pid files: is that ok or should I not use it? – Noé Malzieu May 30 '13 at 00:22
  • @NoéMalzieu - The pid files only matter if you want to systematically kill it using your OSes tools. Should be OK. – slm May 30 '13 at 00:26
  • @HaukeLaging - Where'd you find the reference to those variables? The .c files or somewhere else? I only found them so far in the .c files. – slm May 30 '13 at 00:29
  • @NoéMalzieu The PID files make using the program more comfortable but, of course, you must decide what fits your needs best. – Hauke Laging May 30 '13 at 00:36
  • The env. variables references by @HaukeLaging are referenced in the dhclient.c source here: https://github.com/glance-/dhcp/blob/master/client/dhclient.c – slm May 30 '13 at 00:37
  • @slm That's simply part of my man page. In the one and only, best of all distros... Maybe this is BSD software and thus they are more up-to-date? http://www.manualpages.de/FreeBSD/FreeBSD-ports-9.0-RELEASE/man8/dhclient.8.html – Hauke Laging May 30 '13 at 00:38
  • @HaukeLaging - That's a pretty nice man page, thanks. – slm May 30 '13 at 00:52
4

Idea #1: Try passing 2 interfaces to dhclient

If you look at the man page for dhclient you'll notice that at the end of the example usage the bit [ if0 [ ...ifN ]. This would imply that you can run dhclient and pass it multiple interfaces that you want to run on.

excerpt from the dhclient man page

dhclient [ -4 | -6 ] [ -S ] [ -N [ -N... ] ] [ -T [ -T... ] ] [ -P [ -P... ] ] [ -p port ] [ -d ] [ -e VAR=value ] [ -q ] [ -1 ] [ -r | -x ] [ -lf lease-file ] [ -pf pid-file ] [ -cf config-file ] [ -sf script-file ] [ -s server ] [ -g relay ] [ -n ] [ -nc ] [ -nw ] [ -w ] [ -B ] [ -I dhcp-client-identifier ] [ -H host-name ] [ -F fqdn.fqdn ] [ -V vendor-class-identifier ] [ -R request-option-list ] [ -timeout timeout ] [ -v ] [ --version ] [ if0 [ ...ifN ] ]

So you should be able to get dhclient to release on multiple interfaces and connect on them as well.

dhclient -r eth0 eth1
dhclient eth0 eth1

Idea #2: Look at how NetworkManager does it

Here's how NetworkManager sets up dhclient on my system. You could try calling the second dhclient explicitly with arguments.

/sbin/dhclient -d -4 \
   -sf /usr/libexec/nm-dhcp-client.action \
   -pf /var/run/dhclient-wlan0.pid \
   -lf /var/lib/dhclient/dhclient-01f0476d-04bf-4ce7-8ced-844fae73f430-wlan0.lease \
   -cf /var/run/nm-dhclient-wlan0.conf wlan0
slm
  • 363,520
  • 117
  • 767
  • 871
0

Found a Solution.. One has to write explicit dhclient-conf..

Now, call it with:

dhclient -cf /etc/dhcp/dhclient.conf -v vmbr1 --no-pid

dhclient -cf /etc/dhcp/dhclient-vmbr3.conf -v vmbr3 --no-pid

Comment the standard paragraph. If you add new interfaces, then there won't be a dhcp request if you dont explicitly clarify it here.

:/etc/dhcp# cat dhclient.conf

interface "vmbr1" {
send host-name = gethostname();
select-timeout 3;
request subnet-mask, broadcast-address, time-offset, routers,
        domain-name, domain-name-servers, domain-search, host-name,
        dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers,
        netbios-name-servers, netbios-scope, interface-mtu,
        rfc3442-classless-static-routes, ntp-servers;
}

#send host-name = gethostname();
#request subnet-mask, broadcast-address, time-offset, routers,
      #  domain-name, domain-name-servers, domain-search, host-name,
      #  dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers,
     #   netbios-name-servers, netbios-scope, interface-mtu,
      #  rfc3442-classless-static-routes, ntp-servers;

:/etc/dhcp# cat dhclient-vmbr3.conf

option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;

interface "vmbr3" {
send host-name = gethostname();
request subnet-mask, broadcast-address, time-offset, routers,
        domain-name, domain-name-servers, domain-search, host-name,
        dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers,
        netbios-name-servers, netbios-scope, interface-mtu,
        rfc3442-classless-static-routes, ntp-servers;
}

I assume it's due to the dhcp state machine that one dhclient can't serve both interfaces?

  • Congratulations on “finding” a solution that was already outlined in previous comments. BTW, it appears that one ```dhclient``` *can* manage multiple interfaces, but it cannot interact with multiple servers, which is part of the OP’s requirement. – G-Man Says 'Reinstate Monica' May 12 '22 at 14:58
  • Hmm u mean the one by Noé Malzieu May 30, 2013 at 0:22? My solution is different. The conf file has the standard paragraph commented out. Now, it is working independently on two nic's with two different dhcp server. – MightyMirko May 13 '22 at 06:59