4

Short version

I know this question is very similar to others and I did really search the Internet for answers, but it seems that no approach is working or the solutions seem to be "to complicated to be best practice" (I'll try to explain). I'm looking for a safe way on how to disable DHCP from command line (to use in scripts).

Background

I'm trying to create a "Live-Cd" with Ubuntu Server 12.04 and remastersys (works well). The system is, by default, configured to get the IP with DHCP while booting. That's ok - the important thing is that a script (which runs after booting) is able (in some specific cases) to set a static IP using ifconfig (not /etc/network/interfaces ):

ifconfig eth0 192.168.0.1

Actually this is also working, but the IP only persists until the DHCP lease time of the previously obtained lease is over. A new IP will be assigned to eth0, which (in short) breaks the system. I thought "no big deal, there will be surely something like: "

ifconfig eth0 dhcpdisabled

But I ended up recognizing, it doesn't work that easy. Editing the /etc/network/interfaces is a bit tricky, because it is generated by remastersys (actually the 23networking script of casper).

I could rewrite it and restart networking, but what will happen to active dhclient.leases? Will be DHClient still be running in the background (it shouldn't but some posts suggest it will be my tests showed it is restarted “randomly“, if dhclient is killed), do I have to remove or empty dhclient.leases files, what about resolv.conf?

Despair

To sum it up - In my opinion it's not really that "straightforward" and somewhat doesn't "feel stable". This question (156183) suggested to remove the dhclient package, but: Will this solve the problem? Will this break other stuff?

Is there really no command for this? I hope I just overlooked it.

2
  • For visitors, you don't need to pkill or remove anything. A better answer can be found here.
    – John
    Jun 23, 2016 at 22:34
  • John, I needed this back that days for a script. How would this work here? Pipe the file and doing a string replace? Doesn't sound mature to me. Jun 24, 2016 at 7:19

5 Answers 5

3

I'll try to give you a brief example of what you can do, but it's not complete example because the exact setup, needs etc can differ quite a lot.

First, lets make some assumptions to make this case more simple:

  1. You're not using network-manager
  2. You're not using resolvconf, but static /etc/resolv.conf file
  3. Your interfaces file does not contain allow-hotplug lines to reconfigure if cable is unplugged/plugged.

Let's setup our helper variable DEV

## your device is eth0
# stop /etc/interface control of the eth0 device, should also stop/kill dhcp for that device
ifdown eth0
## stop dhclient to be sure, don't worry about leases files etc
pkill dhclient
## ubuntu precise calls dhclient with dhclient3
pkill dhclient3
# set ip manually: (NETMASK is quite often 24 == 255.255.255.0) 
ifconfig eth0 up 192.168.0.1/NETMASK
# do you need default gw? (for this example it's 192.168.0.254)
route add default gw  192.168.0.254

## if your /etc/resolv.conf is incorrect add correct nameserver like this:
### 8.8.8.8 = goole open dns
echo 'nameserver 8.8.8.8' > /etc/resolv.conf

Now if assumption 1) is false do (stop network-manager):

stop network-manager

For assumption 2) either remove resolvconf or remove /etc/resolv.conf if it's a symlink and replace it with static file

[ -h /etc/resolv.conf ] && rm /etc/resolv.conf

And for assumption 3): This should comment out hotplug lines.. and I hope it's enough

perl -i -pe 's/^(\s*allow-hotplug)/# $1/g' /etc/network/interfaces
1
  • I guess that you don't have to use pkill twice - pkill dhclient should kill both dhclient and dhclient3 - it uses regex. Maybe use killall instead?
    – scdmb
    Jan 15, 2014 at 21:14
1

You can use "systemctl" if available for your distro. It is available for my openSUSE distribution, and I use it to disable the services in the follwoing way.

systemctl disable wickedd-dhcp4.service

where wickedd-dhcp4 is name of the service handling DHCP client configuration on my system. There is also a service for IPv6 dhcp configuration, you migh wan't to disable that too.

systemctl disable wickedd-dhcp6.service
2
  • Thx but Ubuntu was used... Jun 24, 2016 at 7:24
  • 2
    Just to add, if you want to release address obtained by dhcp, after dhcp has run, you can do "dhclient -r eth0" . Not an answer to then main question, but it may help. Jun 25, 2016 at 10:13
0

The easiest standard way to fix this is simple: just tell your DHCP server which IPs should be given to certain MAC addresses. Every router I've ever touched has had this feature and if you're using dhcp3-server, it's just as simple.

1
  • But actually your answer is missing the point. I assume my question wasn't clearly written, but I modified it. Aug 1, 2013 at 17:58
0

What about simply stopping the dhcp-client with pkill dhclient?

2
  • Thanks, I tried this and deleted the lease files, but somehow it doesn't work (dhclient seems to be restarted)... Aug 6, 2013 at 7:57
  • 1
    So we are searching for the process that is restarting the dhclient, right? Just to doublecheck, would you please verify that you don't have network-manager installed? And please post the content of /etc/network/interfaces. Thx!
    – Clausi
    Aug 6, 2013 at 11:04
0

I use the following command to disable DHCP on my server.

sudo update-rc.d -f dhcp3-server remove

What it does is remove any system startup links for /etc/init.d/dhcp3-server.

So that on next reboot DHCP does not kick in.

That's it , simple clean, easy and safe.

You must log in to answer this question.

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