|
本帖最后由 hkal 于 2017-9-12 23:45 编辑
现在有一大堆软路由系统 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 或者 ed25519 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 (另外一个办法在 46 楼)
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 地址,就如一张网卡般使用。
- 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
- eap_reauth_period=360000000
- ignore_broadcast_ssid=0
- # enable 802.11n (n-mode)
- ieee80211n=1
- wmm_enabled=1
复制代码
4. 安装 dnsmasq 作为 DNS 和 DHCP 服务器
修改 /etc/dnsmasq.conf- interface=lo,br0
- dhcp-range=10.0.1.21,10.0.1.250,255.255.255.0,12h
- cache-size=500 # 缓存多少个地址,按需要修改
复制代码 DNS 服务器方面,我除了 dnsmasq 外,我还装了 dnscrypt-proxy 防止 dns 劫持 (dns 污染)。但这帖子已经很长,所以这里不写了。
现在重启路由后,手机等终端可以连接上路由了,可是还是不能上网。因为还缺少了下一步的 MASQUERADE。
5. iptables rules (firewall + Port Forwarding)
Debian/Ubuntu 的 iptables 教程,都是在 /etc/network/if-pre-up.d 里面加一个脚本来启动 iptables 规则。因为我的 Port Forwarding rules 使用到 WAN 口的 地址,所以我把 iptables 分成 if-pre-up.d 和 if-up.d 两部分。pre-up 时(eth0 还没 ip 地址 )启动 firewall filter 等,post-up 时(eth0 已经分配到 ip 地址)加上 port forwarding 规则。
if-pre-up.d 部分,创建 /etc/network/if-pre-up.d/my-pre-up :
- #!/bin/sh
- EXTIF=eth0
- if [ "$IFACE" = "$EXTIF" ]; then
- /bin/sh /usr/myscripts/iptables/pre-up-rules
- fi
复制代码
创建 /usr/myscripts/iptables/pre-up-rules: (修改后页面不能正常显示,只能用附件)
if-up.d 部分,创建 /etc/network/if-up.d/my-post-up :
- #!/bin/sh
- EXTIF=eth0
- if [ "$IFACE" = "$EXTIF" ]; then
- /bin/sh /usr/myscripts/iptables/post-up-rules
- fi
复制代码
创建 /usr/myscripts/iptables/post-up-rules : (修改后好几次保存都不正确,所以只能使用附件)
将以上四个脚本设为“可执行”: chmod +x filename。重启 ...现在终于可以经这个路由上网了!
6. DDNS
我没有固定 IP,所以要用 DDNS。我用的是 dnsexit(免费、收费都有),更新 DNS 非常简单 - 在 /etc/rc.local 执行以下脚本就行。
/usr/myscripts/update-ddns
- #!/bin/bash
- USER=username;
- PASSWORD=updatepassword;
- DOMAINS=(domain1.net domain2.net); # multiple domains at the same IP separated by spaces
- LOG=/tmp/update-ddns.log
- echo "* $(date)" | tee -a $LOG
- echo | tee -a $LOG
- echo "Updating DDNS to dnsexit ..."
- for domain in "${DOMAINS[@]}"; do
- url="http://update.dnsexit.com/RemoteUpdate.sv?login=$USER&password=$PASSWORD&host=$domain";
- echo $url | tee -a $LOG
- result=$(curl -s $url)
- echo "$result" | tee -a $LOG
- echo | tee -a $LOG
- done
- echo | tee -a $LOG
复制代码 当然要 chmod +x /usr/myscripts/update-ddns 将这脚本设为“可执行”。
7. overlayroot
我们当然要求软路由能经得起断电而系统不坏,系统不会在我们不想改变的时候有任何变化。所以我使用了 overlayroot。
- apt-get install overlayroot
复制代码 其实上面的是不需要的,因为 Ubuntu 已经预装了 overlayroot。
修改 /etc/overlayroot.conf,将 overlayroot="" 改为:
重启系统后,整个 root filesystem 已经被保护了。之后的任何改变,都会在重启之后恢复回之前的状态。
之后如果确实要改变系统,可以输入 overlayroot-chroot 进入真实的档案系统,然后修改 /etc/overlayroot.conf,改回 overlayroot="",重启 ... 这样就停用了 overlayroot。
详细可以参考:https://spin.atomicobject.com/2015/03/10/protecting-ubuntu-root-filesystem/。
8. 其他脚本
我测试过把 WAN 口网线拔掉重插,if-pre-up.d 和 if-up.d 并没有触发。这就让我担心掉线之后会不会不能上网,或者 ip 改变导致 dns 和 port forwarding 规则失败。其实最佳的解决办法可能是用 ip monitor address 然后 grep,一直监视 WAN 口。但我在网上找到 ip monitor address 的例子非常少,而且也不知道怎样改变地址来测试,所以只能用 cron。每隔 5分钟或者10分钟检查一遍。
脚本不贴出来了,在附件里面。- vars : 存储 ping-sites 和 dns-check 两个脚本通用的资料。
- ping-sites : ping 一些外面的网站,第一次不通,ifdown && ifup;连续第二次不通,reboot。
- dns-check : 比较 authoritative servers 和 WAN 口的 ip 地址,并且检查 port forwarding 规则是不是含正确的 ip。
- send-mail : 发送 email 的脚本。
9. WAN-to-LAN 速度测试
测试环境:
- al@DESKTOP-CFK04JL:~$ iperf3 -c 10.0.0.133 -t 10
- Connecting to host 10.0.0.133, port 5201
- [ 4] local 10.0.1.29 port 41268 connected to 10.0.0.133 port 5201
- [ ID] Interval Transfer Bandwidth Retr Cwnd
- [ 4] 0.00-1.00 sec 111 MBytes 929 Mbits/sec 0 731 KBytes
- [ 4] 1.00-2.00 sec 110 MBytes 923 Mbits/sec 0 1.07 MBytes
- [ 4] 2.00-3.00 sec 109 MBytes 912 Mbits/sec 0 1.07 MBytes
- [ 4] 3.00-4.00 sec 110 MBytes 923 Mbits/sec 0 1.07 MBytes
- [ 4] 4.00-5.00 sec 111 MBytes 933 Mbits/sec 0 1.07 MBytes
- [ 4] 5.00-6.00 sec 109 MBytes 912 Mbits/sec 0 1.07 MBytes
- [ 4] 6.00-7.00 sec 109 MBytes 912 Mbits/sec 0 1.07 MBytes
- [ 4] 7.00-8.00 sec 108 MBytes 902 Mbits/sec 0 1.13 MBytes
- [ 4] 8.00-9.00 sec 109 MBytes 912 Mbits/sec 0 1.13 MBytes
- [ 4] 9.00-10.00 sec 109 MBytes 912 Mbits/sec 0 1.13 MBytes
- - - - - - - - - - - - - - - - - - - - - - - - - -
- [ ID] Interval Transfer Bandwidth Retr
- [ 4] 0.00-10.00 sec 1.07 GBytes 917 Mbits/sec 0 sender
- [ 4] 0.00-10.00 sec 1.06 GBytes 914 Mbits/sec receiver
- iperf Done.
- al@DESKTOP-CFK04JL:~$ iperf3 -c 10.0.0.133 -t 10
- Connecting to host 10.0.0.133, port 5201
- [ 4] local 10.0.1.29 port 41272 connected to 10.0.0.133 port 5201
- [ ID] Interval Transfer Bandwidth Retr Cwnd
- [ 4] 0.00-1.00 sec 108 MBytes 906 Mbits/sec 0 1.04 MBytes
- [ 4] 1.00-2.00 sec 110 MBytes 923 Mbits/sec 0 1.04 MBytes
- [ 4] 2.00-3.00 sec 111 MBytes 933 Mbits/sec 0 1.04 MBytes
- [ 4] 3.00-4.00 sec 110 MBytes 923 Mbits/sec 0 1.33 MBytes
- [ 4] 4.00-5.00 sec 110 MBytes 923 Mbits/sec 0 1.33 MBytes
- [ 4] 5.00-6.00 sec 111 MBytes 933 Mbits/sec 0 1.33 MBytes
- [ 4] 6.00-7.00 sec 109 MBytes 912 Mbits/sec 0 1.33 MBytes
- [ 4] 7.00-8.00 sec 111 MBytes 933 Mbits/sec 0 1.33 MBytes
- [ 4] 8.00-9.00 sec 110 MBytes 923 Mbits/sec 0 1.33 MBytes
- [ 4] 9.00-10.00 sec 110 MBytes 923 Mbits/sec 0 1.33 MBytes
- - - - - - - - - - - - - - - - - - - - - - - - - -
- [ ID] Interval Transfer Bandwidth Retr
- [ 4] 0.00-10.00 sec 1.07 GBytes 923 Mbits/sec 0 sender
- [ 4] 0.00-10.00 sec 1.07 GBytes 920 Mbits/sec receiver
- iperf Done.
- al@DESKTOP-CFK04JL:~$
复制代码 WAN-to-LAN 速度 > 900 Mbits/sec,满意!
目前就做成这样,先用一段时间看看有什么要修改的。
2017-07-02 : 增加了 sysctl.conf, 在 14 楼
参考网页:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
评分
-
查看全部评分
|