找回密码
 加入我们
搜索
      
查看: 2192|回复: 19

[网络] 开源的路由器VyOS家用使用配置分享——pppoe拨号获取ipv6上网

[复制链接]
发表于 2025-3-28 15:15 | 显示全部楼层 |阅读模式

  • 前言

各种路由系统教程网上很多,OpenWRT、RouterOS、Opnsense等这类有webui的路由系统已经非常多详细的教程,我也是跟着坛子里的狐狸大佬配置的RouterOS用了很久。但是关于VyOS家用的教程,特别是如何获取ipv6的完整教程比较少,前段时间闲来无事折腾了一版VyOS的配置,由于不同版本的VyOS命令有所不同,配置过程中间找教程比较痛苦,所以把过程记录一下,供有需要的小伙伴取用。
需要说明的是,本帖只是我个人使用环境下可以正常使用的配置,具体需要根据网络环境自行合理调整,如有配置不合理的地方,欢迎各位大佬批评指正。
由于VyOS没有webui界面,以下所有内容均为安装好系统后用ssh连接机器进行配置,安装教程这里就不展开分享了。
---------------下面进入代码分享---------------
  • 接口配置

为了尽量贴近我所认为的家用环境,本次教程拟用一个WAN口eth0进行pppoe拨号,两个LAN口eth1、eth2做桥接为br1作为LAN口供局域网使用。
首先是为各物理网口开启硬件优化(如 offload、ring-buffer 等)
  1. # 配置物理网卡 eth0, eth1, eth2
  2. ## eth0
  3. set interfaces ethernet eth0 hw-id '00:xx:x:x:x:x'
  4. set interfaces ethernet eth0 offload gro
  5. set interfaces ethernet eth0 offload gso
  6. set interfaces ethernet eth0 offload hw-tc-offload
  7. set interfaces ethernet eth0 offload lro
  8. set interfaces ethernet eth0 offload rfs
  9. set interfaces ethernet eth0 offload rps
  10. set interfaces ethernet eth0 offload sg
  11. set interfaces ethernet eth0 offload tso
  12. set interfaces ethernet eth0 ring-buffer rx '4096'
  13. set interfaces ethernet eth0 ring-buffer tx '4096'

  14. ## eth1
  15. set interfaces ethernet eth1 hw-id '00:0x:xx:xx:xx:xx'
  16. set interfaces ethernet eth1 offload gro
  17. set interfaces ethernet eth1 offload gso
  18. set interfaces ethernet eth1 offload hw-tc-offload
  19. set interfaces ethernet eth1 offload lro
  20. set interfaces ethernet eth1 offload rfs
  21. set interfaces ethernet eth1 offload rps
  22. set interfaces ethernet eth1 offload sg
  23. set interfaces ethernet eth1 offload tso
  24. set interfaces ethernet eth1 ring-buffer rx '4096'
  25. set interfaces ethernet eth1 ring-buffer tx '4096'

  26. ## eth2
  27. set interfaces ethernet eth2 hw-id '00:0c:9x:xx:xx:xx'
复制代码


接着我们为LAN口创建网桥,网段为172.16.0.1/24
  1. # 配置 LAN 网桥 br1
  2. set interfaces bridge br1 address '172.16.0.1/24'
  3. set interfaces bridge br1 description 'LAN bridge'
  4. set interfaces bridge br1 member interface eth1
  5. set interfaces bridge br1 member interface eth2
复制代码

然后是WAN口的pppoe拨号设置,这里根据ISP提供的前缀长度进行调整,我这边用的是64位
  1. set interfaces pppoe pppoe0 authentication password 'password'
  2. set interfaces pppoe pppoe0 authentication username 'username_of_pppoe'
  3. set interfaces pppoe pppoe0 description 'China Unicom'
  4. set interfaces pppoe pppoe0 dhcpv6-options pd 0 interface br1 address '1'
  5. set interfaces pppoe pppoe0 dhcpv6-options pd 0 interface br1 sla-id '0'
  6. set interfaces pppoe pppoe0 dhcpv6-options pd 0 length '64'
  7. set interfaces pppoe pppoe0 ip adjust-mss 'clamp-mss-to-pmtu'
  8. set interfaces pppoe pppoe0 ipv6 address autoconf
  9. set interfaces pppoe pppoe0 ipv6 adjust-mss 'clamp-mss-to-pmtu'
  10. set interfaces pppoe pppoe0 mtu '1492'
  11. set interfaces pppoe pppoe0 source-interface 'eth0'
复制代码
至此,不出意外的话应该可以拨号成功并获取ip地址了。
  • NAT配置

