1. 程式人生 > >內網伺服器搭建l2tp/ipsec VPN伺服器實現遠端訪問

內網伺服器搭建l2tp/ipsec VPN伺服器實現遠端訪問

這裡先對vpn的實現法法做一個介紹:

(一)安裝VPN:

1.vpn組成及作用

openswan:提供ipsec服務,設定預共享金鑰 
ppp:撥號軟體,提供使用者名稱和密碼用於撥號 

xl2tpd:提供l2tp服務

2.安裝前準備(ppp及其他依賴包)

yum install -y ppp iptables make gcc gmp-devel xmlto bison flex libpcap-devel lsof vim-enhanced

3.下載安裝openswan-2.6.38.tar.gz

#tar -xf  openswan-2.6.38.tar.gz

#cd openswan-2.6.38

#make programs install

修改ipsec相關配置,跟下面一模一樣,left部分改成伺服器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=vpn伺服器ip
	leftid=vpn伺服器ip
    leftprotoport=17/1701
    right=%any
	rightid=%any
    rightprotoport=17/%any

L2TP比PPTP多了一個金鑰項,這也是它比PPTP更安全的原因之一。這個金鑰其實就是一個密碼,不同於使用者的登入密碼,它相當於一個裝置之間通訊的金鑰。它的配置檔案是/etc/ipsec.secrets

vim /etc/ipsec.secrets
vpn伺服器ip  %any:  PSK "祕鑰"

啟動服務:/etc/init.d/ipsec start

4、設定sysctl

vim /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 -p

5、修改核心引數

vim /usr/bin/l2tpset

