使用Gentoo Linux 手搓 软路由-软路由,x86系统,openwrt(x86),Router OS 等-恩山无线论坛 (2024)

原文在: https://macgeeker.com/linux/router/

现在在软路由方面, OpenWRT已经相当火爆了, 当然 OpenWRT也做的非常好了. 但是对我个人来说, 我觉得还是有一些问题

  • 升级困难, 在内核不升级的情况下, 一些对内核有依赖的包升级都会有问题
  • 升级内核(基于ext4的分区), 升级后, 又会遇到apk包的兼容问题(其实最好的方法就是刷机, 但是我又不想)
  • 刷机的话, 之前做过定制编译的, 又得来一遍, 虽然有了 make menuconfig 但是也不能完全解决问题
  • ssr plus 和 passwall 都过于复杂
  • luci 中的各插件基本上都是互相隔离的(应该是设计原则), 如 passwall 会自己开一个 dnsmasq
  • 核心命令使用的是 busybox 裁剪过的. 参数和 linux 标准的有差异. (我非常理解, 因为是嵌入式的, 所以裁剪合理. 但是我是一个x86的路由器, 存储空间不是什么问题)
  • ....

总之有很多让我不爽的地方, 所以折腾了一下, 把 OpenWRT 换成了 Gentoo Linux , 本文就是分享一下, 我搭建过程, 其实过程并不复杂, 我看网上也有一些文章有介绍, 可能并不是所有的朋友都明白为何要这样做.

步骤

  • 安装 Gentoo Linux 这个没啥好分享的, 参照官方手册就好了.
  • 配置成路由器
  • 配置 IPTV
  • 配置 xray
  • 提升性能

本文仅分享前三个方面, 后面再发文续

安装 Gentoo Linux

官方的手册写的非常好, 这是所有 Linux 文档中我觉得写的最好的, 这也是我热爱 Gentoo 的原因之一. 我每次安装也要参考手册, 并不是每一个步骤的指令都记的住. Gentoo AMD64 Handbook

参考他的文档就好了, 这里我就不狗尾续貂了. 说一下我的基本配置吧

  • EFI 引导
  • GPT 分区, fstab 使用 uuid
  • kernel 直接使用 genkernel 来生成
  • 对 CFlags 作了针对本机CPU的优化
  • profile 我选的是 default/linux/amd64/23.0/no-multilib (stable). 我个人不是非常喜欢 systemd, 当然我不否认它非常牛! 所以我还是沿用 OpenRC

配置成路由器

其实把 Gentoo 配置成路由器, 官方也是有文档的. 为了更友好一些, 我略补充一下 Home Router 这个我以双网卡的机器来举例.

这里有几个问题需要解决:

  • WAN 网络的配置
  • LAN 网络的配置
  • IPTV 网络的配置
  • 内核转发以及 NAT 的配置
  • DNS 服务器配置

我觉得我越写条理越差, 我是不是应该把IPTV独立一文出来

  1. ln -s /etc/init.d/net /etc/init.d/net.enp1s0f1
  2. /etc/init.d/net.enp1s0f1 restart

复制代码

LAN 网络配置

LAN 的网段, 我习惯使用 192.168.88.0/24 vim /etc/config/net

  1. config_enp1s0f0="192.168.88.1 netmask 255.255.255.0 brd 192.168.88.255"
  2. routers_enp1s0f0="192.168.88.1"

复制代码

  1. ln -s /etc/init.d/net /etc/init.d/net.enp1s0f0
  2. /etc/init.d/net.enp1s0f0 restart

复制代码

IPTV 网络配置

我的是 vlan id 是 85 . bridge 在 Linux 内部是交换机的意思, 因为要接收 vlan id 是 85 的数据, 所以需要使用到交换机的功能. vim /etc/config/net

  1. vlans_enp1s0f1="85 51"
  2. config_enp1s0f1_85="null"
  3. config_enp1s0f1_51="null"
  4. bridge_briptv85="enp1s0f1.85"
  5. config_briptv85="dhcp"
  6. briptv85_dhcp="nodns nogateway"
  7. emtric_briptv85=20

复制代码

  1. /etc/init.d/net.briptv85 start

复制代码

内核转发以及 NAT 的配置

  1. vim /etc/sysctl.conf

复制代码

  1. # IPv4 forward规则
  2. net.ipv4.ip_forward=1
  3. net.ipv4.ip_dynaddr = 1
  4. ### rp_fileter 针对 wan 我使用的是严格模式, 其它的都是不校验, 一是没有安全问题, 二是 IPTV的需要
  5. net.ipv4.conf.default.rp_filter = 0
  6. net.ipv4.conf.all.rp_filter = 0
  7. net.ipv4.conf.enp1s0f1.rp_filter = 1
  8. net.ipv4.conf.briptv85.rp_filter = 0
  9. net.ipv4.conf.wg0.rp_filter = 0
  10. net.ipv4.icmp_echo_ignore_broadcasts = 0
  11. # net.ipv4.conf.briptv85.rp_filter = 0
  12. # /proc/sys/net/ipv4/conf/all/proxy_arp
  13. net.ipv4.conf.all.proxy_arp = 0

  14. net.ipv4.tcp_fastopen=3

  15. # 开启 bbr, 都懂的
  16. net.core.default_qdisc=fq
  17. net.ipv4.tcp_congestion_control=bbr

