- isc-dhcp-server disabling and configuring
- 2 Answers 2
- Is there a safe way to disable DHCP from command line?
- Background
- Despair
- j-keck / ubuntu: disable dhcp from dnsmasq-base
- Включить / отключить запуск службы DHCP с помощью командной строки Linux
- 2 ответа 2
- Enable/Disable running DHCP service using Linux command lines
- 2 Answers 2
isc-dhcp-server disabling and configuring
I’ve installed isc-dhcp-server just for a one-time task. So basically, after task is completed I dont’t need it anymore: should I just uninstall it? Or it possible to disable it completely? The command sudo service isc-dhcp-server stop seems don’t prevent restarting isc-dhcp service in background, after PC rebooted. Also, there was a lot of modifications in dhcpd.conf, also was assigned static IP to /etc/network/interfaces config: should edit changes back in interfaces config, as don’t need it anymore, or this changes are not persistent? Also, I forgot to backup the dhcpd.conf: how to get unchanged one?
2 Answers 2
It seems that you want to prevent running DHCP server after reboot. to do this action, you must update init scripts by update-rc.d :
sudo update-rc.d -f isc-dhcp-server remove
If you want to add DHCP Server to startup again, enter this command:
sudo update-rc.d isc-dhcp-server defaults
So it removes DHCP from system startup. If in the future I will need ISC-DHCP server again, how I can enable it? Regarding static IP that was set in /etc/network/interfaces : should I change config back?
@minto your change to /etc/network/interfaces must be reverted back because when you reboot your system, it’s uses /etc/network/interfaces to bring up your interfaces.
for ubuntu 16.04 and 14.04 this command :
sudo update-rc.d -f isc-dhcp-server remove
will not stop the service to start at boot time. How to check that ?
sudo service isc-dhcp-server status ● isc-dhcp-server.service - ISC DHCP IPv4 server Loaded: loaded (/lib/systemd/system/isc-dhcp-server.service; enabled; vendor preset: enabled) Active: inactive (dead) Docs: man:dhcpd(8)
Despite the fact that the start and stop links on all rc.d levels are removed the service will still be started at boot time. It is still enable and start script in /etc/default/isc-dhcp-server will be run at boot time starting the service. To stop service loading at boot time use command:
sudo systemctl disable isc-dhcp-server.service
Now You can check the status :
sudo service isc-dhcp-server status ● isc-dhcp-server.service - ISC DHCP IPv4 server Loaded: loaded (/lib/systemd/system/isc-dhcp-server.service; disabled; vendor preset: enabled) Active: inactive (dead) Docs: man:dhcpd(8)
Now you see the service is disabled at boot time.
Is there a safe way to disable DHCP from command line?
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 ):
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: «
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.
j-keck / ubuntu: disable dhcp from dnsmasq-base
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
dnsmasq from package ‘dnsmasq-base’ starts with a (from libvirt) generated configuration: /var/lib/libvirt/dnsmasq/default.conf. |
this configuration file is not directly editable because it gets overwritten from libvirt. |
there is no possibility to set ‘no-dhcp-interface=eth0’ per ‘libvirt’. |
# ########################################################### |
# deactivate dhcp in dnsmasq per libvirt |
# dnsmasq listen on port 53 (dns) and 67 (dhcp) |
j@ubuntu:~$ sudo netstat -taupen | grep -E ‘:53|:67’ |
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 0 9817 1218/dnsmasq |
udp 0 0 192.168.122.1:53 0.0.0.0:* 0 9816 1218/dnsmasq |
udp 0 0 0.0.0.0:67 0.0.0.0:* 0 9813 1218/dnsmasq |
# dump virsh network config |
virsh net-dumpxml default > default-net-org.xml |
# remove dhcp section in config |
sed ‘//,//d’ default-net-org.xml > default-net-without-dhcp.xml |
# load new config |
virsh net-define default-net-without-dhcp.xml |
# restart network (regenerates /var/lib/libvirt/dnsmasq/default.conf) and restart dnsmasq |
virsh net-destroy default |
virsh net-start default |
# dnsmasq listen only on port 53 (dns) |
j@ubuntu:~$ sudo netstat -taupen | grep -E ‘:53|:67’ |
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 0 9673 1202/dnsmasq |
udp 0 0 192.168.122.1:53 0.0.0.0:* 0 9672 1202/dnsmasq |
# ########################################################### |
# reactivate dhcp in dnsmasq per libvirt |
# load original config |
virsh net-define default-net-org.xml |
# restart network (regenerates /var/lib/libvirt/dnsmasq/default.conf) and restart dnsmasq |
virsh net-destroy default |
virsh net-start default |
Включить / отключить запуск службы DHCP с помощью командной строки Linux
Я хотел бы отключить запуск службы DHCP с помощью командной строки Linux, чтобы использовать собственный DHCP-клиент или установить статические IP-адреса. Я знаю, что, как уже упоминалось в этом посте, установив статический тип интерфейса вместо dhcp в /etc /network /interfaces, DHCP отключается для соответствующего интерфейса. Но я хотел бы сделать сценарий оболочки, который использует командные строки, чтобы сделать это и наоборот.
2 ответа 2
Предполагая, что вы уже деактивировали NetworkManager для нужных интерфейсов, вы можете использовать команду «ip». Назначение вручную (скажем, ваш интерфейс — 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
ip link set eth0 up dhclient eth0
Манипулируя IP-адресом интерфейса, в то время как он настроен на использование dhcp, существует риск того, что ваш статический ip будет регулярно переопределяться.
Затем вам нужно убить dchclient, связанный с вашим интерфейсом (htop и kill). Однако, похоже, что dhclient хранит pid своего экземпляра в каталоге по умолчанию: /var/run/dhclient..pid . Затем вы можете сделать:
ifce=eth0 kill -9 $(cat /var/run/dhclient.$.pid sudo ifconfig $ifce [IP] netmask [NETMASK]
ifce=eth0 dhclient -pf /var/run/dhclient.$.pid $ifce
РЕДАКТИРОВАТЬ
Кажется, что сетевой менеджер контролирует экземпляры dhclient. Затем вы можете предоставить другую конфигурацию менеджеру.
- удалите все ссылки на ваш интерфейс в /etc /network /interfaces
- добавьте следующую строку в конец /etc /network /interfaces source /etc/network/interfaces.d/*.conf
- создайте каталог /etc/network/interfaces.d если он не существует, и следующие два файла:
- /etc/network/interfaces.d/ndominterface‹.dhcp, который содержит конфигурацию dhcp вашего интерфейса
- /etc/network/interfaces.d/ndominterface‹.static, который содержит статическую конфигурацию вашего интерфейса
- Затем вы можете переключиться с dhcp на статический и наоборот, скопировав файл в /etc/network/interfaces.d/.conf и перезапустив интерфейс, либо
ifce=eth0 cp /etc/network/interfaces.d/$.static /etc/network/interfaces.d/$.conf ifdown $ifce ifup $ifce
ifce=eth0 cp /etc/network/interfaces.d/$.dhcp /etc/network/interfaces.d/$.conf ifdown $ifce ifup $ifce
Enable/Disable running DHCP service using Linux command lines
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.
2 Answers 2
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..pid . You can then do :
ifce=eth0 kill -9 $(cat /var/run/dhclient.$.pid sudo ifconfig $ifce [IP] netmask [NETMASK]
ifce=eth0 dhclient -pf /var/run/dhclient.$.pid $ifce
It seems that the network manager monitors the dhclient instances. You may then provide another configuration to the manager.
- remove all references to your interface in the /etc/network/interfaces
- add the following line to the end of /etc/network/interfaces source /etc/network/interfaces.d/*.conf
- create the directory /etc/network/interfaces.d if it does not exist and the following two files:
- /etc/network/interfaces.d/.dhcp that contains the dhcp configuration of your interface
- /etc/network/interfaces.d/.static that contains the static configuration of your interface
- you can then swith from dhcp to static and vice versa by copying file to /etc/network/interfaces.d/.conf and restarting the interface, either
ifce=eth0 cp /etc/network/interfaces.d/$.static /etc/network/interfaces.d/$.conf ifdown $ifce ifup $ifce
ifce=eth0 cp /etc/network/interfaces.d/$.dhcp /etc/network/interfaces.d/$.conf ifdown $ifce ifup $ifce