#/bin/bash
for each in /proc/sys/net/ipv4/conf/*
do
echo 0 > $each/accept_redirects
echo 0 > $each/send_redirects
done
~     

/usr/bin/l2tpset

6、安裝:rp-l2tp-0.4.tar.gz

#tar -xf rp-l2tp-0.4.tar.gz

#cd rp-l2tp-0.4
#./configure
#make
#cp handlers/l2tp-control /usr/local/sbin/
#mkdir -p /var/run/xl2tpd/

#ln -s /usr/local/sbin/l2tp-control /var/run/xl2tpd/l2tp-control

7、安裝xl2tpd-1.2.4.tar.gz

#cd xl2tpd-1.2.4
#make install

#mkdir -p /etc/xl2tpd

修改配置

vim /etc/xl2tpd/xl2tpd.conf 

[global]
ipsec saref = yes
[lns default]
ip range = $iprange.2-$iprange.254
local ip = $iprange.1
refuse chap = yes
refuse pap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes

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

執行程式:/usr/local/sbin/xl2tpd

8、修改增加賬號資訊

vim /etc/ppp/chap-secrets

9.其他配置

iptables --table nat --append POSTROUTING --jump MASQUERADE

10、設定開機啟動

iptables --table nat --append POSTROUTING --jump MASQUERADE
/etc/init.d/ipsec restart
/usr/bin/l2tpset
/usr/local/sbin/xl2tpd

(二)在出網裝置上做ip和埠對映,將vpn資料包對映到vpn伺服器。下滿的規則僅供參考,安具體情況來配置。

iptables -A INPUT -i eth0 -p udp -m multiport –dports 500,4500,1701 -m comment –comment “ipsec” -j ACCEPT 

iptables -A FORWARD -i ppp+ -p tcp -m tcp –tcp-flags FIN,SYN,RST,ACK SYN -j TCPMSS –set-mss 1356

到此就配置完了,可以用手機開無線,給電腦連結,然後播VPN進行測試。

方法二:此方法適合新手不想研究太透,或者老手已經不需要了解安裝過程使用。

在伺服器root目錄編寫一鍵安裝指令碼,程式碼如下 :

vim l2tp.sh

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#===============================================================================================
#   SYSTEM REQUIRED:  CentOS-5 (32bit/64bit) or CentOS-6 (32bit/64bit)
#   DESCRIPTION:  install L2TP
#   VERSION:   1.0
#   AUTHOR:    teddysun
#===============================================================================================
# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
   echo "Error: This script must be run as root" 1>&2
   exit 1
fi

tmpip=`ifconfig |grep 'inet' | grep -Evi '(inet6|127.0.0.1)' | awk '{print $2}' | cut -d: -f2 | tail -1`

echo "Please input IP-Range:"
read -p "(Default Range: 10.1.2):" iprange
if [ "$iprange" = "" ]; then
	iprange="10.1.2"
fi

echo "Please input PSK:"
read -p "(Default PSK: vpn):" mypsk
if [ "$mypsk" = "" ]; then
	mypsk="vpn"
fi

clear
get_char()
{
SAVEDSTTY=`stty -g`
stty -echo
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty $SAVEDSTTY
}
echo ""
echo "ServerIP:"
echo "$tmpip"
echo ""
echo "Server Local IP:"
echo "$iprange.1"
echo ""
echo "Client Remote IP Range:"
echo "$iprange.2-$iprange.254"
echo ""
echo "PSK:"
echo "$mypsk"
echo ""
echo "Press any key to start...or Press Ctrl+c to cancel"
char=`get_char`
clear

mknod /dev/random c 1 9
#install some necessary tools
yum install -y ppp iptables make gcc gmp-devel xmlto bison flex libpcap-devel lsof vim-enhanced
#
cur_dir=`pwd`
mkdir -p $cur_dir/l2tp
cd $cur_dir/l2tp
#download openswan-2.6.38.tar.gz
if [ -s openswan-2.6.38.tar.gz ]; then
  echo "openswan-2.6.38.tar.gz [found]"
else
  echo "openswan-2.6.38.tar.gz not found!!!download now......"
  if ! wget https://download.openswan.org/openswan/old/openswan-2.6/openswan-2.6.38.tar.gz;then
    echo "Failed to download openswan-2.6.38.tar.gz, please download it to $cur_dir directory manually and rerun the install script."
	exit 1
  fi
fi
#download rp-l2tp-0.4.tar.gz
if [ -s rp-l2tp-0.4.tar.gz ]; then
  echo "rp-l2tp-0.4.tar.gz [found]"
else
  echo "rp-l2tp-0.4.tar.gz not found!!!download now......"
  if ! wget https://jaist.dl.sourceforge.net/project/rp-l2tp/rp-l2tp/0.4/rp-l2tp-0.4.tar.gz;then
    echo "Failed to download rp-l2tp-0.4.tar.gz, please download it to $cur_dir directory manually and rerun the install script."
	exit 1
  fi
fi
#download xl2tpd-1.2.4.tar.gz
if [ -s xl2tpd-1.2.4.tar.gz ]; then
  echo "xl2tpd-1.2.4.tar.gz [found]"
else
  echo "xl2tpd-1.2.4.tar.gz not found!!!download now......"
  if ! wget https://download.openswan.org/xl2tpd/old/xl2tpd-1.2.4.tar.gz;then
    echo "Failed to download xl2tpd-1.2.4.tar.gz, please download it to $cur_dir directory manually and rerun the install script."
	exit 1
  fi
fi
#untar all files
rm -rf $cur_dir/l2tp/untar
mkdir -p $cur_dir/l2tp/untar
echo "============================untar all files,please wait a moment...======================="
for file in `ls *.tar.gz` ;
do
tar -zxf $file -C $cur_dir/l2tp/untar
done
echo "============================untar all files completed!...================================="
#make & install openswan-2.6.38
cd $cur_dir/l2tp/untar/openswan-2.6.38
make programs install

#configuation ipsec
rm -rf /etc/ipsec.conf
touch /etc/ipsec.conf
cat >>/etc/ipsec.conf<<EOF
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=$tmpip
	leftid=$tmpip
    leftprotoport=17/1701
    right=%any
	rightid=%any
    rightprotoport=17/%any
EOF
cat >>/etc/ipsec.secrets<<EOF
$tmpip %any: PSK "$mypsk"
EOF
sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf
sed -i 's/net.ipv4.conf.default.rp_filter = 1/net.ipv4.conf.default.rp_filter = 0/g' /etc/sysctl.conf
sysctl -p
iptables --table nat --append POSTROUTING --jump MASQUERADE
for each in /proc/sys/net/ipv4/conf/*
do
echo 0 > $each/accept_redirects
echo 0 > $each/send_redirects
done

#make & install rp-l2tp-0.4
cd $cur_dir/l2tp/untar/rp-l2tp-0.4
./configure
make
cp handlers/l2tp-control /usr/local/sbin/
mkdir -p /var/run/xl2tpd/
ln -s /usr/local/sbin/l2tp-control /var/run/xl2tpd/l2tp-control
#make & install xl2tpd-1.2.4.tar.gz
cd $cur_dir/l2tp/untar/xl2tpd-1.2.4
make install
mkdir -p /etc/xl2tpd
rm -rf /etc/xl2tpd/xl2tpd.conf
touch /etc/xl2tpd/xl2tpd.conf
cat >>/etc/xl2tpd/xl2tpd.conf<<EOF
[global]
ipsec saref = yes
[lns default]
ip range = $iprange.2-$iprange.254
local ip = $iprange.1
refuse chap = yes
refuse pap = yes
require authentication = yes
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
EOF
rm -rf /etc/ppp/options.xl2tpd
touch /etc/ppp/options.xl2tpd
cat >>/etc/ppp/options.xl2tpd<<EOF
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
EOF

#default user & password set
pass=`openssl rand 6 -base64`
if [ "$1" != "" ]
then pass=$1
fi
echo "vpn l2tpd ${pass} *" >> /etc/ppp/chap-secrets

touch /usr/bin/l2tpset
echo "#/bin/bash" >>/usr/bin/l2tpset
echo "for each in /proc/sys/net/ipv4/conf/*" >>/usr/bin/l2tpset
echo "do" >>/usr/bin/l2tpset
echo "echo 0 > \$each/accept_redirects" >>/usr/bin/l2tpset
echo "echo 0 > \$each/send_redirects" >>/usr/bin/l2tpset
echo "done" >>/usr/bin/l2tpset
chmod +x /usr/bin/l2tpset
iptables --table nat --append POSTROUTING --jump MASQUERADE
l2tpset
xl2tpd
cat >>/etc/rc.local<<EOF
iptables --table nat --append POSTROUTING --jump MASQUERADE
/etc/init.d/ipsec restart
/usr/bin/l2tpset
/usr/local/sbin/xl2tpd
EOF
clear
ipsec verify
printf "
####################################################
#                                                  #
# Install L2TP bash script                         #
# Version: 1.0                                     #
# Author: teddysun <http://teddysun.com>           #
# enjoy it!!                                       #
####################################################
if there are no [FAILED] above, then you can
connect to your L2TP VPN Server with the default
user/password below:

ServerIP:$tmpip
username:vpn
password:${pass}
PSK:$mypsk

執行指令碼:./l2tp.sh

如果提示無法下載上面幾個檔案,說明下載連結失效了,可以網上搜素有效的下載連結,在腳本里面替換

配置對映。