复制代码

  1. sysctl -p

复制代码


配置 NAT: Linux 有强大的 iptables 所以 NAT 使用这个就好了, 貌似也没有别的选择, nftables 么? 不想学. 都是操作 netfilter 的, 据说性能有一定的提升, 而且书写规则更友好.
  1. export LAN=enp1s0f0
  2. export WAN=enp1s0f1
  3. export IPTV85=briptv85
  4. export IPTV15=briptv15
  5. export WG=wg0
  6. export TUNSERVER=tun0
  7. export TUNFOX=tun1

  8. # 清空iptables规则, 和NAT表
  9. iptables -F
  10. iptables -Z
  11. iptables -t nat -F

  12. # 允许从 WAN 访问 内部的服务
  13. ## 允许ping
  14. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  15. iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT
  16. ### iptables -A INPUT -p icmp --icmp-type 8 -s 0/0 -j DROP # 禁ping

  17. # iptables -A INPUT -p TCP --dport 22 -i ${WAN} -j ACCEPT # SSH
  18. # iptables -A OUTPUT -p TCP --sport ssh -j ACCEPT
  19. ### OPEN VPN
  20. iptables -A INPUT -p TCP --dport 8194 -i ${WAN} -j ACCEPT # OPENVPN
  21. iptables -A INPUT -p UDP --dport 8194 -i ${WAN} -j ACCEPT # OPENVPN
  22. ### shadowsocks
  23. iptables -A INPUT -p TCP --dport 5500 -i ${WAN} -j ACCEPT # shadownsocks TCP
  24. iptables -A INPUT -p UDP --dport 5500 -i ${WAN} -j ACCEPT # shadownsocks UDP
  25. iptables -A INPUT -p TCP --dport 5560 -i ${WAN} -j ACCEPT # shadownsocks TCP
  26. iptables -A INPUT -p UDP --dport 5560 -i ${WAN} -j ACCEPT # shadownsocks UDP
  27. ### ipsec vpn
  28. iptables -A INPUT -p UDP --dport 4500 -i ${WAN} -j ACCEPT # IPSec VPN / IKE
  29. iptables -A INPUT -p TCP --dport 4500 -i ${WAN} -j ACCEPT # IPSec VPN / IKE TCP
  30. iptables -A INPUT -p UDP --dport 500 -i ${WAN} -j ACCEPT # IPSec VPN
  31. iptables -A INPUT -p TCP --dport 500 -i ${WAN} -j ACCEPT # IPSec VPN
  32. ### wireguard
  33. iptables -A INPUT -p UDP --dport 9901 -i ${WAN} -j ACCEPT # IPSec VPN
  34. iptables -A INPUT -p TCP --dport 9901 -i ${WAN} -j ACCEPT # IPSec VPN
  35. ### cloud
  36. iptables -A INPUT -p UDP --dport 8443 -i ${WAN} -j ACCEPT # IPSec VPN
  37. iptables -A INPUT -p TCP --dport 8443 -i ${WAN} -j ACCEPT # IPSec VPN


  38. ## 8443 DNAT到 192.168.88.8服务上,改nginx
  39. # iptables -t nat -A PREROUTING -p TCP --dport 8443 -i ${WAN} -j DNAT --to 192.168.88.8:8443
  40. # iptables -t nat -A POSTROUTING -p TCP -d 192.168.88.8 --dport 8443 -j SNAT --to 192.168.88.1

  41. # 添加NAT规则
  42. iptables -A FORWARD -i ${LAN} -d 192.168.88.0/24 -j DROP
  43. iptables -A FORWARD -i ${LAN} -s 192.168.88.0/24 -j ACCEPT
  44. iptables -A FORWARD -i ${WAN} -d 192.168.88.0/24 -j ACCEPT
  45. # iptables -A FORWARD -i ${LAN} -s 192.168.18.0/24 -j ACCEPT
  46. iptables -A FORWARD -i ${LAN} -d 192.168.18.0/24 -j ACCEPT
  47. iptables -A FORWARD -i ${WAN} -d 192.168.18.0/24 -j ACCEPT
  48. iptables -A FORWARD -i ${TUNSERVER} -d 192.168.88.0/24 -j ACCEPT
  49. # iptables -A FORWARD -o tun1 -d 192.168.18.0/24 -j ACCEPT
  50. # iptables -A FORWARD -i ${WAN} -s 10.8.9.0/24 -j ACCEPT
  51. iptables -A FORWARD -i ${WG} -j ACCEPT
  52. iptables -A FORWARD -o ${WG} -j ACCEPT
  53. iptables -A FORWARD -i ${TUNFOX} -j ACCEPT

  54. # 如果遇到奇怪的错误(例如无法访问某些网页,而其他网页加载正常),则可能是“路径MTU发现”(Path MTU Discovery)问题
  55. iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu

  56. ## IPTV 的INPUT,OUTPUT,FORWARD都是ACCEPT我也不清楚为什么, 从OPENWRT里抄的, 貌似不需要
  57. # iptables -A FORWARD -i ${IPTV85} -s 0.0.0.0/0 -j ACCEPT
  58. # iptables -A FORWARD -i ${IPTV15} -s 0.0.0.0/0 -j ACCEPT

  59. ## 添加路由规则, 把LAN的数据转发WAN (伪装,适合外网IP地址变化的配置)
  60. iptables -t nat -A POSTROUTING -d 192.168.18.0/24 -o tun1 -j MASQUERADE
  61. iptables -t nat -A POSTROUTING -s 192.168.88.0/24 -o ${WAN} -j MASQUERADE
  62. # iptables -t nat -A POSTROUTING -s 10.0.8.0/24 -o ${LAN} -j MASQUERADE
  63. iptables -t nat -A POSTROUTING -s 10.8.9.0/24 -o ${WAN} -j MASQUERADE

  64. # IPv6
  65. ip6tables -A INPUT -p icmpv6 -j ACCEPT

  66. # 添加默认规则
  67. iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  68. iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

  69. iptables -A INPUT -i ${LAN} -j ACCEPT
  70. iptables -A OUTPUT -o ${LAN} -j ACCEPT

  71. iptables -A INPUT -i ${WG} -j ACCEPT
  72. iptables -A OUTPUT -o ${WG} -j ACCEPT

  73. iptables -A INPUT -i lo -j ACCEPT
  74. iptables -A OUTPUT -o lo -j ACCEPT

  75. iptables -A INPUT -i ${TUNFOX} -j ACCEPT
  76. iptables -A OUTPUT -o ${TUNFOX} -j ACCEPT

  77. iptables -A INPUT -i ${TUNSERVER} -j ACCEPT
  78. iptables -A OUTPUT -o ${TUNSERVER} -j ACCEPT

  79. iptables -A INPUT -i ${WAN} -j DROP
  80. iptables -A OUTPUT -o ${WAN} -j ACCEPT

  81. iptables -A INPUT -i ${IPTV85} -j ACCEPT