为了顺利上网,我们还需要给LAN设备做NAT地址转换将内网 `172.16.0.0/24` 出口做源地址转换为外网 IP。
rule 100:匹配源地址 `172.16.0.0/24`,通过 `pppoe0` 出口时,进行地址伪装(MASQUERADE)。
  1. set nat source rule 100 outbound-interface name 'pppoe0'
  2. set nat source rule 100 source address '172.16.0.0/24'
  3. set nat source rule 100 translation address 'masquerade'
复制代码
  • DHCP配置

接着我们继续在br1上开启 进行DHCP为局域网内的设备进行自动分配IP地址、网关、DNS、NTP 等。这里用的是VyOS自身作为NTP服务器。
分配ip地址范围为100-200网段。

  1. set service dhcp-server dynamic-dns-update
  2. set service dhcp-server hostfile-update
  3. set service dhcp-server listen-interface 'br1'
  4. set service dhcp-server shared-network-name LAN_br1 authoritative

  5. # 1) DHCP 选项
  6. set service dhcp-server shared-network-name LAN_br1 option ntp-server '172.16.0.1'
  7. set service dhcp-server shared-network-name LAN_br1 option time-zone 'Asia/Shanghai'

  8. # 2) DHCP 子网 172.16.0.0/24
  9. set service dhcp-server shared-network-name LAN_br1 subnet 172.16.0.0/24 lease '86400'
  10. set service dhcp-server shared-network-name LAN_br1 subnet 172.16.0.0/24 option default-router '172.16.0.1'
  11. set service dhcp-server shared-network-name LAN_br1 subnet 172.16.0.0/24 option name-server '172.16.0.1'
  12. set service dhcp-server shared-network-name LAN_br1 subnet 172.16.0.0/24 range 100 start '172.16.0.100'
  13. set service dhcp-server shared-network-name LAN_br1 subnet 172.16.0.0/24 range 100 stop '172.16.0.200'
复制代码
  • DHCP配置静态映射

为了方便管理,也可以对局域网内的设备ip地址配置静态映射,只需要把mac地址跟ip地址绑定即可。VyOS需要两行命令组合。

  1. set service dhcp-server shared-network-name LAN_br1 subnet 172.16.0.0/24 static-mapping Device1 ip-address '172.16.0.11'
  2. set service dhcp-server shared-network-name LAN_br1 subnet 172.16.0.0/24 static-mapping Device1 mac '00:0x:xx:xx:xx:xx'
复制代码
  • 配置IPv6路由通告(RA)

