在 Ubuntu 20.04 上设置 DHCP 服务器?

在 Ubuntu 20.04 上设置 DHCP 服务器?

我一直在关注Ubuntu 上的教程教我如何在我的机器上设置 DHCP 服务器,但我就是无法让它工作。当我运行

sudo systemctl restart isc-dhcp-server.service
sudo systemctl status isc-dhcp-server.service

它给了我错误状态failed,以及以下输出/var/log/syslog

systemd[1303]: gnome-launched-code.desktop-6211.scope: Succeeded.
Apr 20 18:51:20 dragonfly systemd[1]: Started ISC DHCP IPv4 server.
Apr 20 18:51:20 dragonfly dhcpd[6757]: Internet Systems Consortium DHCP Server 4.4.1
Apr 20 18:51:20 dragonfly sh[6757]: Internet Systems Consortium DHCP Server 4.4.1
Apr 20 18:51:20 dragonfly sh[6757]: Copyright 2004-2018 Internet Systems Consortium.
Apr 20 18:51:20 dragonfly sh[6757]: All rights reserved.
Apr 20 18:51:20 dragonfly sh[6757]: For info, please visit https://www.isc.org/software/dhcp/
Apr 20 18:51:20 dragonfly dhcpd[6757]: Copyright 2004-2018 Internet Systems Consortium.
Apr 20 18:51:20 dragonfly dhcpd[6757]: All rights reserved.
Apr 20 18:51:20 dragonfly dhcpd[6757]: For info, please visit https://www.isc.org/software/dhcp/
Apr 20 18:51:20 dragonfly dhcpd[6757]: Config file: /etc/dhcp/dhcpd.conf
Apr 20 18:51:20 dragonfly sh[6757]: Config file: /etc/dhcp/dhcpd.conf
Apr 20 18:51:20 dragonfly sh[6757]: Database file: /var/lib/dhcp/dhcpd.leases
Apr 20 18:51:20 dragonfly sh[6757]: PID file: /run/dhcp-server/dhcpd.pid
Apr 20 18:51:20 dragonfly dhcpd[6757]: Database file: /var/lib/dhcp/dhcpd.leases
Apr 20 18:51:20 dragonfly sh[6757]: Wrote 0 leases to leases file.
Apr 20 18:51:20 dragonfly dhcpd[6757]: PID file: /run/dhcp-server/dhcpd.pid
Apr 20 18:51:20 dragonfly dhcpd[6757]: Wrote 0 leases to leases file.
Apr 20 18:51:20 dragonfly dhcpd[6757]: 
Apr 20 18:51:20 dragonfly sh[6757]: No subnet declaration for eth4 (no IPv4 addresses).
Apr 20 18:51:20 dragonfly sh[6757]: ** Ignoring requests on eth4.  If this is not what
Apr 20 18:51:20 dragonfly sh[6757]:    you want, please write a subnet declaration
Apr 20 18:51:20 dragonfly sh[6757]:    in your dhcpd.conf file for the network segment
Apr 20 18:51:20 dragonfly sh[6757]:    to which interface eth4 is attached. **
Apr 20 18:51:20 dragonfly sh[6757]: Not configured to listen on any interfaces!
Apr 20 18:51:20 dragonfly sh[6757]: If you think you have received this message due to a bug rather
Apr 20 18:51:20 dragonfly sh[6757]: than a configuration issue please read the section on submitting
Apr 20 18:51:20 dragonfly sh[6757]: bugs on either our web page at www.isc.org or in the README file
Apr 20 18:51:20 dragonfly sh[6757]: before submitting a bug.  These pages explain the proper
Apr 20 18:51:20 dragonfly sh[6757]: process and the information we find helpful for debugging.
Apr 20 18:51:20 dragonfly sh[6757]: exiting.
Apr 20 18:51:20 dragonfly dhcpd[6757]: No subnet declaration for eth4 (no IPv4 addresses).
Apr 20 18:51:20 dragonfly dhcpd[6757]: ** Ignoring requests on eth4.  If this is not what
Apr 20 18:51:20 dragonfly dhcpd[6757]:    you want, please write a subnet declaration
Apr 20 18:51:20 dragonfly dhcpd[6757]:    in your dhcpd.conf file for the network segment
Apr 20 18:51:20 dragonfly dhcpd[6757]:    to which interface eth4 is attached. **
Apr 20 18:51:20 dragonfly dhcpd[6757]: 
Apr 20 18:51:20 dragonfly dhcpd[6757]: 
Apr 20 18:51:20 dragonfly dhcpd[6757]: Not configured to listen on any interfaces!
Apr 20 18:51:20 dragonfly dhcpd[6757]: 
Apr 20 18:51:20 dragonfly dhcpd[6757]: If you think you have received this message due to a bug rather
Apr 20 18:51:20 dragonfly dhcpd[6757]: than a configuration issue please read the section on submitting
Apr 20 18:51:20 dragonfly dhcpd[6757]: bugs on either our web page at www.isc.org or in the README file
Apr 20 18:51:20 dragonfly dhcpd[6757]: before submitting a bug.  These pages explain the proper
Apr 20 18:51:20 dragonfly dhcpd[6757]: process and the information we find helpful for debugging.
Apr 20 18:51:20 dragonfly dhcpd[6757]: 
Apr 20 18:51:20 dragonfly dhcpd[6757]: exiting.
Apr 20 18:51:20 dragonfly systemd[1]: isc-dhcp-server.service: Main process exited, code=exited, status=1/FAILURE
Apr 20 18:51:20 dragonfly systemd[1]: isc-dhcp-server.service: Failed with result 'exit-code'.
Apr 20 18:52:39 dragonfly PackageKit: daemon quit
Apr 20 18:52:39 dragonfly systemd[1]: packagekit.service: Succeeded.

