找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 40227|回复: 84

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

  [复制链接]
发表于 2017-6-29 22:25 | 显示全部楼层 |阅读模式
本帖最后由 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
  1. SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="xx:xx:xx:xx:xx:xx", ATTR{dev_id}=="0x0", ATTR{type}=="1", NAME="eth0"
  2. SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="xx:xx:xx:xx:xx:xx", ATTR{dev_id}=="0x0", ATTR{type}=="1", NAME="eth1"
  3. 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:
  1. # This file describes the network interfaces available on your system
  2. # and how to activate them. For more information, see interfaces(5).

  3. source /etc/network/interfaces.d/*

  4. # The loopback network interface
  5. auto lo
  6. iface lo inet loopback

  7. # The primary network interface
  8. #auto eth0
  9. allow-hotplug eth0
  10. iface eth0 inet dhcp

  11. iface eth1 inet manual
  12. iface wlan0 inet manual

  13. # Bridge setup
  14. auto br0
  15. iface br0 inet static
  16.         bridge_ports eth1       # wlan0 added by hostapd "bridge=br0" line.
  17.         address 10.0.1.1
  18.         broadcast 10.0.1.255
  19.         network 10.0.1.0
  20.         netmask 255.255.255.0
  21.         gateway 10.0.1.1
  22.         bridge_stp off               # disable Spanning Tree Protocol
  23.         bridge_waitport 0            # no delay before a port becomes available
  24.         bridge_fd 0                  # no forwarding delay
复制代码
bridge_ports 只需要 eth1 就行,wlan0 会经 hostapd 加入到 br0 里面。



3. 设置无线 Access Point (hostapd)

  1. apt-get install hostapd haveged
复制代码
haveged看这里)非常重要。没装它的时候,无线终端(例如:手机)连上接路由后能正常使用。但当路由重启后,手机就会出现“身份验证错误”,然后自动忘记无线密码。我花了四五个小时 google,修改 hostapd.conf,甚至换无线网卡测试。后来才找到原因。不明白 Ubuntu 官方连  overlayroot 都预装了,却不预装 haveged。而且大部分的 hostapd 教程也没有提到 haveged。

返回正题。修改 /etc/default/hostapd:
  1. DAEMON_CONF="/etc/hostapd/hostapd.conf"
复制代码


创建 /etc/hostapd/hostapd.conf :
  1. interface=wlan0
  2. driver=nl80211
  3. bridge=br0        # Add wlan0 to br0
  4. country_code=HK
  5. ieee80211d=1

  6. ctrl_interface=wlan0
  7. ctrl_interface_group=0
  8. ssid=YOUR-SSID
  9. hw_mode=g
  10. channel=6
  11. wpa=3
  12. wpa_passphrase=YOUR-PASSPHASE
  13. wpa_key_mgmt=WPA-PSK
  14. wpa_pairwise=TKIP
  15. rsn_pairwise=CCMP
  16. beacon_int=100
  17. auth_algs=3
  18. macaddr_acl=0
  19. eap_reauth_period=360000000

  20. ignore_broadcast_ssid=0

  21. # enable 802.11n (n-mode)
  22. ieee80211n=1
  23. wmm_enabled=1
复制代码




4. 安装 dnsmasq 作为 DNS 和 DHCP 服务器

  1. apt-get install dnsmasq
复制代码

修改 /etc/dnsmasq.conf
  1. interface=lo,br0
  2. dhcp-range=10.0.1.21,10.0.1.250,255.255.255.0,12h
  3. 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 :
  1. #!/bin/sh
  2. EXTIF=eth0
  3. if [ "$IFACE" = "$EXTIF" ]; then
  4.     /bin/sh /usr/myscripts/iptables/pre-up-rules
  5. fi
复制代码

创建 /usr/myscripts/iptables/pre-up-rules: (修改后页面不能正常显示,只能用附件)


if-up.d 部分,创建 /etc/network/if-up.d/my-post-up :
  1. #!/bin/sh
  2. EXTIF=eth0
  3. if [ "$IFACE" = "$EXTIF" ]; then
  4.     /bin/sh /usr/myscripts/iptables/post-up-rules
  5. fi
复制代码

创建 /usr/myscripts/iptables/post-up-rules : (修改后好几次保存都不正确,所以只能使用附件)

将以上四个脚本设为“可执行”: chmod +x filename。重启 ...现在终于可以经这个路由上网了!



6. DDNS
我没有固定 IP,所以要用 DDNS。我用的是 dnsexit(免费、收费都有),更新 DNS 非常简单 - 在 /etc/rc.local 执行以下脚本就行。
/usr/myscripts/update-ddns
  1. #!/bin/bash

  2. USER=username;
  3. PASSWORD=updatepassword;
  4. DOMAINS=(domain1.net domain2.net);                # multiple domains at the same IP separated by spaces

  5. LOG=/tmp/update-ddns.log

  6. echo "* $(date)" | tee -a $LOG
  7. echo | tee -a $LOG

  8. echo "Updating DDNS to dnsexit ..."
  9. for domain in "${DOMAINS[@]}"; do
  10.     url="http://update.dnsexit.com/RemoteUpdate.sv?login=$USER&password=$PASSWORD&host=$domain";
  11.     echo $url | tee -a $LOG
  12.     result=$(curl -s $url)
  13.     echo "$result"  | tee -a $LOG
  14.     echo  | tee -a $LOG
  15. done
  16. echo | tee -a $LOG
复制代码
当然要 chmod +x /usr/myscripts/update-ddns 将这脚本设为“可执行”。



7. overlayroot
我们当然要求软路由能经得起断电而系统不坏,系统不会在我们不想改变的时候有任何变化。所以我使用了 overlayroot。
  1. apt-get install overlayroot
复制代码
其实上面的是不需要的,因为 Ubuntu 已经预装了 overlayroot。

修改 /etc/overlayroot.conf,将 overlayroot="" 改为:
  1. overlayroot="tmpfs"
复制代码
重启系统后,整个 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 速度测试
测试环境:

  1. al@DESKTOP-CFK04JL:~$ iperf3 -c 10.0.0.133 -t 10
  2. Connecting to host 10.0.0.133, port 5201
  3. [  4] local 10.0.1.29 port 41268 connected to 10.0.0.133 port 5201
  4. [ ID] Interval           Transfer     Bandwidth       Retr  Cwnd
  5. [  4]   0.00-1.00   sec   111 MBytes   929 Mbits/sec    0    731 KBytes      
  6. [  4]   1.00-2.00   sec   110 MBytes   923 Mbits/sec    0   1.07 MBytes      
  7. [  4]   2.00-3.00   sec   109 MBytes   912 Mbits/sec    0   1.07 MBytes      
  8. [  4]   3.00-4.00   sec   110 MBytes   923 Mbits/sec    0   1.07 MBytes      
  9. [  4]   4.00-5.00   sec   111 MBytes   933 Mbits/sec    0   1.07 MBytes      
  10. [  4]   5.00-6.00   sec   109 MBytes   912 Mbits/sec    0   1.07 MBytes      
  11. [  4]   6.00-7.00   sec   109 MBytes   912 Mbits/sec    0   1.07 MBytes      
  12. [  4]   7.00-8.00   sec   108 MBytes   902 Mbits/sec    0   1.13 MBytes      
  13. [  4]   8.00-9.00   sec   109 MBytes   912 Mbits/sec    0   1.13 MBytes      
  14. [  4]   9.00-10.00  sec   109 MBytes   912 Mbits/sec    0   1.13 MBytes      
  15. - - - - - - - - - - - - - - - - - - - - - - - - -
  16. [ ID] Interval           Transfer     Bandwidth       Retr
  17. [  4]   0.00-10.00  sec  1.07 GBytes   917 Mbits/sec    0             sender
  18. [  4]   0.00-10.00  sec  1.06 GBytes   914 Mbits/sec                  receiver

  19. iperf Done.
  20. al@DESKTOP-CFK04JL:~$ iperf3 -c 10.0.0.133 -t 10
  21. Connecting to host 10.0.0.133, port 5201
  22. [  4] local 10.0.1.29 port 41272 connected to 10.0.0.133 port 5201
  23. [ ID] Interval           Transfer     Bandwidth       Retr  Cwnd
  24. [  4]   0.00-1.00   sec   108 MBytes   906 Mbits/sec    0   1.04 MBytes      
  25. [  4]   1.00-2.00   sec   110 MBytes   923 Mbits/sec    0   1.04 MBytes      
  26. [  4]   2.00-3.00   sec   111 MBytes   933 Mbits/sec    0   1.04 MBytes      
  27. [  4]   3.00-4.00   sec   110 MBytes   923 Mbits/sec    0   1.33 MBytes      
  28. [  4]   4.00-5.00   sec   110 MBytes   923 Mbits/sec    0   1.33 MBytes      
  29. [  4]   5.00-6.00   sec   111 MBytes   933 Mbits/sec    0   1.33 MBytes      
  30. [  4]   6.00-7.00   sec   109 MBytes   912 Mbits/sec    0   1.33 MBytes      
  31. [  4]   7.00-8.00   sec   111 MBytes   933 Mbits/sec    0   1.33 MBytes      
  32. [  4]   8.00-9.00   sec   110 MBytes   923 Mbits/sec    0   1.33 MBytes      
  33. [  4]   9.00-10.00  sec   110 MBytes   923 Mbits/sec    0   1.33 MBytes      
  34. - - - - - - - - - - - - - - - - - - - - - - - - -
  35. [ ID] Interval           Transfer     Bandwidth       Retr
  36. [  4]   0.00-10.00  sec  1.07 GBytes   923 Mbits/sec    0             sender
  37. [  4]   0.00-10.00  sec  1.07 GBytes   920 Mbits/sec                  receiver

  38. iperf Done.
  39. al@DESKTOP-CFK04JL:~$
复制代码
WAN-to-LAN 速度 > 900 Mbits/sec,满意!



目前就做成这样,先用一段时间看看有什么要修改的。


2017-07-02 : 增加了 sysctl.conf, 在 14 楼


参考网页:



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×

点评

你说的很对。: 5.0 我赞同你的说法。: 5.0
你说的很对。: 5 我赞同你的说法。: 5
  发表于 2017-7-16 12:17
你说的很对。: 5 我赞同你的说法。: 5
好文章!  发表于 2017-7-6 00:27

评分

参与人数 12恩山币 +20 收起 理由
waybq + 1 强大的恩山!(以下重复1万次)
lm317379829 + 1 How ever,this is JB useful!
hixanny + 1 我对你的敬仰犹如江水滔滔,我上朝鲜战场后一定写信给你!!!
hzexe + 1 强大的恩山!(以下重复1万次)
ysbledmh + 1 我对你的敬仰犹如江水滔滔,我上朝鲜战场后一定写信给你!!!
bapi + 1 支付宝已转500w给你!
amoli + 1 强大的恩山!(以下重复1万次)
funison + 1 今天天气不错,适合泡妞。
workboy11 + 1 强大的恩山!(以下重复1万次)
q155128 + 3 迷你电脑近照来一张
chenxin92 + 5 一看就是觉得高端、大气、上档次!
hong0980 + 3 我来恩山就是为了看你!

查看全部评分

我的恩山、我的无线 The best wifi forum is right here.
发表于 2017-6-29 22:36 | 显示全部楼层
这个可以参考参考。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-6-29 22:42 | 显示全部楼层
楼主技术强人啊,这些代码我看着就头晕

点评

都是基本的东西拼凑在一起,没有一样特别深奥的。  详情 回复 发表于 2017-6-30 23:35
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-6-30 06:13 | 显示全部楼层
可以参考参考
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2017-6-30 23:35 | 显示全部楼层
本帖最后由 hkal 于 2017-7-1 00:31 编辑
lurixian 发表于 2017-6-29 22:42
楼主技术强人啊,这些代码我看着就头晕

都是基本的东西拼凑在一起。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-7-1 00:05 | 显示全部楼层
快来试试,软路由主要作用是下载机和NAS。希望楼主能这两个能详细点。

点评

下载机:最早的时候用 transmission,后来用了 baidu 网盘,下完 baiduexporter 导出到 aria2。 再后来百度网盘经常修改,baiduexporter 修改经常跟不上。之后就用迅雷(付费的),再导出给 aria2 下载。 transm  详情 回复 发表于 2017-7-1 00:21
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2017-7-1 00:21 | 显示全部楼层
hong0980 发表于 2017-7-1 00:05
快来试试,软路由主要作用是下载机和NAS。希望楼主能这两个能详细点。

下载机:最早的时候用 transmission,后来用了 baidu 网盘,下完 baiduexporter 导出到 aria2。
再后来百度网盘经常修改,baiduexporter 修改经常跟不上。之后就用迅雷(付费的),再导出给 aria2 下载。

transmission 已经多年没用了。


NAS:
NAS 指的是 samba 还是 ftp ? samba 和 ftp 我都有用。ftp 我用的是 pure-ftpd。

点评

我是主要用挂PT站,aria2是一般网站下载,主力是transmission下载。后面挂了4-5个3T的盘24小时开机。Samba共享播放原盘速度很重要。  详情 回复 发表于 2017-7-1 00:31
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-7-1 00:31 | 显示全部楼层
hkal 发表于 2017-7-1 00:21
下载机:最早的时候用 transmission,后来用了 baidu 网盘,下完 baiduexporter 导出到 aria2。
再后来 ...

我是主要用挂PT站,aria2是一般网站下载,主力是transmission下载。后面挂了4-5个3T的盘24小时开机。Samba共享播放原盘速度很重要。

点评

samba 我现在用在一片 arm 的板子上,只有 usb 2,速度不高。 Samba 速度,以前爬文的时候找到一段,在 smb.conf 里面加上: 我没有比较过是不是有所提升,你有兴趣试试看。  详情 回复 发表于 2017-7-1 00:52
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-7-1 00:38 | 显示全部楼层
弄了个1037u的机器,用LEDE的X86系统我们的联通新版的PPP2.4.7拨不上号非要以前的2.4.5能拨上。看到楼主的教程想更换系统。

点评

自己动手能学习更多,这路由我也是抱着这样的心态做的。  详情 回复 发表于 2017-7-1 00:56
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2017-7-1 00:52 | 显示全部楼层
hong0980 发表于 2017-7-1 00:31
我是主要用挂PT站,aria2是一般网站下载,主力是transmission下载。后面挂了4-5个3T的盘24小时开机。Samb ...

samba 我现在用在一片 arm 的板子上,只有 usb 2,速度不高。
Samba 速度,以前爬文的时候找到一段,在 smb.conf 里面加上:
  1. [global]

  2. # https://www.arm-blog.com/samba-finetuning-for-better-transfer-speeds/
  3. socket options = IPTOS_LOWDELAY SO_RCVBUF=131072 SO_SNDBUF=131072 TCP_NODELAY
  4. min receivefile size = 2048
  5. use sendfile = true
  6. aio read size = 2048
  7. aio write size = 2048
  8. write cache size = 1024000
  9. read raw = yes
  10. write raw = yes
  11. getwd cache = yes
  12. oplocks = yes
  13. max xmit = 32768
  14. dead time = 15
  15. large readwrite = yes
复制代码
我没有比较过是不是有所提升,你有兴趣试试看。

点评

非常感谢谢!  详情 回复 发表于 2017-7-1 00:57
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2017-7-1 00:56 | 显示全部楼层
hong0980 发表于 2017-7-1 00:38
弄了个1037u的机器,用LEDE的X86系统我们的联通新版的PPP2.4.7拨不上号非要以前的2.4.5能拨上。看到楼主的 ...

自己动手能学习更多,这路由我也是抱着这样的心态做的。


我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-7-1 00:57 | 显示全部楼层
hkal 发表于 2017-7-1 00:52
samba 我现在用在一片 arm 的板子上,只有 usb 2,速度不高。
Samba 速度,以前爬文的时候找到一段,在  ...

非常感谢谢!

点评

如果测试过,请告诉我结果。  详情 回复 发表于 2017-7-1 00:59
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2017-7-1 00:59 | 显示全部楼层

如果测试过,请告诉我结果。
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

 楼主| 发表于 2017-7-2 22:31 | 显示全部楼层
本帖最后由 hkal 于 2017-7-2 22:35 编辑

补上 sysctl.conf。

部分设定是希望增加 Network throughput 的,可是测试过,没看到区别。
部分设定是增加安全的(参考了一些网页,还有 ipcop, clearOS, debian 等系统)。


因为 sysctl.conf 已经设置了,所以 1楼的 /usr/myscripts/iptables/pre-up-rules 可以去掉下面几行:
  1. # Ref : https://wiki.debian.org/DebianFirewall
  2. #echo -n '1' > /proc/sys/net/ipv4/ip_forward
  3. #echo -n '0' > /proc/sys/net/ipv4/conf/all/accept_source_route
  4. #echo -n '0' > /proc/sys/net/ipv4/conf/all/accept_redirects
  5. #echo -n '1' > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  6. #echo -n '1' > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
复制代码


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

发表于 2017-7-3 15:49 来自手机 | 显示全部楼层
膜拜。ui难搞么……

点评

你的意思是不是 "没有ui难不难搞"?对 Linux 有些经验就不难搞。 设置的时候,我们一般不在 Linux 脑上直接操作的。我一般在 Windows 上面,用 putty (命令界面)和 WinSCP (创建,修改档案,拷贝....)通过 ssh  详情 回复 发表于 2017-7-3 22:31
我的恩山、我的无线 The best wifi forum is right here.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-4-20 18:55

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表