为了使局域网内的设备能正常获取IPv6地址,需要在 br1上做 IPv6 路由通告,前缀长度 ::/64,有效期 172800`秒。pppoe0也启用 RA 服务,具体根据运营商需求自动获取前缀。

  1. set service router-advert interface br1 link-mtu '1490'
  2.     set service router-advert interface br1 prefix ::/64 valid-lifetime '172800'
  3.     set service router-advert interface pppoe0
复制代码
  • 配置DNS转发

在上一步配置DHCP时,为局域网内的设备配置的是172.16.0.1作为DNS服务器,但实现DNS解析,还需要在VyOS上打开监听服务。

  1. set service dns forwarding allow-from '172.16.0.0/24'
  2.     set service dns forwarding listen-address '172.16.0.1'
  3.     set service dns forwarding name-server 114.114.114.114
  4.     set service dns forwarding name-server 223.5.5.5
复制代码
  • 配置NTP服务

NTP家用一般比较少用,大部分是用公网NTP服务为主,这里配置根据需要取用。

  1. set service ntp allow-client address '127.0.0.0/8'
  2. set service ntp allow-client address '169.254.0.0/16'
  3. set service ntp allow-client address '10.0.0.0/8'
  4. set service ntp allow-client address '172.16.0.0/12'
  5. set service ntp allow-client address '192.168.0.0/16'
  6. set service ntp allow-client address '::1/128'
  7. set service ntp allow-client address 'fe80::/10'
  8. set service ntp server ntp.aliyun.com
  9. set service ntp server ntp.tencent.com
复制代码
-------------------以下是本次配置的重点内容:防火墙-------------------
经过上面的配置,内网设备通过VyOS应该可以正常获取IP地址并访问外网了,为了在冲浪的时候更加安全,这里也分享一下我个人使用的防火墙配置。
注意,在防火墙没配置完成前,不要commit,否则可能失联。
  • 全局选项

允许路由器响应广播 ping 请求(如 `ping 255.255.255.255`),也可以用于排查网络故障。若不需要,可不启用。
  1. set firewall global-options all-ping 'enable'
复制代码


  • IPv4防火墙规则

VYOS防火墙是基zone配置的,这里将防火墙分为LAN、WAN、LOCAL三个zone,各zone之间的防火墙规则如下表:

防火墙名称 默认策略 规则号 动作 备注
LAN-LOCAL accept 内部 LAN 到本地的流量全部允许
LAN-WAN accept 内部 LAN 到外网的流量全部允许
LOCAL-LAN accept 本地到内部 LAN 的流量全部允许
LOCAL-WAN accept 本地到外网的流量全部允许
WAN-LAN drop1 accept (established/related)   允许外部返回的已建立连接
WAN-LAN drop 10 accept (tcp_udp, dst=172.16.0.0/24) 允许外部访问内网的 TCP/UDP 并记录日志
WAN-LOCAL drop 1 accept (established/related) 允许外部返回的已建立连接
WAN-LOCAL drop 10 accept (icmp) 允许外部对路由器的 ping


上述规则默认LAN与LOCAL互访为accept,而WAN到LAN和LOCAL则默认drop

  • IPv4防火墙规则

以下规则定义了 VyOS 内置的防火墙名称及其默认动作,根据上表对特定流量的放行或拒绝。

  1. # 1) 定义 IPv4 防火墙名称与默认策略
  2. set firewall ipv4 name LAN-LOCAL default-action 'accept'
  3. set firewall ipv4 name LAN-WAN   default-action 'accept'
  4. set firewall ipv4 name LOCAL-LAN default-action 'accept'
  5. set firewall ipv4 name LOCAL-WAN default-action 'accept'

  6. # 2) WAN-LAN:默认丢弃(drop),仅允许特定流量
  7. set firewall ipv4 name WAN-LAN default-action 'drop'
  8. ## 允许已建立/相关连接通过
  9. set firewall ipv4 name WAN-LAN rule 1 action 'accept'
  10. set firewall ipv4 name WAN-LAN rule 1 state 'established'
  11. set firewall ipv4 name WAN-LAN rule 1 state 'related'
  12. ## 允许访问 172.16.0.0/24 的 TCP/UDP 流量并记录日志
  13. set firewall ipv4 name WAN-LAN rule 10 action 'accept'
  14. set firewall ipv4 name WAN-LAN rule 10 destination address '172.16.0.0/24'
  15. set firewall ipv4 name WAN-LAN rule 10 log
  16. set firewall ipv4 name WAN-LAN rule 10 protocol 'tcp_udp'

  17. # 3) WAN-LOCAL:默认丢弃(drop),仅允许特定流量
  18. set firewall ipv4 name WAN-LOCAL default-action 'drop'
  19. ## 允许已建立/相关连接通过
  20. set firewall ipv4 name WAN-LOCAL rule 1 action 'accept'
  21. set firewall ipv4 name WAN-LOCAL rule 1 state 'established'
  22. set firewall ipv4 name WAN-LOCAL rule 1 state 'related'
  23. ## 允许 ICMP 流量(如 ping 等)
  24. set firewall ipv4 name WAN-LOCAL rule 10 action 'accept'
  25. set firewall ipv4 name WAN-LOCAL rule 10 protocol 'icmp'
复制代码
  • IPv6防火墙规则

与IPv4配置类似,IPv6只是把命令行的IPv4改成IPv6即可,具体规则如下:

防火墙名称 默认策略 规则号 动作备注
LAN-LOCAL-6 accept LAN 到本地全部允许
LAN-WAN-6 accept LAN 到外网全部允许
LOCAL-LAN-6 accept 本地到 LAN 全部允许
LOCAL-WAN-6 accept 本地到外网全部允许
WAN-LAN-6 drop 1 accept (established/related) 允许外部返回的 IPv6 已建立连接
WAN-LOCAL-6 drop 1 accept (established/related) 允许外部返回的 IPv6 已建立连接
WAN-LOCAL-6 drop 10 accept (ipv6-icmp) 允许外部对路由器的 IPv6 ping
WAN-LOCAL-6 drop 20 accept (DHCPv6, UDP 547 -> 546) 允许 DHCPv6 客户端/服务器流量
以下是配置命令
  1. # 1) 定义 IPv6 防火墙名称与默认策略
  2. set firewall ipv6 name LAN-LOCAL-6 default-action 'accept'
  3. set firewall ipv6 name LAN-WAN-6   default-action 'accept'
  4. set firewall ipv6 name LOCAL-LAN-6 default-action 'accept'
  5. set firewall ipv6 name LOCAL-WAN-6 default-action 'accept'

  6. # 2) WAN-LAN-6:默认丢弃(drop)
  7. set firewall ipv6 name WAN-LAN-6 default-action 'drop'
  8. ## 允许已建立/相关的 IPv6 流量
  9. set firewall ipv6 name WAN-LAN-6 rule 1 action 'accept'
  10. set firewall ipv6 name WAN-LAN-6 rule 1 state 'established'
  11. set firewall ipv6 name WAN-LAN-6 rule 1 state 'related'

  12. # 3) WAN-LOCAL-6:默认丢弃(drop)
  13. set firewall ipv6 name WAN-LOCAL-6 default-action 'drop'
  14. ## 允许已建立/相关的 IPv6 流量
  15. set firewall ipv6 name WAN-LOCAL-6 rule 1 action 'accept'
  16. set firewall ipv6 name WAN-LOCAL-6 rule 1 state 'related'
  17. set firewall ipv6 name WAN-LOCAL-6 rule 1 state 'established'
  18. ## 允许 IPv6 ICMP(如 ping6)
  19. set firewall ipv6 name WAN-LOCAL-6 rule 10 action 'accept'
  20. set firewall ipv6 name WAN-LOCAL-6 rule 10 protocol 'ipv6-icmp'
  21. ## 允许 DHCPv6 Client/Server 的必要流量 (端口 546/547)
  22. set firewall ipv6 name WAN-LOCAL-6 rule 20 action 'accept'
  23. set firewall ipv6 name WAN-LOCAL-6 rule 20 destination port '546'
  24. set firewall ipv6 name WAN-LOCAL-6 rule 20 protocol 'udp'
  25. set firewall ipv6 name WAN-LOCAL-6 rule 20 source port '547'
复制代码
  • 将防火墙规则应用至接口

配置好防火墙规则,我们还需要将其应用到接口,定义各区域才能生效
ZONE接口
LAN br1
WANpppoe0
LOCALlo

  1. # 1) LAN Zone
  2. set firewall zone LAN from LOCAL firewall ipv6-name 'LOCAL-LAN-6'
  3. set firewall zone LAN from LOCAL firewall name 'LOCAL-LAN'
  4. set firewall zone LAN from WAN firewall ipv6-name 'WAN-LAN-6'
  5. set firewall zone LAN from WAN firewall name 'WAN-LAN'
  6. set firewall zone LAN member interface 'br1'

  7. # 2) LOCAL Zone
  8. set firewall zone LOCAL from LAN firewall ipv6-name 'LAN-LOCAL-6'
  9. set firewall zone LOCAL from LAN firewall name 'LAN-LOCAL'
  10. set firewall zone LOCAL from WAN firewall ipv6-name 'WAN-LOCAL-6'
  11. set firewall zone LOCAL from WAN firewall name 'WAN-LOCAL'
  12. set firewall zone LOCAL local-zone

  13. # 3) WAN Zone
  14. set firewall zone WAN from LAN firewall ipv6-name 'LAN-WAN-6'
  15. set firewall zone WAN from LAN firewall name 'LAN-WAN'
  16. set firewall zone WAN from LOCAL firewall ipv6-name 'LOCAL-WAN-6'
  17. set firewall zone WAN from LOCAL firewall name 'LOCAL-WAN'
  18. set firewall zone WAN member interface 'pppoe0'
复制代码
至此,防火墙配置完毕,可以commit confirm来确认是否生效。建议虚拟机配置的话配置前打个快照,方便随时回滚重新配置。
  • 其他系统配置


commit-revisions '100':最多保留 100 个配置版本。
host-name 'vyos':主机名。
time-zone:设置时区为 `Asia/Shanghai`。
syslog:日志级别设置。

  1. set system config-management commit-revisions '100'
  2. set system host-name 'vyos'
  3. set system name-server '116.116.116.116'
  4.     set system syslog local facility all level 'info'
  5.     set system syslog local facility local7 level 'debug'
  6.     set system time-zone 'Asia/Shanghai'
复制代码
-------------------结束-------------------

上述配置命令供各位坛友参考,希望能对想玩VyOS但找不到教程的朋友带来一点帮助,后续计划分享一下在VyOS用容器跑adguard home跟策略路由分流的玩法。
发表于 2025-3-28 15:17 来自手机 | 显示全部楼层
很不错,多搞点教程让更多的人玩起来
发表于 2025-3-28 15:23 | 显示全部楼层
点赞!我在pve里配过一个vyos的虚拟机,但是不会配ipv6。这个系统就是拿来折腾的,家用根本体验不到有任何的快感,就是觉得好累。哈哈哈。。。
发表于 2025-3-28 15:25 | 显示全部楼层
感谢分享,学习学习
发表于 2025-3-28 15:55 | 显示全部楼层
不知道大佬用的什么版本,有没有试过这几个命令,state全局设置就行了,避免重复设置,但是我在1.4.1下用着很正常,在1.5 Stream下就不行

  1. # 1.4正常/1.5目前执行会连不上
  2. set firewall global-options state-policy established action accept
  3. set firewall global-options state-policy related action accept
  4. set firewall global-options state-policy invalid action drop
复制代码
发表于 2025-3-28 16:00 | 显示全部楼层
这个比openwrt有啥优势呢?
发表于 2025-3-28 16:06 | 显示全部楼层
没图形界面那就真的门槛高了
发表于 2025-3-28 16:10 | 显示全部楼层
硬核,期待分流教程
 楼主| 发表于 2025-3-28 16:12 | 显示全部楼层
coolbo 发表于 2025-3-28 15:55
不知道大佬用的什么版本,有没有试过这几个命令,state全局设置就行了,避免重复设置,但是我在1.4.1下用着 ...

问的好,我是从1.5开始用的,哈哈哈。你说的这个命令我没试过,回头看看行不行
 楼主| 发表于 2025-3-28 16:13 | 显示全部楼层
二手烟 发表于 2025-3-28 15:23
点赞!我在pve里配过一个vyos的虚拟机,但是不会配ipv6。这个系统就是拿来折腾的,家用根本体验不到有任何 ...

确实,对我而言快感就是能上网了。然后索然无味
发表于 2025-3-28 16:23 | 显示全部楼层
mmjasmm 发表于 2025-3-28 16:13
确实,对我而言快感就是能上网了。然后索然无味

请问vyos默认nat类型是什么,可以设置么?
发表于 2025-3-28 16:54 | 显示全部楼层
没有webui这个拿来家用的话入门门槛还挺高的
支持技术贴,不过我还是蹲在OP好了
 楼主| 发表于 2025-3-28 19:25 | 显示全部楼层
小把戏 发表于 2025-3-28 16:23
请问vyos默认nat类型是什么,可以设置么?

我目前的网络是nat4,用爱快跟ros试过设置类型,没有效果,vyos我没试过,所以也不知道
发表于 2025-3-30 14:10 | 显示全部楼层
mmjasmm 发表于 2025-3-28 16:12
问的好,我是从1.5开始用的,哈哈哈。你说的这个命令我没试过,回头看看行不行
...

很多人遇到问题
https://forum.vyos.io/t/have-to- ... am-2025-q1/16131/10

https://www.reddit.com/r/vyos/co ... os_firewall_breaks/

https://vyos.dev/T7150
发表于 2025-3-31 09:46 | 显示全部楼层
感谢分享
发表于 2025-3-31 12:22 | 显示全部楼层
不知道大佬用不用openvxn作为remote access,认证这块是怎么做的,只认证证书还是证书+用户名密码

vyos有个很坑的是ipsec即支持radius也支持本地用户认证
radius:
  1. set ** ipsec remote-access connection rw authentication client-mode 'eap-radius'
  2. set ** ipsec remote-access radius server 192.0.2.2 key 'secret'
复制代码


local:
  1. set ** ipsec remote-access connection rw authentication client-mode 'eap-mschapv2'
  2. set ** ipsec remote-access connection rw authentication local-users username vyos password 'vyos'
复制代码


但是openvxn官方之支持ldap,没有本地用户的支持
  1. set interfaces open** vtun0 open**-option "--plugin /usr/lib/open**/open**-auth-ldap.so /config/auth/ldap-auth.config"
复制代码

发表于 2025-3-31 13:49 | 显示全部楼层
可以做个配置脚本,方便部署和维护
发表于 2025-3-31 13:51 | 显示全部楼层
另外,对没有webui的路由系统不是很理解,和直接用debian有什么不同?
发表于 2025-3-31 18:26 来自手机 | 显示全部楼层
IceyHeart 发表于 2025-3-31 13:51
另外,对没有webui的路由系统不是很理解,和直接用debian有什么不同?

就是有配置管理,还有就是最佳实践
发表于 2025-3-31 20:05 | 显示全部楼层
我还是喜欢用代UI界面的·
这种纯代码 尽管知道都需要前前后后的对比·· 挺烦的·
NN年前那些3层交换机就是· ·
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

Archiver|手机版|小黑屋|Chiphell ( 沪ICP备12027953号-5 )沪公网备310112100042806 上海市互联网违法与不良信息举报中心

GMT+8, 2025-4-20 15:40 , Processed in 0.013441 second(s), 5 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2007-2024 Chiphell.com All rights reserved.

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