Tho*_*ard 7 networking dhcpd router
我想使用我的 Ubuntu 服务器作为我网络上两个专用网络的路由器。
我还希望此服务器为专用网络的 DHCP 请求提供服务。
如何设置 Ubuntu 16.04 服务器来执行此操作?
注意 #1:这是为了设置最基本的设置 - 一个用于两个或多个子网的直接路由器,完全不限制跨子网流量。这可以通过防火墙规则进行更改,以更好地控制各个功能或跨子网或出站到 Internet 的网络访问,但这超出了此基本“设置”问答对的范围。
注意#2:在撰写此答案时,Ubuntu 尚未将 Netplan 用于其默认设置的网络控制系统。这个答案是这样写的,没有考虑到 Netplan。
您需要一台带有 3 个网络接口的 Ubuntu 服务器,当然,一个用于连接 Internet 的接口,以及两个专用网络接口。
之后,只需按照本指南将此服务器设置为路由器:
(1) 编辑/etc/sysctl.conf
。查找并取消注释此行:
net.ipv4.ip_forward=1
Run Code Online (Sandbox Code Playgroud)
完成此操作后,执行命令sudo sysctl -p
以重新加载系统内核设置。这允许 Ubuntu box 现在通过 IPv4 为跨子网和 VLAN 的流量提供服务。
(2) 编辑您的路由器盒/etc/network/interfaces
以在服务于专用网络的接口上设置静态 IP 地址。在这个例子中,我知道接口是ens37
和ens38
,而ens33
是我的盒子上主要的互联网连接接口。我ens33
独自离开,但添加ens37
和ens38
配置节:
# Private Subnet 1
auto ens37
iface ens37 inet static
address 10.76.100.1
netmask 255.255.255.0
dns-nameservers 8.8.8.8 8.8.4.4
# Private Subnet 2
auto ens38
iface ens38 inet static
address 10.76.101.1
netmask 255.255.255.0
dns-nameservers 8.8.8.8 8.8.4.4
Run Code Online (Sandbox Code Playgroud)
根据您的设置相应地调整网络地址和接口名称。
**请注意,如果您使用的是 VMware Workstation 或类似vmnet#
设备,并且您在 VM 中为此选择了设备,请确保主机系统没有用于此的主机连接设备vmnet
,或者如果有,您使用与.1
路由器盒上最后一个八位字节不同的地址。
(3) 安装DHCP服务器软件。我们将在后面的步骤中配置它。
sudo apt-get install isc-dhcp-server
Run Code Online (Sandbox Code Playgroud)
这将允许路由器盒为您的私有子网提供 DHCP 请求。
(4) 首先,将自动安装的 DHCP Server 配置复制到备份文件中。
sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.dist
Run Code Online (Sandbox Code Playgroud)
(5) 现在,我们将该原始文件设为空白,以便我们可以应用我们的配置。
echo "" | sudo tee /etc/dhcp/dhcpd.conf
Run Code Online (Sandbox Code Playgroud)
(6) 现在让我们使用我们的配置:
# DHCP Server config
# The ddns-updates-style parameter controls whether or not the server will
# attempt to do a DNS update when a lease is confirmed. We default to the
# behavior of the version 2 packages ('none', since DHCP v2 didn't
# have support for DDNS.)
ddns-update-style none;
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
authoritative;
# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;
# Specify the domain name servers to specify for each subnet.
option domain-name-servers 8.8.8.8;
option domain-name-servers 8.8.4.4;
# DHCP Subnet configurations
# Subnet 1 - ens37
subnet 10.76.100.0 netmask 255.255.255.0 {
default-lease-time 86400;
max-lease-time 86400;
range 10.76.100.10 10.76.100.200;
option routers 10.76.100.1;
option subnet-mask 255.255.255.0;
option broadcast-address 10.76.100.255;
}
# Subnet 2 - ens38
subnet 10.76.101.0 netmask 255.255.255.0 {
default-lease-time 86400;
max-lease-time 86400;
range 10.76.101.10 10.76.101.200;
option routers 10.76.101.1;
option subnet-mask 255.255.255.0;
option broadcast-address 10.76.101.255;
}
Run Code Online (Sandbox Code Playgroud)
根据您的需要相应地调整此配置,并确保根据您在上面的网络配置设置中设置的任何 IP 地址更新“路由器”选项。
根据需要调整文件后,保存文件。
(7) 我们需要告诉系统需要关心哪些接口。编辑/etc/default/isc-dhcp-server
,并在行中指定您的专用网络接口(在我的情况下为ens37
和38
)INTERFACES=""
,使其看起来像这样:
INTERFACES="ens37 ens38"
Run Code Online (Sandbox Code Playgroud)
保存文件。
(8) 现在,防火墙规则。我们需要告诉系统允许事物作为路由器工作,并为此设置适当的控制和规则集。我假设您还没有在这里配置防火墙,因为我正在描述一个从头开始的设置。
如果您已经安装了ufw
这台机器上,运行sudo ufw disable
,然后卸载ufw
用sudo apt-get remove ufw
。 UFW 不适合我们需要的东西,我们需要iptables
直接使用的高级功能。对于大多数路由器,我们应该不被使用UFW可言。
确保我们知道您的 Internet 连接网络接口的接口名称。在我的示例测试系统上,它是ens33
,但在您的系统上可能会有所不同。确保我们也知道我们将成为路由器的私有网络的网络接口;我们这里也需要他们。iptables
使用以下命令进行如下设置。也请注意我的评论:
# Accept localhost traffic (local traffic to the system itself)
sudo iptables -A INPUT -i lo -j ACCEPT
# Accept all traffic related to established connections
sudo iptables -A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# Drop traffic coming from the Internet into our Internet-connected interface (except for traffic related to other established connections)
# Update this to match the interface name of your Internet-connected interface.
sudo iptables -A INPUT -i ens33 -j DROP
# Accept traffic inbound from the local subnets we are acting as a router for.
# CHANGE THESE INTERFACE NAMES ACCORDING TO YOUR SETUP!
sudo iptables -A INPUT -i ens37 -j ACCEPT
sudo iptables -A INPUT -i ens38 -j ACCEPT
# Since we don't want to have our system completely open to the Internet, we need
# to drop all other traffic coming to our network. This way, we can prevent the
# Internet at large from accessing our network directly from the Internet.
sudo iptables -A INPUT -j DROP
# Add rules to accept forwarding on the interfaces we are doing routing for.
# CHANGE THESE INTERFACE NAMES ACCORDING TO YOUR SETUP!
sudo iptables -A FORWARD -i ens37 -j ACCEPT
sudo iptables -A FORWARD -o ens37 -j ACCEPT
sudo iptables -A FORWARD -i ens38 -j ACCEPT
sudo iptables -A FORWARD -o ens38 -j ACCEPT
# Add rules to the NAT table to allow us to actually let traffic on the interfaces
# which we are doing routing for go out to the Internet masquerade as our Internet-
# connected interface.
#
# ADJUST THE IP RANGES HERE TO MATCH THE IP RANGES AND THE SUBNETS FOR YOUR OWN
# ENVIRONMENT! Remember that the IP address of 10.76.100.1 for the router, and
# the netmask 255.255.255.0 is equal to the network range/CIDR of 10.76.100.0/24
# for the purposes of these rules.
sudo iptables -t nat -A POSTROUTING -s 10.76.100.0/24 ! -d 10.76.100.0/24 -j MASQUERADE
sudo iptables -t nat -A POSTROUTING -s 10.76.101.0/24 ! -d 10.76.101.0/24 -j MASQUERADE
Run Code Online (Sandbox Code Playgroud)
(9) 安装iptables-persistent
允许我们真正记住我们的iptables
规则并在重启时加载它们的包。
sudo apt-get install iptables-persistent`
Run Code Online (Sandbox Code Playgroud)
当它要求保存现有规则时,请为 IPv4 和 IPv6 选择“是”。
(10) 测试一下!在您配置的其中一个专用网络上设置另一个系统,并确保设置后它可以与 Internet 通信,并且在上面设置的专用子网中具有配置的 DHCP 地址!