本教程演示如何在 OpenWrt 路由器上配置 OpenConnect VPN,并让 局域网所有设备通过该 VPN 上网。
适用于:
示例环境:
| 项目 | 示例 |
|---|---|
| OpenWrt LAN IP | 192.168.8.1 |
| LAN 网段 | 192.168.8.0/24 |
| VPN 协议 | AnyConnect |
| VPN 服务器 | sslvpn.example.com |
| VPN 端口 | 8443 |
确保:
更新软件源:
opkg update
安装 OpenConnect:
opkg install openconnect luci-proto-openconnect
安装完成后 LuCI 也会支持 OpenConnect。
编辑网络配置:
nano /etc/config/network
添加:
config interface 'ocvpn'
option proto 'openconnect'
option vpn_protocol 'anyconnect'
option server 'sslvpn.example.com'
option port '8443'
option username 'YOUR_USERNAME'
option password 'YOUR_PASSWORD'
option authgroup 'Master Network'
option useragent 'AnyConnect Linux'
option os 'linux'
option defaultroute '1'
option peerdns '0'
参数说明:
| 参数 | 说明 |
|---|---|
| server | VPN 服务器 |
| port | VPN 端口 |
| username | 用户名 |
| password | 密码 |
| authgroup | 认证组(可选) |
| defaultroute | 使用 VPN 作为默认路由 |
/etc/init.d/network restart
查看是否连接成功:
ifconfig
如果看到 tun0,说明 VPN 已连接。
为了让 LAN 设备可以通过 VPN 上网,需要配置防火墙。
编辑:
nano /etc/config/firewall
添加 VPN zone:
config zone
option name 'ocvpn'
option input 'ACCEPT'
option output 'ACCEPT'
option forward 'REJECT'
option masq '1'
option mtu_fix '1'
option network 'ocvpn'
允许 LAN 转发到 VPN:
config forwarding
option src 'lan'
option dest 'ocvpn'
/etc/init.d/firewall restart
在 OpenWrt 上测试:
curl ifconfig.me
在局域网设备访问:
https://ifconfig.me
如果 IP 与 VPN 相同,说明所有设备已经通过 VPN 上网。
如果希望防止设备绕过 VPN,编辑防火墙:
nano /etc/config/firewall
禁用 LAN → WAN:
config forwarding
option src 'lan'
option dest 'wan'
option enabled '0'
重启防火墙:
/etc/init.d/firewall restart
这样 LAN 设备只能通过 VPN 上网。
OpenWrt 默认会自动启动 VPN 接口。
检查接口状态:
ifup ocvpn
手动连接:
ifup ocvpn
断开:
ifdown ocvpn
查看日志:
logread -f
查看接口:
ifstatus ocvpn
检查 NAT:
iptables -t nat -L
确认存在:
MASQUERADE ocvpn
手动设置 DNS,编辑 /etc/config/network,添加:
option dns '8.8.8.8'
现在你的 OpenWrt 已经:
ifup ocvpn
ifdown ocvpn
logread -f
ifconfig
OpenWrt
├── WAN
├── OpenConnect VPN
│ └── ocvpn
└── LAN
├── PC
├── Phone
└── TV
所有设备流量:
LAN → OpenWrt → OpenConnect VPN → Internet