用 Ubuntu 自己设定软路由,不用现成的软路由系统

现在有一大堆软路由系统
https://en.wikipedia.org/wiki/List_of_router_and_firewall_distributions,都不知道那个好,那个不好 。
这么多系统,也不可能一个一个去安装测试,那不如自己试试用 Ubuntu 设置一台路由 。
硬件(迷你电脑):

  • Intel n3150 CPU, 4GB RAM, 64GB SSD
  • Realtek Giga LAN x 2
  • Intel N-7620 Wireless LAN x 1

软件系统:Ubuntu Server 16.04.2 LTS
Ubuntu 系统的安装,SSH 的安全设置(禁止密码登录,4096 bits key)等就不详细说了 。
以下内容包括:
  • 修改 Network Device Name
  • 创建 bridge, br0 (eth1 + wlan0)
  • 设置无线 Access Point (hostapd)
  • 安装 DNSmasq 作为 DNS 和 DHCP 服务器
  • iptables rules (firewall + Port Forwarding)
  • DDNS
  • overlayroot
  • 其他脚本
  • WAN-to-LAN 速度测试

1. 修改 Network Device Name
Ubuntu 从前一两个版本开始,网卡的名称就不是 eth0, eth1, wlan0 等,使用起来很不习惯 。所以第一步是把它们改回原来的命名方式 。
创建一个档 :
/etc/udev/rules.d/70-persistent-net.rules
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="xx:xx:xx:xx:xx:xx", ATTR{dev_id}=="0x0", ATTR{type}=="1", NAME="eth0"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="xx:xx:xx:xx:xx:xx", ATTR{dev_id}=="0x0", ATTR{type}=="1", NAME="eth1"
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="xx:xx:xx:xx:xx:xx", ATTR{dev_id}=="0x0", ATTR{type}=="1", NAME="wlan0"
 
上面 "xx:xx:xx:xx:xx:xx" 是每张网卡的 mac 地址(可以用 ifconfig -a 来查询)
(不要忘了)然后修改 /etc/network/interfaces,将里面的网口名称改回 eth0,eth1,wlan0,存档,重启...
重启后三张网卡的名称已经改变。这三张网卡将会如此使用:
  • eth0 - WAN
  • eth1 - LAN (10.0.1.1)
  • wlan0 - LAN (10.0.1.1)

2. 创建 bridge, br0 (eth1 + wlan0)
就如一般的路由,我希望 eth1 和 wlan0 共用一个 LAN 地址,就如一张网卡般使用 。
 
1
apt-get install bridge-utils
 
修改 /etc/network/interfaces:
 
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
#auto eth0
allow-hotplug eth0
iface eth0 inet dhcp
iface eth1 inet manual
iface wlan0 inet manual
# Bridge setup
auto br0
iface br0 inet static
bridge_ports eth1 # wlan0 added by hostapd "bridge=br0" line.
address 10.0.1.1
broadcast 10.0.1.255
network 10.0.1.0
netmask 255.255.255.0
gateway 10.0.1.1
bridge_stp off # disable Spanning Tree Protocol
bridge_waitport 0 # no delay before a port becomes available
bridge_fd 0 # no forwarding delay
 
bridge_ports 只需要 eth1 就行,wlan0 会经 hostapd 加入到 br0 里面 。
 
3. 设置无线 Access Point (hostapd)
apt-get install hostapd haveged
 
haveged (看这里)非常重要 。没装它的时候,无线终端(例如:手机)连上接路由后能正常使用 。但当路由重启后,手机就会出现“身份验证错误”,然后自动忘记无线密码 。我花了四五个小时 google,修改 hostapd.conf,甚至换无线网卡测试 。后来才找到原因 。不明白 Ubuntu 官方连 overlayroot 都预装了,却不预装 haveged 。而且大部分的 hostapd 教程也没有提到 haveged 。
返回正题 。修改 /etc/default/hostapd:
 
DAEMON_CONF="/etc/hostapd/hostapd.conf"
 
创建 /etc/hostapd/hostapd.conf :
interface=wlan0
driver=nl80211
bridge=br0 # Add wlan0 to br0
country_code=HK
ieee80211d=1
ctrl_interface=wlan0
ctrl_interface_group=0
ssid=YOUR-SSID
hw_mode=g
channel=6
wpa=3
wpa_passphrase=YOUR-PASSPHASE
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
beacon_int=100
auth_algs=3
macaddr_acl=0


推荐阅读