Have you ever wanted to set up your own VPN server? By following the steps below, you can set up your own L2TP VPN server on CentOS 6. Note that an L2TP VPN, which we’re setting up here, is more secure than a PPTP VPN server. OpenVPN is another alternative to L2TP VPNs, but OpenVPN requires OpenVPN software on the client device. In contrast, L2TP VPNs are supported out of the box in most modern operating systems (Windows, Mac OS X, Ubuntu, RHEL, CentOS) as well as mobile devices (iOS [iPhones, iPads], Android, and Windows Phone).

Packages to install

  1. yum install lsof man
  2. yum install openswan
  3. yum install ppp xl2tpd

Note: You need to have the epel repository installed to install xl2tpd. To install epel if you haven’t already, check this post.

Potential OpenSwan version issues with iOS devices behind NAT

OpenSwan version 2.6.32-18.el6_3 had a bug wherein iOS devices were unable to make a successful VPN connection if they were behind NAT, which includes attempting a VPN connection while connected to a cellular network. Downgrading to version 2.6.32-16.el6 allowed iOS devices to again connect.

It appears that this bug is fixed as of version 2.6.32-21.2.el6, but do note if you have trouble getting iOS devices to work behind NAT, it may be due to the version of OpenSwan you’ve installed.

For more information, see this page: http://bugs.centos.org/view.php?id=5832

IP Addresses in this example

 In the following configuration files, various IP addresses are listed. Change these IPs to match your environment

  • 10.0.100.0/24 – Internal LAN IP subnet: This is the IP subnet used on your local LAN that the VPN server resides upon.
  • 10.0.100.3 – Local IP used by VPN server for the L2TP tunnels: This is a completely made up number – you are assigning an IP address to the tunnel side of your VPN server. Make sure the IP you assign is not within your DHCP server’s DHCP scope.
  • 10.0.100.50-100 – Local IP range to be handed out to VPN-connected clients: You define your own range here – make sure it’s in the same subnet as your local LAN but not part of your DHCP scope.
  • 10.0.100.2 – IP address of VPN server: This is the primary IP address of the VPN server on your local LAN – this should be a static or statically assigned address.
  • 10.0.100.1 – DNS server: This is the DNS server that the L2TP VPN-connected clients will use.
  • 10.0.100.1 – Gateway/Router: This is the IP address of the border router on your internal network – it can be NAT’ed.

Configuration Files

/etc/sysctl.conf 

Edit the file to allow IP forwarding:
  1. # Controls IP packet forwarding
  2. net.ipv4.ip_forward = 1

Reload sysctl with this command:

  1. sysctl -p

/etc/rc.local

