OpenVPN | 如何使服务器通过OpenVPN网关与局域网中的另一台服务器进行通信?

我一直在尝试EC2实例(OMD)通过另一个EC2实例(OpenVPN)与我的LAN服务器(Raspberry Pi)进行通信,但是我无法使其工作。

即使SSH设置是默认的,没有防火墙,OMD服务器也可以ping RPi,但不能通过SSH连接。 端口6556是可以访问的。

从OMD服务器端口扫描

[root@omd ~]# nc -zvv 192.168.16.150 6556 Connection to 192.168.16.150 6556 port [tcp/*] succeeded! [root@omd ~]# nc -zvv 192.168.16.150 22 nc: connect to 192.168.16.150 port 22 (tcp) failed: Connection timed out [root@omd ~]# 

RPi的22和6556对所有人都是开放的,但为什么OMD不能SSH呢?

 root@rpi:~# netstat -tunlp | egrep "6556|22" tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 535/sshd tcp 0 0 0.0.0.0:6556 0.0.0.0:* LISTEN 743/xinetd tcp6 0 0 :::22 :::* LISTEN 535/sshd root@rpi:~# 

这是我一直在努力实现的。 请看这个链接

  1. OpenVPN和RPi通过VPN连接相互连接
  2. OMD不会连接到VPN,它只会使用OpenVPN服务器作为网关
  3. OMD将通过OpenVPN服务器与RPi进行通信,反之亦然

你能帮我一下吗?

请让我知道,如果你需要更多的信息。

在此先感谢你们!

AWS – OMD
eth0:10.0.0.4

 ======================= PING ======================= [root@omd ~]# ping -c 3 192.168.16.150 PING 192.168.16.150 (192.168.16.150) 56(84) bytes of data. 64 bytes from 192.168.16.150: icmp_seq=1 ttl=63 time=100 ms 64 bytes from 192.168.16.150: icmp_seq=2 ttl=63 time=100 ms 64 bytes from 192.168.16.150: icmp_seq=3 ttl=63 time=159 ms --- 192.168.16.150 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2159ms rtt min/avg/max/mdev = 100.072/119.877/159.357/27.917 ms [root@omd ~]# ======================= TRACEROUTE ======================= [root@omd ~]# traceroute 192.168.16.150 traceroute to 192.168.16.150 (192.168.16.150), 30 hops max, 60 byte packets 1 * * * 2 * * * 3 * * * 4 * * * 5 * * * 6 * * * 7 * * * 8 * * * 9 * * * 10 * * * 11 * * * 12 * * * 13 * * * 14 * * * 15 * * * 16 * * * 17 * * * 18 * * * 19 * * * 20 * * * 21 * * * 22 * * * 23 * * * 24 * * * 25 * * * 26 * * * 27 * * * 28 * * * 29 * * * 30 * * * [root@omd ~]# ======================= ROUTE TABLE ======================= [root@omd ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 192.168.16.0 10.0.0.5 255.255.255.0 UG 0 0 0 eth0 172.17.0.0 10.0.0.5 255.255.255.0 UG 0 0 0 eth0 0.0.0.0 10.0.0.1 0.0.0.0 UG 0 0 0 eth0 [root@omd ~]# 

AWS – OpenVPN
eth0:10.0.0.5
tun0:172.17.0.1

 ======================= PING ======================= [root@openpvn ~]# ping -c 3 10.0.0.4 PING 10.0.0.4 (10.0.0.4) 56(84) bytes of data. 64 bytes from 10.0.0.4: icmp_seq=1 ttl=64 time=0.502 ms 64 bytes from 10.0.0.4: icmp_seq=2 ttl=64 time=0.639 ms 64 bytes from 10.0.0.4: icmp_seq=3 ttl=64 time=0.570 ms --- 10.0.0.4 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 1999ms rtt min/avg/max/mdev = 0.502/0.570/0.639/0.059 ms [root@openpvn ccd]# ping -c 3 192.168.16.150 PING 192.168.16.150 (192.168.16.150) 56(84) bytes of data. 64 bytes from 192.168.16.150: icmp_seq=1 ttl=64 time=173 ms 64 bytes from 192.168.16.150: icmp_seq=2 ttl=64 time=142 ms 64 bytes from 192.168.16.150: icmp_seq=3 ttl=64 time=120 ms --- 192.168.16.150 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 120.684/145.486/173.209/21.546 ms [root@openpvn ~]# ======================= ROUTE ======================= [root@openvpn ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 10.0.0.1 0.0.0.0 UG 0 0 0 eth0 10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 169.254.169.254 0.0.0.0 255.255.255.255 UH 0 0 0 eth0 172.17.0.0 0.0.0.0 255.255.255.0 U 0 0 0 tun0 192.168.16.0 172.17.0.2 255.255.255.0 UG 0 0 0 tun0 [root@openvpn ~]# ======================= IPTABLES ======================= [root@openvpn ~]# cat /etc/sysconfig/iptables *nat :POSTROUTING ACCEPT [0:0] :PREROUTING ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A POSTROUTING -s 172.17.0.0/24 -d 0.0.0.0/0 -o eth0 -j MASQUERADE COMMIT [root@openvpn ~]# ======================= SYSCTL ======================= [root@openvpn ~]# grep forward /etc/sysctl.conf # Controls IP packet forwarding net.ipv4.ip_forward = 1 [root@openvpn ~]# 

