Skip to content

Instantly share code, notes, and snippets.

@beginor
Created June 8, 2018 13:49
Show Gist options
  • Select an option

  • Save beginor/ef5b74c5181bd5312cdefb267994343d to your computer and use it in GitHub Desktop.

Select an option

Save beginor/ef5b74c5181bd5312cdefb267994343d to your computer and use it in GitHub Desktop.
Ubuntu Router

Ubuntu 16.04 软件路由

服务器配置

操作系统 Ubuntu 16.04 LTS, 双网卡作软件路由

外网网卡配置

网络经光纤猫之后, 连接外网网卡, 名称 enp8s0 , 动态获取 ip 地址;

内网网卡配置

内网网卡, 名称 enp7s0 , 固定 ip 为 192.168.0.1 , 子网掩码 255.255.255.0 , 网关 192.168.0.1 。

系统配置

编辑 /etc/sysctl.conf 配置文件, 将 # net.ipv4.ip_forward=1 前面的 # 去掉, 启用 IP 地址转发。

设置 iptables , 允许内外网两个网卡通讯, 代码如下:

sudo iptables -t nat -A POSTROUTING -o enp8s0 -j MASQUERADE
sudo iptables -A FORWARD -i enp8s0 -o enp7s0 -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A FORWARD -i enp8s0 -o enp7s0 -j ACCEPT

配置成功之后, 将上面的命令去掉 sudo 保存到 /etc/rc.local 中:

iptables -t nat -A POSTROUTING -o enp8s0 -j MASQUERADE
iptables -A FORWARD -i enp8s0 -o enp7s0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i enp8s0 -o enp7s0 -j ACCEPT

这样系统启动时可以自动设置 iptables 。

内网路由配置

WAN 口接服务器内网网卡, 固定 IP 地址, 192.168.0.2 , 子网掩码 255.255.255.0 , 网关 192.168.0.1 。

LAN 口启用 DHCP 服务,

内网无线

接内网路由器, 开启 DHCP 服务

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment