You are here: Home > 技術…… > CentOS6.X下的L2TP/IPSEC VPN的架設

CentOS6.X下的L2TP/IPSEC VPN的架設

前兩天才把VPN給搞起來,結果一升級64位内核之後,要把所有的軟件都更新到64位的,把源換好,一陣子亂yum update之後,VPN就不出所料挂了……

於是,嘗試這種配,各種亂搞,從openswan.org下各種版本來編譯,都無法恢復了……

於是一怒之下開了個新的系統,乾乾淨淨地搞。

首先當然是安裝openswan了

1
yum install openswan

配置/etc/ipsec.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
version 2.0
config setup
  protostack=netkey
  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

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=公網IP
  leftprotoport=17/%any #這裡,網上大多都說是17/1701,不過不知道哪個版本后的xl2tpd之後,光1701已經不夠了,有空看看源代碼,還需要什麽端口。。。
  right=%any
  rightprotoport=17/%any
  #下面三行專門給苹果系列產品的優化
  dpddelay=40
  dpdtimeout=130
  dpdaction=clear

/etc/ipsec.secrets裏面的内容不用改了,因爲裏面是包含/etc/ipsec.d/下面的所有secrets文件,所以我在/etc/ipsec.d/下面建了一個vpn.secrets

1
YOUR.SERVER.IP.ADDRESS %any: PSK "YourSharedSecret"

之後就是設置ipv4轉發,運行以下命令:

1
2
3
4
5
for each in /proc/sys/net/ipv4/conf/*
do
  echo 0 > $each/accept_redirects
  echo 0 > $each/send_redirects
done

修改/etc/sysctl.conf中的net.ipv4.ip_forward,如果沒有就新增吧

1
net.ipv4.ip_forward=1

运行如下命令刷新:

1
sysctl -p

然後就可以把ipsec啓起來了:

1
service ipsec start

然後檢查一下ipsec的狀態

1
ipsec verify

我這邊的結果是這樣的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Checking your system to see if IPsec got installed and started correctly:
Version check and ipsec on-path [OK]
Linux Openswan U2.6.32/K3.14.5-x86_64-linode42 (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]
Two or more interfaces found, checking IP forwarding [OK]
Checking NAT and MASQUERADEing [OK]
Checking for 'ip' command [OK]
Checking /bin/sh is not /bin/dash [OK]
Checking for 'iptables' command [OK]
Opportunistic Encryption Support [DISABLED]

然後安裝xl2tpd……

1
yum install xl2tpd

咦?找不到這個包…………好吧……加個源……

1
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

如果是32位,就把上面的x86_64換成i386好了。如果上面的地址不對了,就看fedora epel的faq好了……在這裡:https://fedoraproject.org/wiki/EPEL/FAQ/zh-cn#How_can_I_install_the_packages_from_the_EPEL_software_repository.3F

再來!

1
yum install xl2tpd
1
yum install ppp

編輯/etc/xl2tpd/xl2tpd.conf如下:

1
2
3
4
5
6
7
8
9
10
11
12
[global]
ipsec saref = no #因爲我上面的ipsec verify的SAref kernel support是N/A,所以這裡就是no了……

[lns default]
ip range = 10.1.2.2-10.1.2.255
local ip = 10.1.2.1
length bit = yes
refuse chap = yes
refuse pap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.l2tpd

編輯/etc/ppp/options.xl2tpd,如果沒有就建立一個吧。你也可以放在你喜歡的地方起一個你喜歡的名字,只要和上面那配置文件裏面的pppoptfile一致就好

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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

上面的ms-dns我用的是google的DNS……
然後編輯/etc/ppp/chap-secrets,這個用來加VPN賬戶,一行一個賬戶:

1
2
# user server password ip
test l2tpd testpassword *

test是用戶名,自己起,第二項必須是l2tpd,第三項是密碼,第4項是限制這個VPN必須從哪裏連上來,*代表那裏都可以
重啓服務:

1
service xl2tpd restart

這樣子應該就能連上來的,只是因爲沒有設置IP轉發,連上來之後不能訪問外網……不過我發現我win7連上來沒有問題,但是我的苹果手機就一直不行,看xl2tpd的日誌,發現根本就沒有把包發給xl2tpd,看/var/log/secure,發現一直這樣子循環:
Jun 25 07:29:52 li600-141 pluto[2685]: “L2TP-PSK-NAT”[1] x.x.x.x #1: message ignored because it contains an unknown or unexpected payload type (ISAKMP_NEXT_SAK) at the outermost level
Jun 25 07:29:52 li600-141 pluto[2685]: “L2TP-PSK-NAT”[1] x.x.x.x #1: sending notification INVALID_PAYLOAD_TYPE to x.x.x.x:3936
Jun 25 07:29:55 li600-141 pluto[2685]: “L2TP-PSK-NAT”[1] x.x.x.x #1: message ignored because it contains an unknown or unexpected payload type (ISAKMP_NEXT_SAK) at the outermost level
Jun 25 07:29:55 li600-141 pluto[2685]: “L2TP-PSK-NAT”[1] x.x.x.x #1: sending notification INVALID_PAYLOAD_TYPE to x.x.x.x:3936
Jun 25 07:29:59 li600-141 pluto[2685]: “L2TP-PSK-NAT”[1] x.x.x.x #1: message ignored because it contains an unknown or unexpected payload type (ISAKMP_NEXT_SAK) at the outermost level
Jun 25 07:29:59 li600-141 pluto[2685]: “L2TP-PSK-NAT”[1] x.x.x.x #1: sending notification INVALID_PAYLOAD_TYPE to x.x.x.x:3936

搜了半天,也沒有有用的信息……依稀感覺是版本的問題……

好吧,那換一個版本。从http://mirror.centos.org/centos/6/updates/x86_64/Packages/ 來看,openswan有兩個版本,我用的是高版本,不行的話就換一個咯。

先卸載舊版本:

1
rpm -e openswan

然後從http://mirror.centos.org/centos/6/updates/x86_64/Packages/openswan-2.6.32-27.2.el6_5.x86_64.rpm安裝另外一個版本。

記得恢復一下ipsec.conf。

然後。。我的苹果。。就連上去了。。。

記得設上轉發哦:
iptables -t nat -A POSTROUTING -s 10.1.2.0/255.255.255.0 -o eth0 -j MASQUERADE

1
iptables -t nat -A POSTROUTING -s 10.1.2.0/24 -j MASQUERADE

爲了讓每次服務器重起VPN服務都能順利運行,所以有必要將一些重啓后就丟失的配置寫進啓動自動運行腳本/etc/rc.local裏面(在exit之前):

1
2
3
4
5
6
for each in /proc/sys/net/ipv4/conf/*
do
  echo 0 > $each/accept_redirects
  echo 0 > $each/send_redirects
done
/etc/init.d/ipsec restart

保存iptables的設置不用寫在rc裏面,設置好之後,service iptables save,就能把配置保存下來啦。

當然了,別忘記chkcfg on,把ipsec和xl2tpd都加上去吧……

Tags: ,

2 Responses to “CentOS6.X下的L2TP/IPSEC VPN的架設”

  1. pizza说道:

    就为了 VPN?你这是给自己找事吗,哈哈

    • Netson说道:

      也不全是为了VPN,还有这个博客,还有ssh……还有一些别的东西,顺便也玩玩有公网IP的linux~

回复 pizza 取消回复

您的电子邮箱地址不会被公开。 必填项已用*标注