阿里云centos下vpn的搭建

来源:互联网 发布:js ajax请求跨域问题 编辑:程序博客网 时间:2024/06/01 10:22

配置完后发现网站无法打开,ftp无法访问,惊了下,然后考虑是不是防火墙把端口给限制了,结果真的是:

 /etc/init.d/iptables stop//关闭防火墙试试

  关闭之后再次查看114.80.*.*:80(即ip:80)发现果然成功。但是貌似安全隐患大大增加……使用

  /etc/init.d/iptables status

  查看防火墙信息,可以看到打开的端口。那么我们把需要使用的端口打开应该是一个比较可行的办法了,命令如下:

  /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #8080为指定端口

 /sbin/iptables -I INPUT -p tcp --dport 21 -j ACCEPT #8080为指定端口

  /etc/init.d/iptables restart #重启防火墙以便改动生效,当然如果不觉得麻烦也可重启系统(命令:reboot)

  /etc/rc.d/init.d/iptables save #将更改进行保存

  当然了,还有另外直接在/etc/sysconfig/iptables中增加一行:

  -A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT



一.原理

1.阿里云ECS上架设VPN.

2.本地连接使用VPN拨号到阿里云ECS.

3.使用阿里云ECS网络访问电信服务器.

使用VPN前效果

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2505 ttl=115 time=257.393 ms  
  2. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2506 ttl=115 time=254.470 ms  
  3. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2507 ttl=115 time=254.939 ms  
  4. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2508 ttl=115 time=247.348 ms  
  5. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2509 ttl=115 time=263.706 ms  
  6. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2510 ttl=115 time=246.666 ms  
  7. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2511 ttl=115 time=245.548 ms  
  8. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2512 ttl=115 time=243.927 ms  

使用VPN后效果

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2834 ttl=115 time=40.660 ms  
  2. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2835 ttl=115 time=41.702 ms  
  3. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2836 ttl=115 time=41.329 ms  
  4. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2837 ttl=115 time=143.046 ms  
  5. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2838 ttl=115 time=42.002 ms  
  6. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2839 ttl=115 time=41.551 ms  
  7. 64 bytes from xxx.xxx.xxx.xxx: icmp_seq=2840 ttl=115 time=42.558 ms  

效果还是相当明显地,使用前平均Ping 250ms 使用后平均Ping 40ms !!!

二.架设VPN

1.安装 ppp pptpd iptables

ppp 数据链路层协议

pptpd VPN服务类型之一

iptables 防火墙,用来消息转发

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo yum install ppp pptpd iptables  

安装后版本信息

[plain] view plaincopy
  1. $ lsb_release -a  
  2. LSB Version:    :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch  
  3. Distributor ID: CentOS  
  4. Description:    CentOS release 6.6 (Final)  
  5. Release:    6.6  
  6. Codename:   Final  
  7. $ pptpd --version  
  8. pptpd v1.4.0  
  9. $ iptables --version  
  10. iptables v1.4.7  

2.配置 ppp DNS信息

a.编辑 options.pptpd 配置文件

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo vim /etc/ppp/options.pptpd  

b.找到66和67行进行修改

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #ms-dns 10.0.0.1  
  2. #ms-dns 10.0.0.2  

c.配置后结果如下:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ms-dns 8.8.8.8  
  2. ms-dns 8.8.4.4  

3.配置 ppp VPN账号信息

a.编辑 chap-secrets 配置文件,这个文件默认只有两行注释,在注释下配置VPN账号信息

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo vim /etc/ppp/chap-secrets  

b.设置规则为 VPN账号 服务类型 VPN密码 IP,若IP为*则代表所有IP都可以使用该账号密码,配置后结果如下:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. # Secrets for authentication using CHAP  
  2. # client    server  secret          IP addresses  
  3. wangjijun   pptpd   wangjijun       *  
本例中账号密码都为 wangjijun

4.配置 pptpd

a.编辑 options.pptpd 配置文件

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo vim /etc/pptpd.conf  

b.修改如下内容,其实就是将注释配置项取消注释即可,这里配置地址很是纠结,其实个人理解为DHCP,跟服务器实际网卡无任何关系!

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. localip 192.168.0.1  
  2. remoteip 192.168.0.234-238,192.168.0.245  

5.配置内核支持转发

a.编辑 sysctl.conf 配置文件

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo vim /etc/sysctl.conf  

b.找到第7行进行修改

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. net.ipv4.ip_forward = 0  

c,配置后结果如下:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. net.ipv4.ip_forward = 1  

d.重新加载内核配置项

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo sysctl -p  

6.配置 iptables

a.首次运行 iptables

查看iptabls运行状态

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo service iptables status  
  2. iptables: Firewall is not running.  

有一个很有意思的现象,若首次启动 iptables ,使用如下 start 命令是无法启动的!

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo service iptables start  

但可以通过查看配置规则命令启动,不知道是不是我理解的有问题!如果你知道请指教,谢谢!

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo iptables -L -n  

清空防火墙配置

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo iptables -P INPUT ACCEPT #改成 ACCEPT 标示接收一切请求  
  2. $ sudo iptables -F #清空默认所有规则  
  3. $ sudo iptables -X #清空自定义所有规则  
  4. $ sudo iptables -Z #计数器置0  

配置规则

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo iptables -A INPUT -i lo -j ACCEPT #允许127.0.0.1访问本地服务  
  2. $ sudo iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT #允许访问外部服务  
  3. $ sudo iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT #允许 ping  
  4. $ sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT #开启 ssh 端口  

若需要配置其他端口,比如 80 3306 8080 参考 ssh 端口配置方式

b.已配置过 iptables

设置转发策略,由于阿里云是双网卡,内网eth0,外网eth1,所以这块特别容易误写为eth0

[plain] view plaincopy
  1. $ sudo iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -o eth1 -jMASQUERADE  

c.设置VPN端口策略

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo iptables -A INPUT -p tcp --dport 1723 -j ACCEPT  

d.保存 iptabls 选项并重启 iptables

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo service iptables save  

7.收尾工作

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. $ sudo service iptables restart #重新启动 iptables  
  2. $ sudo service pptpd restart #重新启动 pptpd  
  3. $ sudo chkconfig iptables on #开机启动 iptables  
  4. $ sudo chkconfig pptpd on #开机启动 pptpd  
  5. $ sudo iptables -P INPUT DROP #加载防火墙策略  

感谢以下作者

[教程]阿里云服务器配置VPN详解

阿里云centos配置VPN(PPTPD服务)

已解决,结帖Re阿里云Centos6.3搭了VPN内网通,但无法访问外网,折腾3天。

阿里云Centos配置iptables防火墙

0 0
原创粉丝点击