概念性的东西这里不再赘述,网上有太多,一键安装脚本也有很多,但是很多不能用,能用的也仅仅只是在CentOS6下使用,CentOS7基本没看到这些安装脚本。于是花了一些时间来折腾测试,写这个脚本方便以后VPN的一键安装搭建。其中用的开源软件包是openswan和xl2tpd,,中间碰到过很多很多问题,如openswan和xl2tpd之间的兼容性问题。
请容许我这样做,脚本依赖《OneinStack》或《lnmp一键安装包》,可以在安装完lnmp后,执行这个脚本(vpn_centos.sh)来一键搭建你的个人vpn
CentOS 6、7下IPSEC/L2TP VPN一键安装脚本,如下:
#!/bin/bash
# Author: yeho
# Blog: //linuxeye.com
#
# This script's project home is:
# //linuxeye.com/31.html
# https://github.com/lj2007331/lnmp
# Check if user is root
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
clear
printf "
#######################################################################
# LNMP/LAMP/LANMP for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
# For more information please visit //linuxeye.com/31.html #
#######################################################################
"
[ ! -e "src" ] && mkdir src
cd src
. ../include/download.sh
while :
do
echo
read -p "Please input private IP-Range(Default Range: 10.0.2): " iprange
[ -z "$iprange" ] && iprange="10.0.2"
if [ -z "`echo $iprange | grep -E "^10.|^192.168.|^172." | grep -o '^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$'`" ];then
echo -e "33[31minput error! Input format: xxx.xxx.xxx33[0m"
else
break
fi
done
echo
read -p "Please input PSK(Default PSK: psk): " MYPSK
[ -z "$MYPSK" ] && MYPSK="psk"
while :
do
echo
read -p "Please input username: " Username
[ -n "$Username" ] && break
done
while :
do
echo
read -p "Please input password: " Password
[ -n "$Password" ] && break
done
clear
public_IP=`../functions/get_public_ip.py`
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:$public_IP"
echo ""
echo "Server Local IP:$iprange.1"
echo ""
echo "Client Remote IP Range:$iprange.2-$iprange.254"
echo ""
echo "PSK:$MYPSK"
echo ""
echo "Press any key to start..."
char=`get_char`
clear
if [ -n "`grep 'CentOS Linux release 7' /etc/redhat-release`" ];then
CentOS_REL=7
for Package in wget ppp iptables iptables-services make gcc gmp-devel xmlto bison flex xmlto libpcap-devel lsof vim-enhanced
do
yum -y install $Package
done
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
elif [ -n "`grep 'CentOS release 6' /etc/redhat-release`" ];then
CentOS_REL=6
for Package in wget ppp iptables make gcc gmp-devel xmlto bison flex xmlto libpcap-devel lsof vim-enhanced
do
yum -y install $Package
done
sed -i 's@net.ipv4.ip_forward.*@net.ipv4.ip_forward = 1@g' /etc/sysctl.conf
else
echo -e "33[31mDoes not support this OS, Please contact the author! 33[0m"
exit 1
fi
sysctl -p
mknod /dev/random c 1 9
src_url=https://download.openswan.org/openswan/old/openswan-2.6/openswan-2.6.38.tar.gz && Download_src
tar xzf openswan-2.6.38.tar.gz
cd openswan-2.6.38
make programs install
cd ..
cat >/etc/ipsec.conf< config setupnat_traversal=yesvirtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12oe=offprotostack=netkeyplutostderrlog=/var/log/ipsec.logconn L2TP-PSK-NATrightsubnet=vhost:%privalso=L2TP-PSK-noNATconn L2TP-PSK-noNATauthby=secrettype=tunnelpfs=noauto=addkeyingtries=3rekey=noikelifetime=8hkeylife=1hleft=$public_IPleftprotoport=17/1701right=%anyrightprotoport=17/%anyrightsubnetwithin=0.0.0.0/0dpddelay=30dpdtimeout=120dpdaction=clearEOFcat >/etc/ipsec.secrets< $public_IP %any: PSK "$MYPSK"EOFcat > /usr/bin/zl2tpset << EOF#!/bin/bashfor each in /proc/sys/net/ipv4/conf/*doecho 0 > $each/accept_redirectsecho 0 > $each/send_redirectsdoneEOFchmod +x /usr/bin/zl2tpset/usr/bin/zl2tpset[ -z "`grep zl2tpset /etc/rc.local`" ] && echo '/usr/bin/zl2tpset' >> /etc/rc.localservice ipsec restartsrc_url=http://pkgs.fedoraproject.org/repo/pkgs/xl2tpd/xl2tpd-1.3.6.tar.gz/2f526cc0c36cf6d8a74f1fb2e08c18ec/xl2tpd-1.3.6.tar.gz && Download_srctar xzf xl2tpd-1.3.6.tar.gzcd xl2tpd-1.3.6make install[ ! -e "/var/run/xl2tpd" ] && mkdir /var/run/xl2tpd[ ! -e "/etc/xl2tpd" ] && mkdir /etc/xl2tpdcd ..cat >/etc/xl2tpd/xl2tpd.conf< [global]listen-addr = $public_IPipsec saref = yes[lns default]ip range = $iprange.2-$iprange.254local ip = $iprange.1refuse chap = yesrefuse pap = yesrequire authentication = yesppp debug = yespppoptfile = /etc/ppp/options.xl2tpdlength bit = yesEOFcat >/etc/ppp/options.xl2tpd< require-mschap-v2ms-dns 8.8.8.8ms-dns 8.8.4.4noccpasyncmap 0authcrtsctslockhide-passwordmodemdebugname l2tpdproxyarplcp-echo-interval 30lcp-echo-failure 4idle 1800mtu 1410mru 1410nodefaultrouteconnect-delay 5000logfd 2logfile /var/log/l2tpd.logEOFcat >>/etc/ppp/chap-secrets< $Username l2tpd $Password *EOFNETWORK_INT=`route | grep default | awk '{print $NF}'`iptables -t nat -A POSTROUTING -s ${iprange}.0/24 -o $NETWORK_INT -j MASQUERADEiptables -I FORWARD -s ${iprange}.0/24 -j ACCEPTiptables -I FORWARD -d ${iprange}.0/24 -j ACCEPTiptables -I INPUT -p udp --dport 1701 -j ACCEPTiptables -I INPUT -p udp --dport 500 -j ACCEPTiptables -I INPUT -p udp --dport 4500 -j ACCEPTservice iptables saveservice ipsec restartxl2tpdchkconfig ipsec onclearipsec verifyprintf "Serverip:$public_IPPSK:$MYPSKusername:$Usernamepassword:$Password"
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)