Add the following block to the bottom of the configuration file:

  1. # Correct ICMP Redirect issues with OpenSWAN
  2.  
  3. for each in /proc/sys/net/ipv4/conf/*; do
  4.         echo 0 > $each/accept_redirects
  5.         echo 0 > $each/send_redirects
  6.        echo 0 > $each/rp_filter
  7. done

/etc/ipsec.conf

In the ipsec.conf file, you define a “left” and “right” side of the IPsec connection. In this example, the “left” side is your internal LAN, while the “right” side is the remote, client side which originates on port 1701.Make sure to change the virtual_private line to match your internal LAN subnet.

nehelpers is set to 0 to work around an error message when network helpers are not available.

plutodebug may be set to “control” if you wish to see messages logged in /var/log/pluto.log. Note: I do not suggest leaving this turned on as the log file will quickly grow to a massive size.

Additional lines are added at the bottom of the defined L2TP-PSK connection to better handle compatibility with Mac OS X and iOS clients.

  1. # /etc/ipsec.conf - Openswan IPsec configuration file
  2. #
  3. # Manual:     ipsec.conf.5
  4. #
  5. # Please place your own config files in /etc/ipsec.d/ ending in .conf
  6.  
  7. version       2.0    # conforms to second version of ipsec.conf specification
  8.  
  9. # basic configuration
  10.  
  11. config setup
  12.        interfaces=%defaultroute
  13.        klipsdebug=none
  14.        nat_traversal=yes
  15.        nhelpers=0
  16.        oe=off
  17.        plutodebug=none
  18.        plutostderrlog=/var/log/pluto.log
  19.        protostack=netkey
  20.        virtual_private=%v4:10.0.100.0/24
  21. conn L2TP-PSK
  22.        authby=secret
  23.        pfs=no
  24.        auto=add
  25.        keyingtries=3
  26.        rekey=no
  27.        type=transport
  28.        forceencaps=yes
  29.        right=%any
  30.        rightsubnet=vhost:%any,%priv
  31.        #rightprotoport=17/0 rightprotoport=17/%any 多客户端不同账号同一IP连接VPN,尝试过有问题
  32.        # Using the magic port of "0" means "any one single port". This is
  33.        # a work around required for Apple OSX clients that use a randomly
  34.        # high port, but propose "0" instead of their port.
  35.        left=%defaultroute
  36.        leftprotoport=17/1701
  37.        # Apple iOS doesn't send delete notify so we need dead peer detection
  38.        # to detect vanishing clients
  39.        dpddelay=10
  40.        dpdtimeout=90
  41.        dpdaction=clear
  42.  
  43. #You may put your configuration (.conf) file in the "/etc/ipsec.d/" and uncomment this.
  44. #include /etc/ipsec.d/*.conf

/etc/ipsec.secrets 

First, create a new host key for the machine — this example is using a pre-shared key (PSK), but it’s still a good idea to generate the machine key:

  1.     ipsec newhostkey --output /etc/ipsec.secrets --bits 2048 --verbose --configdir /etc/pki/nssdb/

Next, add a line with the internal LAN IP address of the server, the var %any:PSK to use the pre-shared key, and then define the pre-shared key in quotes.

  1.     10.0.100.2 %any:     PSK     "yourPSKHere"

In the /etc/IPsec.secrets file, make sure you remove or uncomment the line “include /etc/ipsec.d/*.secrets,” or you’ll get an error and the VPN just won’t connect.

  1. : RSA  {
  2.  
  3. # Your RSA generated machine key will be here after running the above IPsec newhostkey command
  4.  
  5. }
  6.  
  7. # do not change the indenting of that "}"
  8.     10.0.100.2      %any:     PSK     "yourPSKHere"
Make sure you set the permissions on your secrets file to keep it private.
  1. sudo chown root:root /etc/ipsec.secrets
  2. sudo chmod 600 /etc/ipsec.secrets

/etc/xl2tpd/xl2tpd.conf

This is the xl2tpd configuration file. Make sure to change the listen-addr to that of your server, the ip range for your VPN clients, and the local IP of the VPN interface on the server.
  1. [global]
  2. listen-addr = 10.0.100.2
  3. ;
  4. ; requires openswan-2.5.18 or higher - Also does not yet work in combination
  5. ; with kernel mode l2tp as present in linux 2.6.23+
  6. ; ipsec saref = yes
  7. ; Use refinfo of 22 if using an SAref kernel patch based on openswan 2.6.35 or
  8. ;  when using any of the SAref kernel patches for kernels up to 2.6.35.
  9. ; ipsec refinfo = 30
  10. ;
  11. ; works around bug: http://bugs.centos.org/view.php?id=5832
  12.  
  13. force userspace = yes
  14.  
  15. ;
  16. [lns default]
  17. ip range = 10.0.100.50-10.0.100.100
  18. local ip = 10.0.100.3
  19. ; leave chap unspecified for maximum compatibility with windows, iOS, etc
  20. ; require chap = yes
  21. refuse pap = yes
  22. require authentication = yes
  23. name = CentOSVPNserver
  24. ppp debug = yes
  25. pppoptfile = /etc/ppp/options.xl2tpd
  26. length bit = yes

/etc/ppp/options.xl2tpd

ms-dns should be set to the DNS server you wish the VPN clients to use. You can specify multiple DNS servers by adding multiple ms-dns entries on separate lines.

  1. ipcp-accept-local
  2. ipcp-accept-remote
  3. ms-dns  10.0.100.1
  4. # ms-dns  192.168.1.1
  5. # ms-dns  192.168.1.3
  6. # ms-wins 192.168.1.2
  7. # ms-wins 192.168.1.4
  8. noccp
  9. auth
  10. crtscts
  11. idle 1800
  12. mtu 1410
  13. mru 1410
  14. nodefaultroute
  15. debug
  16. lock
  17. proxyarp
  18. connect-delay 5000
  19. logfile /var/log/ppp.log

/etc/ppp/chap-secrets

This is the file in which you define your user accounts for the VPN — they are in username and password pairs.

  1. # Secrets for authentication using CHAP
  2. # client        server  secret                  IP addresses
  3. user1 *       sgrongPassword1 *
  4. user2           *       strongPassword2 *

Secure the /etc/ppp/chap-secrets file

  1. sudo chown root:root /etc/ppp/chap-secrets
  2. sudo chmod 600 /etc/ppp/chap-secrets

IPTables Configuration

If you are running IPTables as the firewall on your VPN server, run the following commands to allow functioning VPN access

  1. #Allow ipsec traffic
  2. iptables -A INPUT -m policy --dir in --pol ipsec -j ACCEPT
  3. iptables -A FORWARD -m policy --dir in --pol ipsec -j ACCEPT
  4.  
  5. #Do not NAT VPN traffic
  6. iptables -t nat -A POSTROUTING -m policy --dir out --pol none -j MASQUERADE
  7.  
  8. #Forwarding rules for VPN
  9. iptables -A FORWARD -i ppp+ -p all -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
  10. iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
  11.  
  12. #Ports for Openswan / xl2tpd
  13. iptables -A INPUT -m policy --dir in --pol ipsec -p udp --dport 1701 -j ACCEPT
  14. iptables -A INPUT -p udp --dport 500 -j ACCEPT
  15. iptables -A INPUT -p udp --dport 4500 -j ACCEPT
  16.  
  17. #Save your configuration
  18. iptables save

Note that if your current firewall configuration contains the following lines, your VPN connection will fail!

Remove these lines if they exist in your /etc/sysconfig/iptables file:

iptables -A INPUT -j REJECT –reject-with icmp-host-prohibited iptables -A FORWARD -j REJECT –reject-with icmp-host-prohibited, the VPN connection will fail!

Enable and Start Services

  1. chkconfig xl2tpd on
  2. chkconfig ipsec on
  3. service ipsec start
  4. service xl2tpd start

Optional configuration

Ignore ICMP Redirects:

  1. for f in /proc/sys/net/ipv4/conf/*/accept_redirects; do echo 0 > $f; done

