AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 956457
Accepted
Thomas Ward
Thomas Ward
Asked: 2017-09-17 09:16:50 +0800 CST2017-09-17 09:16:50 +0800 CST 2017-09-17 09:16:50 +0800 CST

如何将 Ubuntu 服务器设置为我网络上两个专用网络的路由器,并进行 DHCP 寻址?

  • 772

我想使用我的 Ubuntu 服务器作为我网络上两个专用网络的路由器。

我还希望这台服务器为专用网络提供 DHCP 请求。

如何设置 Ubuntu 16.04 服务器来执行此操作?

networking
  • 1 1 个回答
  • 14309 Views

1 个回答

  • Voted
  1. Best Answer
    Thomas Ward
    2017-09-17T09:16:50+08:002017-09-17T09:16:50+08:00

    注意#1:这是为了设置可能的最基本设置 - 两个或多个子网的直接路由器,根本不限制跨子网流量。这可以通过防火墙规则进行更改,以更好地控制单个功能或跨子网或出站到 Internet 的网络访问,但这超出了此基本“设置”Q/A 对的范围。

    注意 #2:在编写此答案时,Ubuntu 尚未将 Netplan 用于其网络控制系统进行默认设置。这个答案是在没有考虑Netplan的情况下编写的。

    您需要一台带有 3 个网络接口的 Ubuntu 服务器,当然,一个用于 Internet 连接的接口,以及两个专用网络接口。

    之后,只需按照本指南将此服务器设置为路由器:

    (1) 编辑/etc/sysctl.conf。查找并取消注释此行:

    net.ipv4.ip_forward=1
    

    完成此操作后,执行命令sudo sysctl -p以重新加载系统内核设置。这使得 Ubuntu 机器现在可以通过 IPv4 跨子网和 VLAN 提供流量。

    (2) 编辑您的路由器框/etc/network/interfaces以在服务于专用网络的接口上设置静态 IP 地址。在这个例子中,我知道接口是ens37和ens38,而ens33是我盒子上的主要 Internet 连接接口。我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
    

    为您的设置相应地调整网络地址和接口名称。

    **请注意,如果您使用的是 VMware Workstation 或类似设备,并且vmnet#为此在 VM 中选择了设备,请确保主机系统没有为此连接主机的设备vmnet,或者如果有,则使用与路由器盒上最后一个八位字节中的地址不同.1。

    (3) 安装 DHCP 服务器软件。我们将在后面的步骤中配置它。

    sudo apt-get install isc-dhcp-server
    

    这将允许路由器为您的私有子网提供 DHCP 请求。

    (4) 首先,将自动安装的 DHCP Server config 复制到备份文件中。

    sudo cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.dist
    

    (5) 现在,我们将原始文件设为空白,以便我们可以应用我们的配置。

    echo "" | sudo tee /etc/dhcp/dhcpd.conf
    

    (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;
    }
    

    根据您的需要调整此配置,并确保根据您在上面的网络配置设置中设置的任何 IP 地址更新“路由器”选项。

    根据需要完成文件调整后,保存文件。

    (7) 我们需要告诉系统需要关心哪些接口。编辑/etc/default/isc-dhcp-server,并在该行中指定您的专用网络接口(在我的情况下,ens37和38)INTERFACES="",使其看起来像这样:

    INTERFACES="ens37 ens38"
    

    保存文件。

    (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
    

    (9) 安装iptables-persistent让我们能够真正记住我们的iptables规则并在重新启动时加载它们的包。

    sudo apt-get install iptables-persistent`
    

    当它要求保存现有规则时,为 IPv4 和 IPv6 选择“是”。

    (10) 测试一下!在您配置的一个专用网络上设置另一个系统,并确保一旦设置它就可以与 Internet 通信,并且在上面设置的专用子网中具有配置的 DHCP 地址!

    • 6

相关问题

  • 如何设置 VLAN 转发?

  • 如何将主机 Ubuntu 上的 VPN (tun0) 网络适配器映射到 VirtualBox 来宾 Windows?

  • 如何限制下载/上传带宽?

  • 如何通过 Windows 网络共享文件?

  • 面板小程序以文本形式显示当前网络流量?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve