0

I would like to disable running DHCP service using Linux command lines, inorder to use my own DHCP client or set static IP addresses.

I know that as mentioned in this post, by setting interface type to static instead of dhcp in /etc/network/interfaces, DHCP is disabled for the concerned interface.

But I would like to make a shell script that uses command lines to do this and the reverse.

1

By manipulating the ip address of an interface while it is configured to use dhcp, the risk is that your static ip is regularly override.

You then have to kill the dchclient related to your interface (htop and kill). However, it seems that the dhclient store the pid of its instance in a default directory : /var/run/dhclient.{interface}.pid. You can then do :

ifce=eth0
kill -9 $(cat /var/run/dhclient.${ifce}.pid
sudo ifconfig $ifce [IP] netmask [NETMASK]

and to re-enable dhcp:

ifce=eth0
dhclient -pf /var/run/dhclient.${ifce}.pid $ifce

EDIT

It seems that the network manager monitors the dhclient instances. You may then provide another configuration to the manager.

  1. remove all references to your interface in the /etc/network/interfaces

  2. add the following line to the end of /etc/network/interfaces

    source /etc/network/interfaces.d/*.conf

  3. create the directory /etc/network/interfaces.d if it does not exist and the following two files:

    • /etc/network/interfaces.d/{interface}.dhcp that contains the dhcp configuration of your interface
    • /etc/network/interfaces.d/{interface}.static that contains the static configuration of your interface
  4. you can then swith from dhcp to static and vice versa by copying file to /etc/network/interfaces.d/{interface}.conf and restarting the interface, either

    ifce=eth0
    cp /etc/network/interfaces.d/${ifce}.static /etc/network/interfaces.d/${ifce}.conf
    ifdown $ifce
    ifup $ifce
    

    or

    ifce=eth0
    cp /etc/network/interfaces.d/${ifce}.dhcp /etc/network/interfaces.d/${ifce}.conf
    ifdown $ifce
    ifup $ifce
    
  • I tried killing the service as you mentioned, but I found that it is restarted again with another pid – ARM Sep 13 '17 at 13:38
  • I didn't know that the network manager monitors dhcp instances. I edit the the reply to provide you another solution. – vera Sep 13 '17 at 13:54
1

Assuming you have deactivated the NetworkManager already for desired interfaces, so you could use the command 'ip'.

Manually assigment (let's say your interface is eth0):

ip link set eth0 up
ip addr add 192.168.0.2/255.255.0.0 broadcast 192.168.255.255 dev eth0
ip route add default via 192.168.0.1

Dynamically assigment:

ip link set eth0 up
dhclient eth0

ip addr command accepts replace and change sub-commands also and you could use label SOMETHING to name your device in human-readable form. So it should help to make the shell script like you need.

Further reads:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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