我猜测错误代码的相关部分是这样的:

No subnet declaration for eth4 (no IPv4 addresses).
Ignoring requests on eth4.  If this is not what
you want, please write a subnet declaration
in your dhcpd.conf file for the network segment
to which interface eth4 is attached.

有人知道该怎么处理吗?我还发现相关问题,但它已经很老了,我仍然感到困惑。

编辑:

/etc/dhcp/dhcpd.conf

default-lease-time 600;
max-lease-time 7200;

subnet 192.168.1.0 netmask 255.255.255.0 {
 range 192.168.1.150 192.168.1.200;
 option routers 192.168.1.254;
 option domain-name-servers 192.168.1.1, 192.168.1.2;
 option domain-name "mydomain.example";
}

/etc/default/isc-dhcp-server

INTERFACESv4="eth4"

输出ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp4s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether f0:2f:74:dd:bb:18 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.240/24 brd 192.168.0.255 scope global dynamic noprefixroute enp4s0
       valid_lft 4978sec preferred_lft 4978sec
    inet6 fe80::8452:bb97:94cd:6ea6/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: wlo1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 7c:50:79:76:23:f2 brd ff:ff:ff:ff:ff:ff
    altname wlp0s20f3

答案1

基于@Doug Smythies评论我让它工作了。

首先,我将/etc/default/isc-dhcp-server其改为INTERFACESv4="enp4s0",因为这是我的有线接口,我想将其用于 DHCP。

当我运行 时ip addr,它告诉我接口enp4s0在 inet 上192.168.0.240/24。但是,在我的 中,/etc/dhcp/dhcpd.conf我将子网定义为subnet 192.168.1.0 netmask 255.255.255.0。请注意此处子网的区别,192.168.0.x和 部分192.168.1.x。要使 DHCP 服务器正常工作,这些必须匹配。所以我不得不将我的接口移动到192.168.1.x。为此,请按照以下步骤操作:

  1. 转到网络设置。
  2. 删除当前的有线设置。
  3. 使用图标创建一个新的+
  4. 转到IPv4设置,更改IPv4 Method为手动,并设置Addresses字段以匹配您的dhcpd.conf配置。 有线连接 IPv4 配置
  5. 单击添加并保存更改。
  6. 当您重新打开设置时,您应该会看到刚刚设置的正确的 IPv4 地址。有线连接 IPv4 配置检查

或者,我也可以通过/etc/dhcp/dhcpd.conf将所有设置更改为 192.168.0.x 等效设置来重写我的设置以匹配子网,并且保持我的接口设置不变。


所以,现在我的正确设置是:

网络设置:我的正确设置

ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: enp4s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 04:42:1a:2c:9e:a1 brd ff:ff:ff:ff:ff:ff
    altname enp0s31f6
    inet 192.168.1.0/24 brd 192.168.1.255 scope global noprefixroute enp4s0
       valid_lft forever preferred_lft forever
    inet6 fe80::8a50:e8aa:63c1:f6fa/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: wlo1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 20:1e:88:2e:cc:f0 brd ff:ff:ff:ff:ff:ff
    altname wlp0s20f3
    inet 192.168.50.169/24 brd 192.168.50.255 scope global dynamic noprefixroute wlo1
       valid_lft 15594sec preferred_lft 15594sec
    inet6 fe80::45f1:e127:53a0:e49c/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever

/etc/dhcp/dhcpd.conf

default-lease-time 600;
max-lease-time 7200;
authoritative;

subnet 192.168.1.0 netmask 255.255.255.0 {
 range 192.168.1.100 192.168.1.200;
 option routers 192.168.1.254;
 option domain-name-servers 192.168.1.1, 192.168.1.2;
}

/etc/default/isc-dhcp-server

INTERFACESv4="enp4s0"

答案2

帮自己一个忙,使用 systemd-networkd 来设置 DHCP 服务器。

示例配置文件:/etc/systemd/network/eth0.network

[Match]
Name=eth0

[Link]
RequiredForOnline=no

[Network]
DHCPServer=yes
Address=192.168.1.1/24
ConfigureWithoutCarrier=yes

[DHCPServer]
PoolOffset=100
PoolSize=100

可以找到更多选项man systemd.network,甚至 NAT 伪装、端口转发和所有其他好的东西。

通过 启用sudo systemctl enable systemd-networkd和启动sudo systemctl restart systemd-networkd。用于监控的 CLI 工具networkctl

相关内容