Don’t send ICMP Redirects:

  1. for f in /proc/sys/net/ipv4/conf/*/send_redirects; do echo 0 > $f; done

Troubleshooting

To see if your IPsec configuration looks OK, run the following command:

  1. ipsec verify

You can also enable logging in /etc/ipsec.conf by setting plutodebug to “control”. This will log messages to /var/log/pluto.log.

There is an additional log file in /var/log/ppp.log.

Helpful links

http://confoundedtech.blogspot.com/2011/08/android-nexus-one-ipsec-psk-vpn-with.html

http://coding.zencoffee.org/2012/10/ipsecl2tp-vpn-server-on-centos-6-psk.html

http://amadys.blogspot.com/2010/06/openswan-2626-ipsec-for-linux.html

http://comments.gmane.org/gmane.network.openswan.user/20373

http://www.drlongghost.com/wordpress/2011/04/22/trying-to-get-iphone-to-ubuntu-vpn-working-with-openswan/

http://www.pariahzero.net/Blog/files/e7d5abf84a96640d5cd70dd0dfb3d200-71.html

http://agit8.turbulent.ca/bwp/2011/01/setting-up-a-vpn-server-with-ubuntu-1004-and-strongswan/

Categories: CentOS, Linux, VPN

How to: Set up Openswan L2TP VPN Server on CentOS 6的更多相关文章

  1. 通过openswan基于Azure平台搭建VPN server

    用过Azure的读者都知道,Vnet一直是Azure比较自豪的地方,尤其是VPN,Azure提供了两种VPN以及专线来保证客户数据的安全性,S2S vpn(站点到站点的,基于IPsec的),P2S v ...

  2. CentOS Linux VPS安装IPSec+L2TP VPN

    CentOS Linux VPS安装IPSec+L2TP VPN 时间 -- :: 天使羊波波闪耀光芒 相似文章 () 原文 http://www.live-in.org/archives/818.h ...

  3. setting up a IPSEC/L2TP vpn on CentOS 6 or Red Hat Enterprise Linux 6 or Scientific Linux

    This is a guide on setting up a IPSEC/L2TP vpn on CentOS 6 or Red Hat Enterprise Linux 6 or Scientif ...

  4. Win10 连接L2TP VPN 失败解决方法

    Win10 连接L2TP VPN 失败解决方法 iOS系统不知道在什么时候,已经不支持PPTP VPN.偶尔的机会刚好看到github上的一键式VPN服务器部署脚本setup-ipsec-vpn,就在 ...

  5. 如何在Ubuntu 11.10上连接L2TP VPN

    要在家继续项目的开发,但架设的GitLab只能校内访问,更悲催的是学校架设的SSL VPN不支持Linux,好在想起学校以前架设的L2TP VPN,应该可以支持Linux,于是便一通谷歌百度,然而发现 ...

  6. Andorid 6连接Libreswan L2TP VPN

    手机升级到Android 6以后,以前正常使用的L2TP VPN却无法连接了.服务器端日志: "vpnpsk"[119] 114.249.245.192 #240: no acce ...

  7. Centos6一键搭建L2TP VPN服务器

    用VPS在墙上打洞还有一种叫L2TP,也是常见的一种方式.本脚本结合了L2TP(Layer Tunneling Protocol)和IPSec(Internet Protocol Security), ...

  8. juniper防火墙 L2TP VPN配置

    juniper防火墙 L2TP  VPN配置 建立L2TP_POOL 创建连接的用户: 创建用户组: 更改L2TP的连接池: 更改L2TP的隧道: 设置防火墙的策略: Win7连接:

  9. Installing MySQL Server on CentOS

    MySQL is an open-source relational database. For those unfamiliar with these terms, a database is wh ...

随机推荐

  1. 第二章:Javascript词法结构

    编程语言的词法结构是一套基础性的规则,用来描述你如何编写这门语言.作为语法的基础,它规定了变量名是怎么样的,如何写注释,以及语句之间是如何区分的.本节用很短的篇幅来介绍javascript的词法结构. ...

  2. c++ 中 delete p与 delete []p的区别

    #include <cstdio> class A{private: int i;public: ~A() { printf("hi"); }};void d(A *) ...

  3. zoj3888 找第二大

    题目简化后最终要求的就是第二大的数.但是由于数据较大,不能直接求.可以先预处理,求出所有情况. #include<stdio.h> #include<string.h> #in ...

  4. hdu1247 字典树

    开始以为枚举会超时,因为有50000的词.后来试了一发就过了.哈哈.枚举没一个单词,将单词拆为2半,如果2半都出现过,那就是要求的. #include<stdio.h> #include& ...

  5. 第六节 JBPM版本控制以及Token对象

    1.JBPM版本 2.Token 3.流程上下文

  6. 【bzoj1037】 ZJOI2008—生日聚会Party

    http://www.lydsy.com/JudgeOnline/problem.php?id=1037 (题目链接) 题意 有n个boy和m个girl排成一排,求使得任意一段的boy个数girl个数 ...

  7. POJ1088 滑雪

    Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道 ...

  8. 洛谷P2507 [SCOI2008]配对

    题目背景 四川NOI2008省选 题目描述 你有 n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一个Bp[i].要求所有配对的整数差的绝对值之和尽量小,但不允许两个相同的数配对. ...

  9. bzoj2054 疯狂的馒头

    bzoj上现在找不到这题,所以目前只是过了样例,没有测 2054: 疯狂的馒头 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 715  Solved: ...

  10. UVa 11988 Broken Keyboard (a.k.a. Beiju Text)

    题目复制太麻烦了,甩个链接 http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18693 直接模拟光标操作时间复杂度较高,所以用链 ...