centos7中如何根据“dhcp-server-identifier”用一张网卡服务不同的子网

centos7中如何根据“dhcp-server-identifier”用一张网卡服务不同的子网

我有一个无法访问互联网的孤立系统。

我想使用其中一台服务器来运行 dhcp 服务器,并在客户端想要居住的不同子网中提供不同的 IP。

我可以从客户端向服务器发送请求到subnet2,但服务器总是返回subnet1中的ip。

以下是我的设置。

/etc/dhcp/dhcpd.conf

subnet 172.16.50.0 netmask 255.255.255.0 {
  range 172.16.50.100 172.16.50.200;
  option subnet-mask 255.255.255.0;
  option routers 172.16.50.254;
  option broadcast-address 172.16.50.255;
  option ip-forwarding off;
  option domain-name-servers 8.8.8.8;
  if exists user-class and option user-class = "iPXE" {
    filename "bootstrap.ipxe";
  } else {
    filename "undionly.kpxe";
  }
  next-server   172.16.50.31;
}
subnet 172.16.51.0 netmask 255.255.255.0 {
  range 172.16.51.100 172.16.51.200;
  option subnet-mask 255.255.255.0;
  option routers 172.16.51.254;
  option broadcast-address 172.16.51.255;
  option ip-forwarding off;
  option domain-name-servers 8.8.8.8;
  if exists user-class and option user-class = "iPXE" {
    filename "bootstrap.ipxe";
  } else {
    filename "undionly.kpxe";
  }
  next-server   172.16.51.31;
}

IP设置

$ sudo modprobe dummy
$ sudo ip link set name eth10 dev dummy0
$ sudo ip addr add 172.16.51.31/24 brd + dev eth10 label eth10:0

$ip 一个

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    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: enp1s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 04:7d:7b:d8:bb:6a brd ff:ff:ff:ff:ff:ff
    inet 172.16.50.31/24 brd 172.16.50.255 scope global enp1s0f0
       valid_lft forever preferred_lft forever
    inet6 fe80::67d:7bff:fed8:bb6a/64 scope link
       valid_lft forever preferred_lft forever
3: enp1s0f1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN qlen 1000
    link/ether 04:7d:7b:d8:bb:6b brd ff:ff:ff:ff:ff:ff
4: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
    link/ether 52:54:00:36:03:d7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever
5: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 500
    link/ether 52:54:00:36:03:d7 brd ff:ff:ff:ff:ff:ff
7: eth10: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN
    link/ether d2:b8:2e:44:a5:2f brd ff:ff:ff:ff:ff:ff
    inet 172.16.51.31/24 brd 172.16.51.255 scope global eth10:0
       valid_lft forever preferred_lft forever

$ifconfig

enp1s0f0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.50.31  netmask 255.255.255.0  broadcast 172.16.50.255
        inet6 fe80::67d:7bff:fed8:bb6a  prefixlen 64  scopeid 0x20<link>
        ether 04:7d:7b:d8:bb:6a  txqueuelen 1000  (Ethernet)
        RX packets 5288228  bytes 1902558434 (1.7 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6310647  bytes 6023578728 (5.6 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xdfe20000-dfe3ffff

enp1s0f1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 04:7d:7b:d8:bb:6b  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device memory 0xdfe00000-dfe1ffff

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (Local Loopback)
        RX packets 6164718  bytes 2009821769 (1.8 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6164718  bytes 2009821769 (1.8 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
        ether 52:54:00:36:03:d7  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

“在客户端”/etc/dhcp/dhclient.conf

send dhcp-server-identifier 172.16.51.31;

运行 dhcp 客户端

$ dhclient -r -v ens20f0
    Listening on LPF/ens20f0/54:ab:3a:3d:2c:7a
    Sending on   LPF/ens20f0/54:ab:3a:3d:2c:7a
    Sending on   Socket/fallback
    DHCPRELEASE on ens20f0 to 172.16.50.31 port 67 (xid=0x41973da)

$ dhclient -s 172.16.51.31 -v ens20f0
    Listening on LPF/ens20f0/54:ab:3a:3d:2c:7a
    Sending on   LPF/ens20f0/54:ab:3a:3d:2c:7a
    Sending on   Socket/fallback
    DHCPDISCOVER on ens20f0 to 172.16.51.31 port 67 interval 7 (xid=0x2c4dfe6c)
    DHCPREQUEST on ens20f0 to 172.16.51.31 port 67 (xid=0x2c4dfe6c)
    DHCPOFFER from 172.16.50.31
    DHCPPACK from 172.16.50.31 (xid=0x2c4dfe6c)
    bound to 172.16.50.116 -- renewal in 20324 seconds.

我向 172.16.50.31 发送请求。但我只得到了172.16.50.0/24的ip。

我在网上读过一种解决方案:https://ubuntuforums.org/showthread.php?t=2186879

但这是在 Ubuntu 中,而不是 centos 中。我无法一步一步做到。

有什么方法可以在 CentOS 7 中提供不同子网中的 ip 吗?

答案1

我自己找到了解决方案。

在 /etc/sysconfig/network-script/ 中添加虚拟接口作为永久接口将使服务器通过一个真实接口提供多个子网。

如果该接口被添加为临时接口(例如ifconfig eth0:1 172.16.52.0/24),服务器将无法提供多子网服务。

相关内容