局域网 – 树莓派
eth0:192.168.16.150
tun0:172.17.0.253

 ======================= PING ======================= root@rpi:~# ping -c 3 10.0.0.4 PING 10.0.0.4 (10.0.0.4) 56(84) bytes of data. 64 bytes from 10.0.0.4: icmp_seq=1 ttl=63 time=128 ms 64 bytes from 10.0.0.4: icmp_seq=2 ttl=63 time=106 ms 64 bytes from 10.0.0.4: icmp_seq=3 ttl=63 time=126 ms --- 10.0.0.4 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 106.837/120.409/128.312/9.644 ms root@rpi:~# ======================= TRACEROUTE ======================= root@rpi:~# traceroute 10.0.0.4 traceroute to 10.0.0.4 (10.0.0.4), 30 hops max, 60 byte packets 1 172.17.0.1 (172.17.0.1) 177.150 ms 200.416 ms 199.949 ms 2 10.0.0.4 (10.0.0.4) 205.052 ms 216.804 ms 223.456 ms root@rpi:~# ======================= ROUTE TABLE ======================= root@rpi:~# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.17.0.1 128.0.0.0 UG 0 0 0 tun0 0.0.0.0 192.168.8.1 0.0.0.0 UG 0 0 0 eth1 0.0.0.0 192.168.16.254 0.0.0.0 UG 202 0 0 eth0 0.0.0.0 192.168.8.1 0.0.0.0 UG 203 0 0 eth1 5X.XX.XX.XXX 192.168.8.1 255.255.255.255 UGH 0 0 0 eth1 128.0.0.0 172.17.0.1 128.0.0.0 UG 0 0 0 tun0 172.17.0.0 0.0.0.0 255.255.255.0 U 0 0 0 tun0 192.168.8.0 0.0.0.0 255.255.255.0 U 203 0 0 eth1 192.168.16.0 0.0.0.0 255.255.255.0 U 202 0 0 eth0 root@rpi:~# 

鉴于我看到的图表是指定您正在使用tun适配器,这意味着路由networking,而不是第2层VPNnetworking。 您需要让IP转发正常工作。 另外,为了保证您的情况,您还需要在VPN服务器上执行一些NAT操作,这是因为ssh协议涉及的安全性,以确保stream量通过相同的服务器跳转来回。

有这样的工作有几件。 以下所有内容都是针对一个稍旧的CentOS系统。

在连接有LAN的任何OpenVPN(客户机或服务器)机器上设置IP转发。

  • vi / etc / sysconfig / network并添加行FORWARD_IPV4 = true
  • vi /etc/sysctl.conf并更改net.ipv4.ip_forward = 1

要设置所需的NAT,如果您使用iptables作为防火墙,则在连接有LAN的任何OpenVPN(客户端或服务器)计算机上都需要这些命令。

  • iptables –table nat –append POSTROUTING –out-interface eth0 -j MASQUERADE
  • iptables –table nat –append POSTROUTING –out-interface tun0 -j MASQUERADE

禁用防火墙会破坏它。 你也可以使用firewalld,但是我没有这个方便的命令。 另外,一旦满足iptables规则,请记住保存它们,以便通过重新启动来坚持。

再加上VPN的每一端知道在VPN另一端的LAN上路由stream量,以便通过服务器需要的隧道

  • configuration线将每个远程站点的remote_network子网连接到一个连接了LAN的远程networking子网 ,以便本地服务器知道通过该通道路由去往这些子网的通信量。
  • configuration线推送“路由networking子网”,以便每个连接客户端知道通过隧道为这些子网路由stream量。 这将包括服务器LAN和所有客户端LAN。
  • configuration线client-config-dir ccd,所以服务器知道检查文件夹/ etc / openvpn / ccd中是否有带有局域网的客户机名
  • ccd /客户端名称文件与内容iroutenetworking子网,所以VPN软件知道当客户端连接时,该子网的stream量将被发送到该客户端。

当你有多个客户端局域网连接时,所有这些线路都是必需的,或者至less是特别有用的。 当只有一个客户端连接到一台服务器时,这是非常简单的。 一个局域网到局域网并不多。