ECS自行搭建VPN

鄭重申明:本文用於記錄、總結、分享雲服務器自行搭建VPN的過程及方法。如將其作用於違反法律法規的事情,與本人無關。

1、查看當前系統版本,安裝相應的軟件包(此方法只在centos中測試通過)
#查看系統版本
cat  /etc/redhat-release
a)centos版本小於7,安裝以下軟件
yum -y install ppp pptpd iptables
b)centos版本大於7,還需額外安裝一下軟件
yum update iptables
yum -y install iptables iptables-services

2、配置pptpd服務
a)配置pptpd服務ip,取消默認文件localip、remoteip前的#,也可自行設置。主要用於服務地址及客戶端地址配置,客戶端地址在後面配置防火牆策略至關重要
vi /etc/pptpd.conf 
修改後(此處只截取了部分)
# (Recommended)
localip 192.168.0.1
remoteip 192.168.0.100-109,192.168.0.245
# or
#localip 192.168.0.234-238,192.168.0.245
#remoteip 192.168.1.234-238,192.168.1.245
b)配置ppp中DNS,增加8.8.8.8、8.8.4.4 GoogleDNS
vi /etc/ppp/options.pptpd 
修改後(此處只截取了部分)
#ms-dns 10.0.0.1
#ms-dns 10.0.0.2
ms-dns 8.8.8.8
ms-dns 8.8.4.4
c)配置客戶端用戶
vi /etc/ppp/chap-secrets 
修改後,分別爲用戶名、服務、密碼、ip。(ip爲 " * " 則代表所有ip均可訪問)
# Secrets for authentication using CHAP
# client        server  secret                  IP addresses
root    pptpd   root    *

3、開啓內核轉發
#查看內核配置(此處主要作用於修改內核轉發)
cat /etc/sysctl.conf
#修改爲允許轉發(默認=0),如果沒有自行添加即可
net.ipv4.ip_forward=1
#生效
sysctl -p 

4、配置防火牆策略
#查看防火牆當前配置
iptables -L -n
#允許所有請i去
iptables -P INPUT ACCEPT
# 清空默認所有規則
iptables -F
# 清空自定義所有規則
iptables -X
# 計數器置0
iptables -Z

# 允許127.0.0.1訪問本地服務
iptables -A INPUT -i lo -j ACCEPT
# 允許訪問外部服務
iptables -A INPUT -m state --state ESTABLISHED -j ACCEPT
# 允許 ping
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# 開啓 ssh 端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# 開啓 pptpd 端口
iptables -A INPUT -p tcp --dport 1723 -j ACCEPT

#配置轉發策略 192.168.0.* 到 MASQUERADE(會自動判斷公網IP)
iptables -A POSTROUTING -t nat -s 192.168.0.0/24 -o eth0 -j MASQUERADE
#此處需要注意,我購置的ECS爲專有網絡,開通了公網彈性IP,但是ifconfig下是隻有一塊內網網卡顯示。網上很多教程會有說明阿里雲的服務器是雙網卡,要修改成eth1,經過翻來覆去測試均已失敗告終。此處我用了內網網卡eth0,搭建成功。

5、保存並測試
#保存iptables配置
service iptables save 或者 /etc/init.d/iptables save
#重啓iptables服務
service iptables restart 或者 /etc/init.d/iptables restart 
#重啓pptpd服務,如果提示需要restart-kill,執行即可
service pptpd restart 或者 /etc/init.d/pptpd restart 

6、設置開機自啓
# 開機啓動 iptables
chkconfig iptables on
# 開機啓動 pptpd
chkconfig pptpd on
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章