centos 6.5下搭建ipsec/xl2tpd VPN
本文安裝都是直接yum安裝,省事,省心。
一、安裝(一條命令解決了)
yum install openswan ppp xl2tpd
喜歡源碼安裝的朋友可去去http://pkgs.org 去下載相就的源碼包。
二、配置
1.編輯/etc/ipsec.conf
vim /etc/ipsec.conf
把下面xx.xxx.xxx.xxx換成你自己VPS實際的外網固定IP。其他的不動。
config setup
nat_traversal=yes
virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12
oe=off
protostack=netkey
conn L2TP-PSK-NAT
rightsubnet=vhost:%priv
also=L2TP-PSK-noNAT
conn L2TP-PSK-noNAT
authby=secret
pfs=no
auto=add
keyingtries=3
rekey=no
ikelifetime=8h
keylife=1h
type=transport
left=xx.xxx.xxx.xxx
leftprotoport=17/1701
right=%any
rightprotoport=17/%any
2.編輯/etc/ipsec.secrets
vim /etc/ipsec.secrets
192.168.1.100 %any: PSK "YourPsk"
192.168.1.100換成你自己VPS實際的公網IP, YourPsk你自己定一個密鑰,到時候連VPN的時候在安全設定裡要用。
3.修改/添加 /etc/sysctl.conf
vim /etc/sysctl.conf
確保下面的欄位都有,對應的值或下面一樣。省事的話直接在/etc/sysctl.conf的末尾直接把下面內容的粘過去。
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.all.log_martians = 0
net.ipv4.conf.default.log_martians = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.icmp_ignore_bogus_error_responses = 1
讓修改後的sysctl.conf生效:
sysctl -p
有可以報一些關於ipv6的error,不要管它,繼續下一步。
4.驗證ipsec的運行狀態
service ipsec start
ipsec verify
執行ipsec verify後會有如下輸出:
Checking your system to see if IPsec got installed and started correctly:
Version check and ipsec on-path [OK]
Linux Openswan U2.6.32/K2.6.32-431.el6.x86_64 (netkey)
Checking for IPsec support in kernel [OK]
SAref kernel support [N/A]
NETKEY: Testing for disabled ICMP send_redirects [OK]
NETKEY detected, testing for disabled ICMP accept_redirects [OK]
Checking that pluto is running [OK]
Pluto listening for IKE on udp 500 [OK]
Pluto listening for NAT-T on udp 4500 [OK]
Checking for 'ip' command [OK]
Checking /bin/sh is not /bin/dash [OK]
Checking for 'iptables' command [OK]
Opportunistic Encryption Support [DISABLED]
如果有failed,就無法進行下去,那需要自己去百度、google找答案吧,我搭建的時候是沒有遇到,記得把selinux關掉。
5.編輯/etc/xl2tpd/xl2tpd.conf
vim /etc/xl2tpd/xl2tpd.conf
內容如下:
[global]
ipsec saref = yes
listen-addr = 120.26.115.39 ;伺服器位址
[lns default]
ip range = 192.168.1.2-192.168.1.100;這裡是VPN client的內網ip位址範圍
local ip = 192.168.1.1 ;這裡是VPN server的內網地址
refuse chap = yes
refuse pap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = y
6.編輯/etc/ppp/options.xl2tpd
vim /etc/ppp/options.xl2tpd
內容如下:
require-mschap-v2
ms-dns 8.8.8.8
ms-dns 8.8.4.4
asyncmap 0
auth
crtscts
lock
hide-password
modem
debug
name l2tpd
proxyarp
lcp-echo-interval 30
lcp-echo-failure 4
7.編輯 /etc/ppp/chap-secrets
此設定檔是設定VPN的使用者名稱,密碼:
vim /etc/ppp/chap-secrets
格式如下:
# Secrets for authentication using CHAP
# client server secret IP addresses
admin * admin *
此設定表示,vpn的賬戶和密碼都為admin,可以在任何可以上網的客機上登陸此vpn賬戶。
8.啟動 xl2tpd 服務
service xl2tpd start
9.開放連接埠及轉寄
1.iptables -t nat -A POSTROUTING -m policy --dir out --pol none -j MASQUERADE
2.iptables -A FORWARD -i ppp+ -p all -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
3.iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
4.iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth1 -j MASQUERADE
儲存規則:
service iptables save
至此 IPsec/xl2tpd VPN 已全部配置完成,下面就可以測試了。
10.在win7上測試連接vpn
開啟網際網路共用中心--設定新的串連或網路--串連到工作區--建立vpn
點擊右下角網狀圖標--vpn串連--點擊屬性
1.常規中的ip為vps的公網ip
2.vpn類型:使用Ipsec 的第二層通道通訊協定(L2TP/IPsec)
資料加密:需要加(如果伺服器拒絕將中斷連線)
進階設定:使用預共用的密鑰作身分識別驗證
密鑰:YourPsk (前面設定過的)
點擊確定後輸入使用者名稱和密碼即可登陸。