Linux

只能 ping OpenVPN 伺服器,不能 ping VPN 網路上的其他訪客(僅主機網路)

  • June 5, 2020

我在伺服器 (XXXX/32) 上成功安裝了 OpenVPN 伺服器。這是我的 OpenVPN 配置文件:

port 1194
proto udp6
dev tun

ca /etc/openvpn/keys/ca.crt
cert /etc/openvpn/keys/server.crt
key /etc/openvpn/keys/server.key
dh /etc/openvpn/keys/dh.pem
crl-verify /etc/openvpn/keys
crl-verify /etc/openvpn/keys/ca-crl.pem
tls-auth /etc/openvpn/keys/ta.key 0
tls-server
auth SHA256
cipher AES-256-CBC
tls-version-min 1.2
tls-cipher TLS-ECDHE-ECDSA-WITH-AES-256-GCM-SHA384:TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-ECDHE-ECDSA-WITH-AES-256-CBC-SHA384:TLS-ECDHE-RSA-WITH-AES-256-CBC-SHA384:TLS-DHE-RSA-WITH-AES-256-CBC-SHA256

server 10.109.0.0 255.255.0.0
ifconfig-pool-persist ipp.txt

push "route 192.168.59.0 255.255.255.0"
keepalive 5 30
compress lzo
persist-key
persist-tun
user nobody
group nogroup


status openvpn-status.log
status-version 1
log-append /var/log/openvpn.log
verb 3

在該伺服器(XXXX/32)上,我有一台 vagrant 機器在(僅主機網路)專用網路(vm.network ‘private_network’)中執行,ip 為 192.168.59.2。

從我的本地主機連接到 OpenVPN 後,我想 ping 192.168.59.2。我只能ping 192.168.59.1。如您所見,Vagrant 專用網路和 OpenVPN 網路不同。請問有人可以幫忙解決這個問題嗎?我還附上了我的 ovpn(片段)文件:

tls-client
auth SHA256
cipher AES-256-CBC
remote-cert-tls server
tls-version-min 1.2

proto udp
remote X.X.X.X 1194
dev tun
resolv-retry 5
nobind
keepalive 5 30
compress lzo
persist-key
persist-tun
verb 3
route-method exe
route-delay 2
key-direction 1

EDIT1:我正在使用 192.168.59.2 因為 vagrant 顯示警告vagrant up

guest: You assigned a static IP ending in ".1" to this machine. ==> guest: This is very often used by the router and can cause the ==> guest: network to not work properly. If the network doesn't work ==> guest: properly, try changing this IP.

Vagrant 機器上的 route -n 輸出:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.2.2        0.0.0.0         UG    100    0        0 enp0s3
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
10.0.2.2        0.0.0.0         255.255.255.255 UH    100    0        0 enp0s3
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
192.168.59.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s8

EDIT2:Vagrant 的機器 ip 更改為192.168.59.1. 我還在 ovpn 文件中添加了以下行: route 192.168.59.0 255.255.255.0 從 localhost 我現在可以192.168.59.1在連接到 vpn 後 ping,但我仍然無法 ping 192.168.59.2

好的,我找到了解決方案。我的流浪機器(虛擬機提供商)位於伺服器(XXXX/32)上,IP 為 192.168.59.2 的專用網路上。

如果我想從我的 localhost ping 192.168.59.2,在連接到 vpn 後,我需要先在 vagrant 機器內部執行以下命令:

ip route add 10.109.0.0/16 via 192.168.59.1 dev <VARGRANT_NETWORK_INTERFACE>

在哪裡:

  • 10.109.0.0 = openvpn 網路
  • 192.168.59.1 = vboxnet* ip
  • VARGRANT_NETWORK_INTERFACE = 在我的情況下是enp0s8

如果您希望您的 VPN 通過 VPN 伺服器主機與其他主機通信,您需要在您的 VPN 伺服器主機上啟用 IP 轉發(路由):

sysctl -w net.ipv4.ip_forward=1

看到這個答案

引用自:https://unix.stackexchange.com/questions/569336