检测是否支持
执行如下命令
cat /dev/net/tun
返回信息应该为
cat: /dev/net/tun: File descriptor in bad state
再执行如下命令
cat /dev/ppp
返回信息应该为
cat: /dev/ppp: No such device or address
如果返回信息不同 那么请联系你的主机商开通TUN和PPP
软件
1. openswan:提供IPSec加密
2. lsof:用于数据访问
3. ppp:提供用户名、密码 认证
4. xl2tp:提供L2TP VPN服务
安装相关软件
openswan
首先之前openswan 如果装过 建议先删除 删除方法如下
yum remove openswan
依赖包
然后安装一些需要的依赖包
yum install -y iptables make ppp gcc gmp-devel xmlto bison flex xmlto libpcap-devel lsof
其次yum安装openwan
yum install openswan
编辑ipsec.conf
然后编辑 /etc/ipsec.conf 这文件对格式要求很严格 缩进问题都会导致出错 所以这个下面的这个代码仅供参考 如果直接复制出现问题 请编辑原来的配置文件 修改成这样子
# /etc/ipsec.conf - Libreswan IPsec configuration file
# This file: /etc/ipsec.conf
#
# Enable when using this configuration file with openswan instead of libreswan
#version 2
#
# Manual: ipsec.conf.5
# basic configuration
config setup
# which IPsec stack to use, "netkey" (the default), "klips" or "mast".
# For MacOSX use "bsd"
protostack=netkey
#
# Normally, pluto logs via syslog. If you want to log to a file,
# specify below or to disable logging, eg for embedded systems, use
# the file name /dev/null
# Note: SElinux policies might prevent pluto writing to a log file at
# an unusual location.
#logfile=/var/log/pluto.log
#
# The interfaces= line is only required for the klips/mast stack
#interfaces="%defaultroute"
#interfaces="ipsec0=eth0 ipsec1=ppp0"
#
# If you want to limit listening on a single IP - not required for
# normal operation
#listen=127.0.0.1
#
# Do not set debug options to debug configuration issues!
#
# plutodebug / klipsdebug = "all", "none" or a combation from below:
# "raw crypt parsing emitting control kernel pfkey natt x509 dpd
# private".
# Note: "crypt" is not included with "all", as it can show confidential
# information. It must be specifically specified
# examples:
# plutodebug="control parsing"
# plutodebug="all crypt"
# Again: only enable plutodebug or klipsdebug when asked by a developer
#plutodebug=none
#klipsdebug=none
#
# Enable core dumps (might require system changes, like ulimit -C)
# This is required for abrtd to work properly
# Note: SElinux policies might prevent pluto writing the core at
# unusual locations
dumpdir=/var/run/pluto/
#
# NAT-TRAVERSAL support
# exclude networks used on server side by adding %v4:!a.b.c.0/24
# It seems that T-Mobile in the US and Rogers/Fido in Canada are
# using 25/8 as "private" address space on their wireless networks.
# This range has never been announced via BGP (at least upto 2015)
virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v4:25.0.0.0/8,%v4:100.64.0.0/10,%v6:fd00::/8,%v6:fe80::/10
oe=off
nat_traversal=yes
# For example connections, see your distribution's documentation directory,
# or https://libreswan.org/wiki/
#
# There is also a lot of information in the manual page, "man ipsec.conf"
#
# It is best to add your IPsec connections as separate files in /etc/ipsec.d/
#include /etc/ipsec.d/*.conf
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=107.19.15.162 ###设备公网IP
leftprotoport=17/1701
right=%any
rightprotoport=17/%any
设置预共享密钥(PSK)
vim /etc/ipsec.secrets
上面的文件应该要自己建立,内容编辑成这样
SERVER-IP:服务器的IP地址
SharedKey:你自己设置的PSK
SERVER-IP%any: PSK"SharedKey"
线上配置:
#include /etc/ipsec.d/*.secrets
107.19.15.162 %any: PSK "vpn"
设置完毕以后ipsec就搞定了
修改/添加 /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,不要管它,继续下一步。
验证ipsec的运行状态
service ipsec start
# ipsec verify
Verifying installed system and configuration files
Version check and ipsec on-path [OK]
Libreswan 3.15 (netkey) on 2.6.32-573.el6.x86_64
Checking for IPsec support in kernel [OK]
NETKEY: Testing XFRM related proc values
ICMP default/send_redirects [OK]
ICMP default/accept_redirects [OK]
XFRM larval drop [OK]
Pluto ipsec.conf syntax [OK]
Hardware random device [N/A]
Two or more interfaces found, checking IP forwarding [OK]
Checking rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/lo/rp_filter [ENABLED]
/proc/sys/net/ipv4/conf/em1/rp_filter [ENABLED]
rp_filter is not fully aware of IPsec and should be disabled
Checking that pluto is running [OK]
Pluto listening for IKE on udp 500 [OK]
Pluto listening for IKE/NAT-T on udp 4500 [OK]
Pluto ipsec.secret syntax [OK]
Checking 'ip' command [OK]
Checking 'iptables' command [OK]
Checking 'prelink' command does not interfere with FIPS [PRESENT]
Checking for obsolete ipsec.conf options [OK]
Opportunistic Encryption [DISABLED]
ipsec verify: encountered 5 errors - see 'man ipsec_verify' for help
安装依赖(xl2tpd)
查看系统是否安装epel-release及其版本
rpm -q epel-release
正确返回的是6.8
epel-release-6-8.noarch
如无返回安装对应版本的epel源
rpm -Uvh http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
安装完成后安装xl2tpd
yum install xl2tpd -y
cd /etc/
mkdir xl2tpd/
cd xl2tpd/
touch xl2tpd.conf
chmod 666 xl2tpd.conf
编辑xl2tpd.conf成下面这个样子设置以下你想分配给客户端的地址以及虚拟服务器的IP
vim /etc/xl2tpd/xl2tpd.conf
[lns default]
ip range = 192.168.1.128-192.168.1.254 #这里是VPN client的内网ip地址范围
local ip = 192.168.1.99 #这里是VPN server的内网地址
require chap = yes
refuse pap = yes
require authentication = yes
name = LinuxVPNserver
ppp debug = yes
pppoptfile = /etc/ppp/options.xl2tpd
length bit = yes
[global]
ipsec saref = no
修改xl2tp配置
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
mtu 1400
noccp
connect-delay 5000
上面的noccp用来解决IOS连接问题
添加账号密码
vim /etc/ppp/chap-secrets
格式如下
账号 l2tp密码 *
账号密码用英文和数字 *可以改为指定使用上面IP段中的IP地址
线上配置:
# Secrets for authentication using CHAP
# client server secret IP addresses
admin * admin *
test1 * test1 *
此设置表示,vpn的账户为admin 密码为admin,可以在任何可以上网的客机上登陆此vpn账户,类似创建test1账号,密码为test1,每次添加后重启xl2tpd即可。
启动 xl2tpd 服务
service xl2tpd start
开放端口及转发
iptables -t nat -A POSTROUTING -m policy --dir out --pol none -j MASQUERADE
iptables -A FORWARD -i ppp+ -p all -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o em1 -j MASQUERADE
iptables -I INPUT -p udp -m multiport --dport 1701,4500,500 -j ACCEPT
iptables -I FORWARD -s 192.168.1.0/24 -j ACCEPT
iptables -I FORWARD -d 192.168.1.0/24 -j ACCEPT
vim /etc/sysconfig/iptables
# Generated by iptables-save v1.4.7 on Thu Sep 1 17:02:17 2016
*nat
:PREROUTING ACCEPT [10:418]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A POSTROUTING -m policy --dir out --pol none -j MASQUERADE
-A POSTROUTING -s 192.168.1.0/24 -o em1 -j MASQUERADE
COMMIT
# Completed on Thu Sep 1 17:02:17 2016
# Generated by iptables-save v1.4.7 on Thu Sep 1 17:02:17 2016
*filter
:INPUT ACCEPT [23:1576]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [22:2834]
-A INPUT -p tcp -m state --state NEW -m tcp --dport 23432 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p udp -m multiport --dports 1701,4500,500 -j ACCEPT
-A FORWARD -d 192.168.1.0/24 -j ACCEPT
-A FORWARD -s 192.168.1.0/24 -j ACCEPT
-A FORWARD -i ppp+ -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
COMMIT
# Completed on Thu Sep 1 17:02:17 2016
保存规则
service iptables save
IPsec/xl2tpd VPN 已全部配置完成
下面就可以测试了。
service xl2tpd restart
service iptables restart
chkconfig xl2tpd on
chkconfig iptables on
chkconfig ipsec on
下面我们直接创建VPN账号开始登陆:至此,VPN搭建和使用完成!