Ubuntu – Is there a safe way to disable DHCP from command line

dhcpifconfignetworkingstatic-ip

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.

Best Answer

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
Related Question