基于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通。
本文來自于飛諾網
新聞熱點
疑難解答