银川市安全生产信息网:基于OpenVPN在linux上建立SSL VPN网络(连接异地局域网+远程单点接入)

来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 03:33:57
系统环境:
服务端:RHEL5 [ 2.6.18-8.el5xen ]
用户端:Windows XP SP2
软件环境:
openssl-0.9.8b-8.3.el5 (RHEL5自带)

http://www.oberhumer.com/opensource/lzo/download/lzo-2.03.tar.gz

http://openvpn.net/release/openvpn-2.0.9.tar.gz

http://openvpn.se/files/install_packages/openvpn-2.0.9-gui-1.0.3-install.exe

网络拓扑结构:



VPN服务器R1(济南):
eth0 192.168.1.1/24 (作为LAN1的默认网关地址)
eth1 64.3.2.1/24 (接入Internet)
VPN服务器R2(青岛):
eth0 192.168.2.1/24 (作为LAN2的默认网关地址)
eth1 75.6.7.8/24 (接入Internet)
VPN移动终端PC1:
使用Windows XP系统,接入Internet
####################################################################
一、配置VPN服务器R1(济南)
1、安装lzo-2.03、openvpn-2.0.9软件包
使用标准的tarball方式编译安装即可。
shell> tar zxvf lzo-2.03.tar.gz -C /usr/src
shell> cd /usr/src/lzo-2.03
shell> ./configure && make && make install
shell> tar zxvf openvpn-2.0.9.tar.gz -C /usr/src
shell> cd /usr/src/openvpn-2.0.9
shell> ./configure && make && make install
shell> cp sample-scripts/openvpn.init /etc/init.d/openvpn
shell> chkconfig --add openvpn
shell> service openvpn status

2、制作证书和相关密钥文件 (参考/usr/src/openvpn-2.0.9/easy-rsa/README文件)
1)调整及预定义变量
shell> mkdir /etc/openvpn/
shell> cd /usr/src/openvpn-2.0.9/easy-rsa
shell> vi vars
export D=`pwd`
export KER_CONFIG=$D/openssl.cnf
export KEY_DIR="/etc/openvpn/keys/" #//修改生成的密钥等文件的保存位置
export KEY_SIZE=1024
export KEY_COUNTRY=CN #//以下为用于各密钥中的预定义信息
export KEY_PROVINCE=ShanDong
export KEY_CITY=JiNan
export KEY_ORG="OpenVPN-TEST"
export KEY_EMAIL="TsengYia#126.com"
shell> . vars
shell> ./clean-all

2)创建证书、密钥等文件
shell> ./build-ca #//生成CA证书
shell> ./build-dh #//生成dh(Diffie-Hellman)文件
shell> ./build-key-server server_r1 #//生成R1服务器的密钥
shell> ./build-key client_r2 #//生成R2服务器的密钥
shell> ./build-key client_pc1 #//生成PC1用户端的密钥,以上各密钥的“Common Name”不要相同。
shell> openvpn --genkey --secret /etc/openvpn/keys/ta.key #//若不使用tls-auth,此项可不做

3、建立OpenVPN服务端配置文件
可以参考/usr/src/openvpn-2.0.9/sample-config-files/server.conf文件。

1)建立r1-r2隧道的server配置文件
shell> vi /etc/openvpn/r1_r2_server.conf
local 64.3.2.1 #//指定监听服务的公网接口地址
port 1194 #//指定监听的端口
proto udp
dev tun
ca keys/ca.crt
cert keys/server_r1.crt
key keys/server_r1.key
dh keys/dh1024.pem
tls-auth keys/ta.key 0
cipher BF-CBC #//若使用tls-auth,各客户端需与此保持一致
server 10.8.8.0 255.255.255.0 #//指定r1-r2 SSL VPN隧道的虚拟子网
ifconfig-pool-persist ipp.txt
push "dhcp-option DNS 218.56.57.58" #//为r2指定DNS服务器地址
push "dhcp-option DNS 202.106.0.20" #//为r2指定DNS服务器地址
push "route 192.168.1.0 255.255.255.0" #//为r2添加访问LAN1网段的路由记录
push "route 10.9.9.0 255.255.255.0" #//为r2添加访问r1-PC1隧道虚拟网络的路由记录
route 192.168.2.0 255.255.255.0 #//为r1添加访问LAN2网段的路由
client-config-dir ccd #//允许在ccd子目录中提供个别客户端的额外配置文件(以客户端的Common Name命名 )
keepalive 10 120
comp-lzo
max-clients 100 #//指定最大并发连接数
user nobody
group nobody
persist-key
persist-tun
status /tmp/openvpn-status-r1r2.log
log-append /tmp/openvpn-r1r2.log
verb 3

