Open×××服务器的搭建:http://qicheng0211.blog.51cto.com/3958621/1575273

    CentOS下Open×××客户端配置:http://qicheng0211.blog.51cto.com/3958621/1840055

应用一、点对点安全通道

    工作中可能会遇到这样的场景:由于业务需要,异地两台服务器需要安全的相互访问,除了拉专线,比较经济的方法就是通过公网建立加密隧道,openvpn是一个很好的选择。

    服务端:内网IP 192.168.20.220,vpn服务端口1194通过防火墙映射到公网。

    客户端:内网IP 192.168.1.220。

    点对点网络的配置主要在于服务端,客户端无需特殊配置。

    编辑服务端配置文件server.conf:

shell> vim /etc/openvpn/server.conf
port 1194
proto tcp
dev tun
ca keys/ca.crt
cert keys/server.crt
key keys/server.key  # This file should be kept secret
dh keys/dh2048.pem
server 10.8.0.0 255.255.255.0         # 虚拟局域网网段,不要和实际的局域网冲突
push "route 192.168.20.220 255.255.255.255" # 推送给客户端的路由设置(服务端的IP/32)
route 192.168.1.220 255.255.255.255      # 服务端到客户端的路由(客户端的IP/32)
client-config-dir /etc/openvpn/ccd       # 客户端独立配置文件目录
keepalive 10 120
tls-auth keys/ta.key 0 # This file is secret
comp-lzo
persist-key
persist-tun
status openvpn-status.log
log-append  openvpn.log
verb 5

    在服务器上编辑client1的独立配置文件:

shell> mkdir /etc/openvpn/ccd
shell> vim /etc/openvpn/ccd/client1
iroute 192.168.1.220 255.255.255.255

    客户端配置文件client.ovpn:

client
dev tun
proto tcp
remote xxx.xxx.xxx.xxx 1194 # openvpn服务端映射到公网的IP和端口
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert client1.crt # 客户端client1的证书文件
key client1.key  # 客户端client1的密钥文件
remote-cert-tls server
tls-auth ta.key 1
comp-lzo
verb 3

    openvpn服务端和客户端都重启一下。服务端增加的的路由:

OpenVPN应用案例_open

    客户端增加的路由:

OpenVPN应用案例_vpn_02

    测试两端互ping、互相登录都没问题。

应用、客户端安全接入服务器局域网

   情景:IDC机房网络有两个网段(vlan20、40),各vlan互通,现需要一个运维跳板机,运维通过跳板机管理vlan20和vlan40服务器。

    为实现上述目的,我们在机房内搭建一台openvpn服务器,客户端通过vpn服务器接入IDC机房内网。

    服务端配置文件server.conf:

port 1194
proto tcp
dev tun
ca keys/ca.crt
cert keys/server.crt
key keys/server.key  # This file should be kept secret
dh keys/dh2048.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "route 192.168.20.0 255.255.254.0" # 推送给客户端的路由设置(vlan20:192.168.20.0/23)
push "route 192.168.40.0 255.255.254.0" # 推送给客户端的路由设置(vlan40:192.168.40.0/23)
keepalive 10 120
tls-auth keys/ta.key 0 # This file is secret
comp-lzo
persist-key
persist-tun
status openvpn-status.log
log-append  openvpn.log
verb 5

    服务端添加iptables规则使服务器可以转发数据包(对网段10.8.0.0/24的数据包做SNAT):

shell> iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE
shell> service iptables save

    客户端配置文件跟上个案例一样,无须更改配置。

    客户端启动后,我们看下路由,到服务器端局域网的路由已经设置好了:

OpenVPN应用案例_open_03

应用、客户端通过服务器访问Internet

    为啥这么做大家都懂,省略1万字。

    服务端配置文件server.conf:

port 1194
proto tcp
dev tun
ca keys/ca.crt
cert keys/server.crt
key keys/server.key  # This file should be kept secret
dh keys/dh2048.pem
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp" # 改客户端的默认网关
push "dhcp-option DNS 114.114.114.114"   # 为客户端设置DNS服务器(对非win客户端无效)
push "dhcp-option DNS 8.8.8.8"
keepalive 10 120
tls-auth keys/ta.key 0 # This file is secret
comp-lzo
persist-key
persist-tun
status openvpn-status.log
log-append  openvpn.log
verb 5

    服务端添加iptables规则使服务器可以转发数据包(对网段10.8.0.0/24的数据包做SNAT):

shell> iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE
shell> service iptables save

    客户端配置文件无须更改。我们再看一下客户端的路由:

OpenVPN应用案例_vpn_04

   客户端路由增加了一条走×××的默认路由,出口IP也变成了服务器端的出口IP了。