基于OpenVPN连接两个远程局域网段

来源:互联网 发布:2维旋转矩阵 编辑:程序博客网 时间:2024/05/01 10:09

转载自:http://blog.chinaunix.net/uid-20164485-id-1973809.html


基于OpenVPN连接两个远程局域网段
 
2008-07-01 TsengYia#126.com
 
####################################################################
系统环境:
    服务端:RHEL5 [ 2.6.18-8.el5xen ]
 
软件环境:
    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
    http://www.oberhumer.com/opensource/lzo/download/lzo-2.03.tar.gz
 
参考文档:
    http://openvpn.net/index.php/documentation/howto.html
    http://www.linux.com/articles/58336
 
目标功能:
    搭建OpenVPN服务器,跨越Internet连接两个异地的局域网段。
 
              |---------------| <-->   Internet  <--> |---------------|
    LAN2 <--> |Router2(Client)|                       |Router1(Server)| --> LAN1
              |---------------| <--> SSL VPN Tun <--> |---------------|
 
    (北京)Server(router1)网络参数:
        eth0 173.16.16.1/24
        eth1 192.168.20.1/24(本例中作为LAN1的网关)
        LAN1:192.168.20.0/24
 
    (广州)Client(router2)网络参数:
        eth0 211.20.20.1/24
        eth2 192.168.40.1/24(本例中作为LAN2的网关)
        LAN2:192.168.40.0/24
####################################################################
一、安装OpenVPN软件包 (在router1、router2上均执行以下操作,按默认配置安装到/usr/local目录下)
 
    1、安装lzo (为SSL数据提供压缩)
shell> tar zxvf lzo-2.03.tar.gz -C /usr/src
shell> cd /usr/src/lzo-2.03
shell> ./configure && make && make install
 
    2、安装openvpn
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
 

二、配置OpenVPN Server端(router1)
 
    1、制作证书和相关密钥文件 (可参考/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=BJ
export KEY_CITY=BJ
export KEY_ORG="BJ-GZ"
export KEY_EMAIL="TsengYiashell>126.com"
shell> . vars
shell> ./clean-all
 
      2)创建证书、密钥等文件
shell> ./build-ca    #//生成CA证书
shell> ./build-dh    #//生成dh(Diffie-Hellman)文件
shell> ./build-key-server router1    #//生成服务端密钥
shell> ./build-key router2    #//生成客户端密钥
shell> /usr/local/sbin/openvpn --genkey --secret /etc/openvpn/keys/ta.key    #//生成tls-auth密钥
 
    2、建立OpenVPN服务配置文件
shell> cp /usr/src/openvpn-2.0.9/sample-config-files/server.conf /etc/openvpn/
shell> vi /etc/openvpn/server.conf
local 173.16.16.1    #//指定VPN服务监听的接口地址(本例中eth0网卡的地址)
port 1194    #//指定VPN服务监听的端口
proto udp
dev tun
ca keys/ca.crt
cert keys/router1.crt
key keys/router1.key
dh keys/dh1024.pem
server 10.8.8.0 255.255.255.0    #//指定vpn隧道的虚拟子网,vpn server将自动使用第一个IP,如10.8.8.1
ifconfig-pool-persist ipp.txt
push "route 192.168.20.0 255.255.255.0"    #//向客户端通告服务器端LAN1网段
client-config-dir ccd    #//指定调用ccd子目录下的客户端配置文件,可在文件中指定对端的ip地址
route 192.168.40.0 255.255.255.0    #//为server端添加到client端LAN2网段的路由
client-to-client    #//允许各客户端之间的互相访问
duplicate-cn    #//允许client密钥被复用
keepalive 10 120
tls-auth keys/ta.key 0    #//指定tls认证密钥
cipher BF-CBC    #//指定cipher加密算法
comp-lzo
max-clients 100    #//指定最大并发连接数
user nobody
group nobody
persist-key
persist-tun
status /tmp/openvpn-status.log
verb 3
mute 20
 
shell> mkdir /etc/openvpn/ccd
shell> vi /etc/openvpn/ccd/router2    #//在client的独立配置文件中指定对端tun0的ip地址参数
iroute 192.168.40.0 255.255.255.0
ifconfig-push 10.8.8.2 10.8.8.1    #//依次为tun0本地地址,P-t-P对端地址
 
    3、准备启动脚本、启动OpenVPN
shell> cp -p /usr/src/openvpn-2.0.9/sample-scripts/openvpn.init /etc/init.d/
shell> vi /etc/init.d/openvpn
shell> chkconfig --add openvpn
shell> chkconfig --level 35 openvpn on
shell> service openvpn start
 
三、配置OpenVPN Client端(router2)
 
    1、下载证书和相关密钥文件
      1)下载在服务器生成的ca.crt、router2.crt、router2.key、ta.key文件,做好备份
      2)复制上述文件到router2的/etc/openvpn/keys目录
 
    2、修改Client配置文件
shell> cp /usr/src/openvpn-2.0.9/sample-config-files/client.conf /etc/openvpn/
shell> vi /etc/openvpn/client.conf
client
dev tun
proto udp
remote 173.16.16.1 1194
resolv-retry infinite
nobind
user nobody
group nobody
persist-key
persist-tun
ca keys/ca.crt
cert keys/router2.crt
key keys/router2.key
ns-cert-type server
tls-auth keys/ta.key 1
cipher BF-CBC
comp-lzo
verb 3
mute 20
 
 
四、准备启动脚本、启动OpenVPN (在router1、router2上均执行以下操作)
 
shell> cp -p /usr/src/openvpn-2.0.9/sample-scripts/openvpn.init /etc/init.d/
shell> vi /etc/init.d/openvpn
shell> chkconfig --add openvpn
shell> chkconfig --level 35 openvpn on
shell> service openvpn start

五、连通测试
    1、可以分别在router1、router2上查看tun0设备参数(ifconfig tun0)
router1的tun0信息:
    inet addr:10.8.8.1    P-t-P:10.8.8.2
router2的tun0信息:
    inet addr:10.8.8.2    P-t-P:10.8.8.1
 
    2、可以分别在router1、router2上查看路由记录(route -n)
router1的路由表信息中应有到LAN2网段的路由记录:
    Destination    Gateway    Genmask        Flags  Metric  Ref  Use  Iface
    192.168.40.0   10.8.8.2   255.255.255.0  UG     0        0   0    tun0
router2的路由表信息中应有到LAN1网段的路由记录:
    Destination    Gateway    Genmask        Flags  Metric  Ref  Use  Iface
    192.168.20.0   10.8.8.1   255.255.255.0  UG     0        0   0    tun0
 
    3、LAN1、LAN2两个网段的客户端互联测试,例如:
北京的192.168.20.20和广州的192.168.40.40能够相互ping通。

原创粉丝点击