我在让客户端连接到互联网时遇到一些问题。它可以连接到内部网络,但不能连接到互联网。有人知道可能出了什么问题吗?这是我的 OpenVPN 服务器的 iptables:
$ sudo iptables -L -v --line-number
Chain INPUT (policy ACCEPT 53189 packets, 70M bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT udp -- any any anywhere anywhere udp dpt:openvpn
2 137 16324 ACCEPT all -- tun0 any anywhere anywhere
3 4841 586K ACCEPT udp -- wlan0 any anywhere anywhere udp dpt:443
Chain FORWARD (policy ACCEPT 4 packets, 256 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED
2 4274 270K ACCEPT all -- any any 10.8.0.0/24 anywhere
3 0 0 ACCEPT all -- wlan0 tun0 anywhere anywhere
4 0 0 ACCEPT all -- tun0 wlan0 anywhere anywhere
Chain OUTPUT (policy ACCEPT 13229 packets, 1247K bytes)
num pkts bytes target prot opt in out source destination
$ sudo iptables -nvL -t nat
Chain PREROUTING (policy ACCEPT 122 packets, 19337 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 3 packets, 583 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 22 packets, 1386 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 22 packets, 1386 bytes)
pkts bytes target prot opt in out source destination
2621 166K SNAT all -- * * 10.8.0.0/24 !10.8.0.0/24 to:120.xx.xx.xx
0 0 MASQUERADE all -- * wlan0 10.8.0.0/24 0.0.0.0/0
22 1386 all -- * * 0.0.0.0/0 0.0.0.0/0
$ ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
tun0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1500
inet 10.8.0.1 netmask 255.255.255.0 destination 10.8.0.1
inet6 fe80::6f08:6434:23ed:1239 prefixlen 64 scopeid 0x20<link>
unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00 txqueuelen 100 (UNSPEC)
RX packets 1173 bytes 75296 (75.2 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7 bytes 432 (432.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.188 netmask 255.255.255.0 broadcast 192.168.100.255
ether 02:81:05:ba:1b:d4 txqueuelen 1000 (Ethernet)
RX packets 62966 bytes 75908690 (75.9 MB)
RX errors 0 dropped 1012 overruns 0 frame 0
TX packets 18570 bytes 2387345 (2.3 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
另外,sysctl -a
显示net.ipv4.ip_forward = 1
.
更新:更多输出
$ netstat -rn
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 wlan0
10.8.0.0 0.0.0.0 255.255.255.0 U 0 0 0 tun0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 wlan0
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
$ route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 wlan0
10.8.0.0 0.0.0.0 255.255.255.0 U 0 0 0 tun0
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 wlan0
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 wlan0
这是ovpn
:
client
proto udp
remote 120.xx.xx.xx 443
dev tun
resolv-retry infinite
nobind
persist-key
persist-tun
remote-cert-tls server
verify-x509-name server_EjfYsD2SU86zv2oB name
auth SHA256
auth-nocache
cipher AES-128-GCM
tls-client
tls-version-min 1.2
tls-cipher TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
setenv opt block-outside-dns # Prevent Windows 10 DNS leak
verb 3
...
UPDATE2:向 iptables 添加一些规则后,客户端仍然无法访问互联网:
$ sudo iptables -L -v --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT udp -- any any anywhere anywhere udp dpt:openvpn
2 0 0 ACCEPT all -- tun0 any anywhere anywhere
3 1094 134K ACCEPT udp -- wlan0 any anywhere anywhere udp dpt:443
4 721 721K ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED
5 0 0 ACCEPT udp -- any any anywhere anywhere udp dpt:443
6 0 0 ACCEPT tcp -- any any anywhere anywhere tcp dpt:ssh
7 0 0 ACCEPT icmp -- any any anywhere anywhere
8 1 201 REJECT all -- any any anywhere anywhere reject-with icmp-port-unreachable
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT all -- any any anywhere anywhere state RELATED,ESTABLISHED
2 417 26066 ACCEPT all -- any any 10.8.0.0/24 anywhere
3 0 0 ACCEPT all -- wlan0 tun0 anywhere anywhere
4 0 0 ACCEPT all -- tun0 wlan0 anywhere anywhere
Chain OUTPUT (policy ACCEPT 4 packets, 496 bytes)
num pkts bytes target prot opt in out source destination
答案1
除了对数据包进行计数之外,几乎所有iptables
规则都没有任何效果,因为默认策略是ACCEPT
。例外的是您的POSTROUTING
表,它设法无法挽回地破坏数据包。
您需要定义要向 Internet 公开哪些服务(假设您的 120.xxx 可以通过 Internet 访问),并阻止其他所有服务。此示例允许 udp/443、tcp/22 和 icmp:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # Existing connections
iptables -A INPUT -p udp --dport 443 -j ACCEPT # OpenVPN
iptables -A INPUT -p tcp --dport 22 -j ACCEPT # SSH
iptables -A INPUT -p icmp -j ACCEPT # Ping and other ICMP
iptables -A INPUT -j REJECT # Discard everything else
iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE # Hide behind me
有更复杂的示例可用(只需使用“openvpn iptables masquerade”等术语进行搜索),但这应该是让您运行的最低限度。