[实用技巧] 基于OpenVPN连接两个远程局域网段

[复制链接]
小忆1975 2014-3-23 11:11:31
基于OpenVPN连接两个远程局域网段



        系统环境:

服务端: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通。





        本文来自于飞诺网
单于力 2014-3-23 14:21:39
撸过
回复

使用道具 举报

晏洛灵 2014-3-27 16:30:52
LZ敢整点更有创意的不?兄弟们等着围观捏~
回复

使用道具 举报

邢萦怀 2014-4-11 10:20:15
边撸边过
回复

使用道具 举报

马海恬 2014-4-20 08:39:52
学习下
回复

使用道具 举报

禹云亭 2014-4-27 15:00:03
路过
回复

使用道具 举报

慕容梦云 2014-5-17 18:26:14
传说中的沙发???哇卡卡
回复

使用道具 举报

蔡海溪 2014-6-20 08:31:56
报告!别开枪,我就是路过来看看的。。。
回复

使用道具 举报

谭濡霈 2014-6-20 20:36:06
楼下的接上
回复

使用道具 举报

亢昭懿 2014-6-21 12:40:48
顶起顶起顶起
回复

使用道具 举报

柏嘉言 2014-6-25 09:40:55
看起来不错
回复

使用道具 举报

隗春娇 2024-8-16 15:18:55
前排,哇咔咔
回复

使用道具 举报

姜震 2024-8-16 16:15:28
专业抢沙发的!哈哈
回复

使用道具 举报

郁宜然 2025-1-12 07:21:37 来自手机
看起来不错
回复

使用道具 举报

全雪戈 2025-3-7 14:49:09
传说中的沙发???哇卡卡
回复

使用道具 举报

袁淳雅 2025-3-13 20:47:24
支持支持再支持
回复

使用道具 举报

巴妍 2025-4-22 10:10:11
我也顶起出售广告位
回复

使用道具 举报

乐悦可 2025-5-10 16:06:02
专业抢沙发的!哈哈
回复

使用道具 举报

手机版

GMT+8, 2025-6-6 16:24

Copyright © 2012 技术派 | 技术支持:技术派设计

Powered by Discuz! X3.4