2)建立r1-pc1隧道的server配置文件
shell> vi /etc/openvpn/r1_pc1_server.conf
local 64.3.2.1
port 1195 #//指定一个区别于r1-r2隧道配置的端口
proto udp
dev tun
ca keys/ca.crt
cert keys/server_r1.crt
key keys/server_r1.key
dh keys/dh1024.pem
tls-auth keys/ta.key 0
cipher BF-CBC
server 10.9.9.0 255.255.255.0 #//指定r1-pc1 SSL VPN隧道的虚拟子网
ifconfig-pool-persist ipp.txt
push "dhcp-option DNS 218.56.57.58" #//为pc1指定DNS服务器地址
push "dhcp-option DNS 202.106.0.20" #//为pc1指定DNS服务器地址
push "route 192.168.1.0 255.255.255.0" #//为pc1添加访问LAN1网段的路由记录
push "route 192.168.2.0 255.255.255.0" #//为pc1添加访问LAN2网段的路由记录
push "route 10.8.8.0 255.255.255.0" #//为pc1添加访问r1-r2隧道虚拟网络的路由记录
route 192.168.2.0 255.255.255.0 #//为r1添加访问LAN2网段的路由
client-config-dir ccd
keepalive 10 120
comp-lzo
max-clients 100
user nobody
group nobody
persist-key
persist-tun
status /tmp/openvpn-status-r1pc1.log
log-append /tmp/openvpn-r1pc1.log
verb 3

3)建立VPN客户端r2、pc1的独立配置
shell> mkdir /etc/openvpn/ccd/
shell> vi /etc/openvpn/ccd/client_r2 #//使用密钥文件中R2的Common Name作为文件名
iroute 192.168.2.0 255.255.255.0 #//通告r2所连接的LAN2局域网段
ifconfig-push 10.8.8.2 10.8.8.1 #//设置r2的IP、P-t-P地址
shell> vi /etc/openvpn/ccd/client_pc1
ifconfig-push 10.9.9.2 10.9.9.1 #//设置pc1的IP、P-t-P地址

4、开启路由及SNAT、启动OpenVPN服务
shell> sysctl -w net.ipv4.ip_forward=1
shell> iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth1 -j SNAT --to-source 64.3.2.1
shell> service openvpn start
shell> netstat -anp | grep openvpn #//查看是否监听udp端口1194、1195
shell> ifconfig tun0 #//查看tun0的VPN接口信息( IP:10.8.8.1 P-P:10.8.0.2)
shell> ifconfig tun1 #//查看tun1的VPN接口信息( IP:10.8.1.1 P-P:10.8.1.2)


二、配置VPN服务器R2(青岛)
1、安装lzo-2.03、openvpn-2.0.9软件包
----> 同R1。

2、下载证书和相关密钥文件
从R1服务器上下载ca.crt、client_r2.key、client_r2.crt、ta.key文件,并保存至/etc/openvpn/keys/目录。注意做好备份。

3、建立VPN用户端配置文件
建立r1-r2隧道的client配置文件
shell> vi /etc/openvpn/r1_r2_client.conf
client #//使用客户端模式
dev tun
remote 64.3.2.1:1194 #//指定对端服务器的地址和端口(1194)
proto udp
resolv-retry infinite
nobind
user nobody
group nobody
persist-key
persist-tun
ca keys/ca.crt
cert keys/client_r2.crt
key keys/client_r2.key
ns-cert-type server
tls-auth keys/ta.key 1
cipher BF-CBC #//注意与r1的配置保持一致
comp-lzo
verb 3

4、开启路由及SNAT、启动OpenVPN服务
shell> sysctl -w net.ipv4.ip_forward=1
shell> iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o eth1 -j SNAT --to-source 75.6.7.8
shell> service openvpn start
shell> ifconfig tun0 #//查看tun0的VPN接口信息( IP:10.8.8.2 P-P:10.8.8.1)


三、配置VPN移动终端PC1
前提是能够接入Internet。
1、安装软件包
安装openvpn-2.0.9-gui-1.0.3-install.exe

2、下载证书和相关密钥文件
从R1服务器上下载ca.crt、client_pc1.crt、client_pc1.key、ta.key文件,并保存至C:\Program Files\OpenVPN\config\目录。注意做好备份。

3、建立用户端配置文件
用记事本建立C:\Program Files\OpenVPN\config\r1-pc1_client.ovpn文件,内容如:
client
dev tun
proto udp
remote 64.3.2.1 1195 #//指定远程服务器的地址和端口(1195)
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert client01.crt
key client01.key
ns-cert-type server
tls-auth keys/ta.key 1
cipher BF-CBC #//注意与r1的配置保持一致
comp-lzo
verb 3

4、连接VPN服务器
右击系统托盘区的OpenVPN GUI的图标,选择“Connect”即可。
—— 连接成功以后,可以在客户端的cmd中运行ipconfig /all查看VPN Client网络连接信息。

四、测试VPN连接
1、LAN1、LAN2两个局域网的主机能够通过SSL VPN隧道进行互相通讯(默认网关分别设为R1、R2的内网接口地址)。
2、PC1能够通过SSL VPN隧道访问LAN1、LAN2中的主机。

3、以上可以使用traceroute命令进行简单测试。
例如,在PC1中跟踪访问LAN2中主机的路由:-------->
C:\Documents and Settings\Administrator> tracert 192.168.2.163
Tracing route to Qingdao-Station163 [192.168.2.163]
over a maximum of 30 hops:
1
Trace complete.
C:\Documents and Settings\Administrator> 本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/40349/showart_1415254.html