复制代码


保存为 firewall-init.sh . 运行它
  1. bash firewall-init.sh

复制代码

配置 DNS 服务器

个人比较喜欢 Dnsmasq, 就以这个举例子. Dnsmasq 本身也有 dhcp 的功能, 所以就不必为 dhcp 单独再开服务了.

vim /etc/dnsmasq.conf

  1. resolv-file=/etc/resolv.dnsmasq.conf
  2. no-resolv

  3. # only for LAN interface
  4. # interface=enp1s0f0
  5. # DHCP ip address range
  6. # no-dhcp-interface=enp1s0f1
  7. dhcp-range=192.168.88.20,192.168.88.99,255.255.255.0,9h
  8. # DHCP 下发配置
  9. # dhcp-option=option:dns-server,192.168.88.1, 114.114.114.114
  10. dhcp-option=option:dns-server,192.168.88.1
  11. dhcp-option=option:router,192.168.88.1
  12. # dhcp-option=option:ntp-server,192.168.88.1
  13. # Broadcast Address
  14. dhcp-option=28,192.168.88.255

  15. dhcp-authoritative

  16. ## IPTV 伪造报文, 非桥应该不需要的
  17. dhcp-option-force=125,00:00:00:00:3a:02:06:48:47:57:2d:43:54:03:04:5a:58:48:4e:0a:02:20:00:0b:02:00:55:0d:02:00:2e
  18. dhcp-option=15
  19. dhcp-option=28
  20. dhcp-option=60,00:00:01:00:02:03:43:50:45:03:0e:45:38:20:47:50:4f:4e:20:52:4f:55:54:45:52:04:03:31:2E:30

  21. # 内网域名解析配置 也可以写到 hosts文件了
  22. # address=/aa.bb.com/192.168.88.8

  23. # NIC MAC address mac与ip绑定
  24. # dhcp-host=b4:2e:99:d7:33:b1,192.168.88.88 # 举个栗子


  25. cache-size=102400

  26. # 上游DNS, 后面配置科学的部分会在xray里拦截这个udp数据包的
  27. server=114.114.114.114
  28. conf-dir=/etc/dnsmasq.d

复制代码

  1. /etc/init.d/dnsmasq restart

复制代码

这个时候, 你的路由器就可以正常工作了
使用Gentoo Linux 手搓 软路由-软路由,x86系统,openwrt(x86),Router OS 等-恩山无线论坛 (2024)
Top Articles
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 5879

Rating: 4.6 / 5 (56 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.