请选择 进入手机版 | 继续访问电脑版

网硕互联技术交流社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 2388|回复: 0

Centos 6.X 搭建 L2TP/IPSEC 详细教程

[复制链接]

主题

帖子

0

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
0
发表于 2017-9-28 00:09:33 | 显示全部楼层 |阅读模式
网上有很多的 L2TP/IPSEC一键安装脚本,但测试时,都有各种各样的错误出现。最终考虑还是自己手动配置。

在此声明:本教程仅供个人学习使用,请勿做非法用途。


环境准备

由于Centos默认源中已经没有xl2tpd,需要我们手动更新源,提供两种源的替换办法:
1)#安装epel源
  1. rpm -ivh http://mirrors.yun-idc.com/epel/6/x86_64/epel-release-6-8.noarch.rpm
复制代码


更新epel源CA证书:
  1. yum --disablerepo=epel -y update ca-certificates
复制代码


更新yum缓存:
  1. yum makecache
复制代码



2)使用阿里云源
详见

  1. http://plus.wsisp.net/thread-755-1-1.html
复制代码


两种源使用哪一种都可以



  1. #关闭防火墙
  2. service iptables stop
复制代码

  1. #关闭SELinux
  2. setenforce 0
复制代码

  1. #开启数据包转发
  2. sysctl -w net.ipv4.ip_forward=1
复制代码



2、软件安装

安装环境依赖包

  1. yum install -y make gcc gmp-devel xmlto bison flex xmlto libpcap-devel lsof vim-enhanced man
复制代码



安装软件包

  1. yum install -y openswan ppp xl2tpd
复制代码

3、配置

编辑 /etc/ipsec.conf
  1. vi /etc/ipsec.conf
复制代码

  1. config setup
  2.         nat_traversal=yes
  3.         virtual_private=%v4:10.0.0.0/8,%v4:192.168.1.0/24,%v4:172.16.0.0/12
  4.         oe=off
  5.         protostack=netkey
  6. conn L2TP-PSK-NAT
  7.         rightsubnet=vhost:%priv
  8.         also=L2TP-PSK-noNAT
  9. conn L2TP-PSK-noNAT
  10.         authby=secret
  11.         pfs=no
  12.         auto=add
  13.         keyingtries=3
  14.         rekey=no
  15.         ikelifetime=8h
  16.         keylife=1h
  17.         type=transport
  18.         left=0.0.0.0    #为服务器公网Ip
  19.         leftprotoport=17/1701
  20.         right=%any
  21.         rightprotoport=17/%any
复制代码


这里一定要注意部分参数前是TAB键增加的空格,否则会出错。


(2)编辑/etc/ipsec.secrets
  1. vi /etc/ipsec.secrets
复制代码

  1. include /etc/ipsec.d/*.secrets
  2. 117.18.15.12 %any: PSK "YourPsk"  #YourPsk 为PSK密钥
复制代码


(3) 修改/添加 /etc/sysctl.conf并生效
  1. vim /etc/sysctl.conf
复制代码


在/etc/sysctl.conf的末尾加上如下内容:

  1. net.ipv4.ip_forward = 1
  2. net.ipv4.conf.default.rp_filter = 0
  3. net.ipv4.conf.all.send_redirects = 0
  4. net.ipv4.conf.default.send_redirects = 0
  5. net.ipv4.conf.all.log_martians = 0
  6. net.ipv4.conf.default.log_martians = 0
  7. net.ipv4.conf.default.accept_source_route = 0
  8. net.ipv4.conf.all.accept_redirects = 0
  9. net.ipv4.conf.default.accept_redirects = 0
  10. net.ipv4.icmp_ignore_bogus_error_responses = 1
复制代码


生效上面的修改使用如下命令

  1. sysctl -p
复制代码


(4)验证ipsec运行状态

  1. ipsec restart

  2. ipsec verify

  3. [root@server17 ~]# ipsec verify
  4. Checking your system to see if IPsec got installed and started correctly:
  5. Version check and ipsec on-path [OK]
  6. Linux Openswan U2.6.32/K2.6.32-431.23.3.el6.x86_64 (netkey)
  7. Checking for IPsec support in kernel [OK]
  8. SAref kernel support [N/A]
  9. NETKEY: Testing for disabled ICMP send_redirects [OK]
  10. NETKEY detected, testing for disabled ICMP accept_redirects [OK]
  11. Checking that pluto is running [OK]
  12. Pluto listening for IKE on udp 500 [OK]
  13. Pluto listening for NAT-T on udp 4500 [OK]
  14. Two or more interfaces found, checking IP forwarding [OK]
  15. Checking NAT and MASQUERADEing [OK]
  16. Checking for 'ip' command [OK]
  17. Checking /bin/sh is not /bin/dash [OK]
  18. Checking for 'iptables' command [OK]
  19. Opportunistic Encryption Support [DISABLED]
复制代码

这里可能会出现一些错误,但测试并不影响正常的代理使用。具体根据自己的系统环境而定。


(5) 编辑 /etc/xl2tpd/xl2tpd.conf

  1. vi /etc/xl2tpd/xl2tpd.conf
  2. [lns default]
  3. ip range = 192.168.100.10-192.168.100.200  #客户端获取的IP范围
  4. local ip = 0.0.0.0      #服务器的IP
  5. require chap = yes
  6. refuse pap = yes
  7. require authentication = yes
  8. name = LinuxVPNserver
  9. ppp debug = yes
  10. pppoptfile = /etc/ppp/options.xl2tpd
  11. length bit = yes
复制代码

(6)配置用户名,密码:编辑 /etc/ppp/chap-secrets

  1. vi /etc/ppp/chap-secrets
复制代码


username 写登录vpn的用户名,userpass 写登录vpn的密码

  1. # Secrets for authentication using CHAP
  2. # client         server         secret         IP addresses
  3. vpn              *               "123456"        *
复制代码

(7)重启xl2tp

  1. service xl2tpd restart
复制代码


(8)添加自启动

  1. chkconfig xl2tpd on
  2. chkconfig iptables on
  3. chkconfig ipsec on
复制代码


(8)使用VPN服务器公网做为客户端互联网出口(跳板机、代理)
使用iptables实现,增加规则

  1. iptables -t nat -A POSTROUTING -o eth0 -s 192.168.1.0/24 -j MASQUERADE
复制代码


eth1为公网网卡,后面的Ip段为在xl2tpd.conf配置文件中设置的客户端IP

然后保存

  1. iptables save
复制代码


重启

  1. /etc/init.d/iptables restart
复制代码



到这里配置就完成了,已测苹果IOS10系统可以正常连接。






回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|网硕互联技术交流社区

GMT+8, 2024-3-29 02:36 , Processed in 0.232896 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表