Is there a safe way to disable DHCP from command line?
Solution 1
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:
- You're not using network-manager
- You're not using
resolvconf
, but static /etc/resolv.conf file - 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
Solution 2
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
Related videos on Youtube
Mister Henson
Updated on September 18, 2022Comments
-
Mister Henson over 1 year
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
dhcpdisabledBut 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 bemy 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.
-
John almost 8 yearsFor visitors, you don't need to pkill or remove anything. A better answer can be found here.
-
Mister Henson almost 8 yearsJohn, 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.
-
-
Mister Henson over 10 yearsThanks, I tried this and deleted the lease files, but somehow it doesn't work (dhclient seems to be restarted)...
-
Clausi over 10 yearsSo 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!
-
Nadeem over 10 yearsI 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?
-
Mister Henson almost 8 yearsThx but Ubuntu was used...
-
Sahil Singh almost 8 yearsJust 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.