无法使用 systemd 在 Ubuntu 上启动 dhcpd

无法使用 systemd 在 Ubuntu 上启动 dhcpd

我在 Ubuntu (x64) 上运行时遇到了一个奇怪的问题dhcpd。我使用它在启动时systemd启动dhcpd,但它就退出了。

当我查询服务状态时

$ systemctl status dhcpd
dhcpd.service - dhcpcd service
 Loaded: loaded (/etc/systemd/system/dhcpd.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Tue 2016-08-16 20:37:11 UTC; 15min ago
Process: 1591 ExecStart=/sbin/dhcpd -cf /etc/dhcp/dhcpd.conf eth1 (code=exited, status=0/SUCCESS)
Main PID: 1591 (code=exited, status=0/SUCCESS)

当我查看日志时,我也没有看到任何错误:

journalctl _PID=1591

Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Internet Systems Consortium DHCP Server 4.3.3-P1
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Internet Systems Consortium DHCP Server 4.3.3-P1
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Copyright 2004-2016 Internet Systems Consortium.
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Copyright 2004-2016 Internet Systems Consortium.
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: All rights reserved.
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: For info, please visit https://www.isc.org/software/dhcp/
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: All rights reserved.
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: For info, please visit https://www.isc.org/software/dhcp/
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Config file: /etc/dhcp/dhcpd.conf
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Config file: /etc/dhcp/dhcpd.conf
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Database file: /var/lib/dhcp/dhcpd.leases
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Database file: /var/lib/dhcp/dhcpd.leases
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: PID file: /var/run/dhcpd.pid
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: PID file: /var/run/dhcpd.pid
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Wrote 0 leases to leases file.
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Wrote 0 leases to leases file.
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Listening on LPF/eth1/08:00:27:10:48:17/192.168.0.0/24
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Listening on LPF/eth1/08:00:27:10:48:17/192.168.0.0/24
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Sending on   LPF/eth1/08:00:27:10:48:17/192.168.0.0/24
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Sending on   LPF/eth1/08:00:27:10:48:17/192.168.0.0/24
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Sending on   Socket/fallback/fallback-net
Aug 16 20:37:11 kirk.sb.computer dhcpd[1591]: Sending on   Socket/fallback/fallback-net

看上去一切正常,但是dhcpd却没有运行。

我的配置文件(/etc/dhcp/dhcpd.conf)非常简单:

# dhcp server configuration file

allow bootp;  #allow bootp protocol

#define DHCP subnet
#specify /etc/spock/spock.ub as boot file
subnet 192.168.0.0 netmask 255.255.255.0 {
    range 192.168.0.2 192.168.0.200;
    filename "/etc/spock/spock.ub";
}

在我的服务脚本中,我确保 dhcpd 在网络启动之前不会启动:

# Start and stop dhcpcd service on eth1 interface

[Unit]
Description=dhcpcd service
# Come up after networking
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
#start dhcpd on eth1
ExecStart=/sbin/dhcpd -cf /etc/dhcp/dhcpd.conf eth1

[Install]
WantedBy=multi-user.target

当我尝试通过启动它时它也会退出,systemctl start dhcpd 但是,如果我'/sbin/dhcpd -cf /etc/dhcp/dhcpd.conf eth1'从命令行执行,它就可以正常工作。

知道它为什么退出吗?

答案1

dhcpd 自行分叉,因此我必须适当地更改服务类型:

[服务]
类型=分叉
ExecStart=/sbin/dhcpd -cf /etc/dhcp/dhcpd.conf eth1

现在它起作用了

答案2

如果需要在网络接口完全正常运行并能够按照建议连接到外界后运行该脚本network-online.target,那么您可能需要先通过 ping google 等来测试连接。

我建议您在命令之前创建一个包含 ping 测试的单独脚本,然后在您的服务中/sbin/dhcpd -cf /etc/dhcp/dhcpd.conf eth1执行该脚本。ExecStart

相关内容