CentOS 7 安装VPN Server 和 Client

来源:互联网 发布:nginx 重启 windows 编辑:程序博客网 时间:2024/05/24 04:48

1. CentOS 7 安装VPN Server

参考 http://www.lkycn.com/2015/03/23/425.html
阿里云的论坛-架设vpn服务器
阿里云论坛-架设vpn客户端
* 先看看你的主机是否支持pptp,返回结果为yes就表示通过。

    modprobe ppp-compress-18 && echo yes
  • 是否开启了TUN,有的虚拟机主机需要开启,返回结果为cat: /dev/net/tun: File descriptor in bad state。就表示通过。

    cat /dev/net/tun
  • 安装ppp和pptpd

    yum -y install ppp pptpd
  • 配置pptpd.conf

    vim  /etc/pptpd.conf  localip 192.168.9.1remoteip 172.168.9.101-200#Localip这行是给vpn服务器设置一个隧道ip#Remoteip是自动分配给客户端的ip地址范围。
  • 配置options.pptpd

    vim /etc/ppp/options.pptpd      #在末尾添加dns#先去掉debug前面的#,开启日志记录ms-dns  202.96.209.5  ms-dns  8.8.8.8logfile /var/log/pptpd.log
  • 配置连接VPN客户端要用到的帐号密码。

    vim /etc/ppp/chap-secrets    #格式很通俗易懂。#client为帐号,server是pptpd服务,secret是密码,*表示是分配任意的ip#Secrets for authentication using CHAP#client     server     secret          IP addresses vpn        pptpd       vpn             *
  • 配置sysctl.conf

    vi /etc/sysctl.conf#添加一行    net.ipv4.ip_forward = 1    #到末尾即可,然后保存,这个很重要,系统路由模式功能。sysctl -p    #运行这个命令会输出上面添加的那一行信息,意思是使内核修改生效
  • 配置iptables的转发规则

    这个时候把iptables关闭的话是可以连接VPN了,之所以要把iptables关闭是因为没有开放VPN的端口,客户如果直接连接的话是不允许的。这里还需要设置iptables的转发规则,让你的客户端连接上之后能访问外网。

iptables -t nat -A POSTROUTING -s 192.168.9.0/24 -j MASQUERADE
  • 在centos 7 中由于使用firewalld 来替换了iptables,所以需要用如下命令来配置转发规则
firewall-cmd --permanent --direct --passthrough ipv4 -t nat -I POSTROUTING -o eth1 -j MASQUERADE -s 192.168.9.0/24

目标网络vpn的内部网络为192.168.9.0/24,vpn服务器有2个网卡,其中一个网卡连私有网络(eth0),ip是10.46.89.192,另外一个连接公网(eth1),ip地址是120.76.25.191。现在把所有 192.168.9.0 网段过来的请求,路由到外网。 这样就能正常地路由所涉及的网络了。

  • 配置iptables的端口

    iptables -I INPUT -p tcp --dport 1723 -j ACCEPTiptables -I INPUT -p tcp --dport 47 -j ACCEPTiptables -I INPUT -p gre -j ACCEPT

在centos 7 中,用firewalld 来取代了iptables

firewall-cmd --zone=public --add-port=1723/tcp --permanentfirewall-cmd --zone=public --add-port=47/tcp --permanentfirewall-cmd --permanent --direct --add-rule ipv4 filter INPUT 0 -p gre -j ACCEPTfirewall-cmd --permanent --direct --add-rule ipv6 filter INPUT 0 -p gre -j ACCEPTfirewall-cmd --reload   
  • 系统自动启动pptp的vpn server

    systemctl start pptpdsystemctl enable pptpd

2. CentOS 7 安装VPN Client

  • 安装ppp pptp pptp-setup

        # yum install ppp pptp pptp-setup  
  • 创建VPN连接

    pptpsetup --create vpn --server 10.46.89.192 --username vpn --password vpn --start
  • 加载模块

    # modprobe ppp_mppe  
  • 连接VPN连接

    # pppd call vpn  //这里的vpn是上面创建vpn连接  
  • 如果报以下错误:

    Connect: ppp0 <--> /dev/pts/3  CHAP authentication succeeded  LCP terminated by peer (MPPE required but peer refused)  Modem hangup  
  • 解决办法

        # vim /etc/ppp/peers/vpn  //vpn是上面创建的连接      文件尾部,加上以下内容      require-mppe-128  
  • 如果报以下错误:

    Refusing MPPE stateful mode offered by peer  MPPE required but peer negotiation failed  
  • 解决方法:
    说明服务端不支持MPPE加密,pptpsetup时不需要使用–encrypt选项。或者去掉上面的,require-mppe-128

  • 连接成功(连接成功地后,客户端和服务端都会有ppp0这样的东西。)

    # ifconfig |grep ppp  ppp0 Link encap:Point-to-Point Protocol   # route  Kernel IP routing table  Destination Gateway Genmask Flags Metric Ref Use Iface  li684-32.member 192.168.10.1 255.255.255.255 UGH 0 0 0 eth0  192.168.0.100 * 255.255.255.255 UH 0 0 0 ppp0  192.168.10.0 * 255.255.255.0 U 0 0 0 eth0  192.168.122.0 * 255.255.255.0 U 0 0 0 virbr0  link-local * 255.255.0.0 U 1002 0 0 eth0  default 192.168.10.1 0.0.0.0 UG 0 0 0 eth0  
  • 设置缺省路由

    ip route replace default dev ppp0

centos 7 上安装的问题和解决办法

  • 按照上述步骤安装后,在vpn的客户端可以ping 通外网地址,但是无法通过curl www.sina.com.cn 获取内容,出现《失败:没有到主机的路由》
  • 解决办法
    1. 在server端 关闭 firewalld 。
    2. 在server端 指定路由,并且放在 /etc/rc.d/rc.local里面随着系统启动

      #vpn server 的路由定义
      iptables -t nat -A POSTROUTING -s 192.168.9.0/24 -o eth1 -j MASQUERADE
    3. 在客户端这里 配置随系统启动的自动拨号和增加缺省路由的命令。也是放在/etc/rc.d/rc.local里面

      #自动拨号vpn
      pppd call vpn
      sleep 2
      ip route replace default dev ppp0

      应该有更好的方法来处理这个问题,但是一直没有找到。特别是centos 7 上面使用了firewalld 来取代了 iptables
0 0
原创粉丝点击