Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

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
share|improve this answer
    
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 at 21:14

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.

share|improve this answer
    
But actually your answer is missing the point. I assume my question wasn't clearly written, but I modified it. –  Mister Henson Aug 1 '13 at 17:58

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

share|improve this answer
    
Thanks, I tried this and deleted the lease files, but somehow it doesn't work (dhclient seems to be restarted)... –  Mister Henson Aug 6 '13 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 '13 at 11:04

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.