Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 上架設 PPTP VPN:一鍵安裝腳本與開機自啟設定

懶得每次重開機後手動把服務拉起來?先把開機自啟設定好,再搭配一鍵腳本,PPTP VPN 的部署可以省下不少功夫。

設定開機自動啟動

# chkconfig pptpd on    # 開機啟動 pptp vpn 服務
# chkconfig iptables on # 開機啟動 iptables

OpenVZ 環境的一鍵安裝腳本(CentOS / Fedora / RedHat 6.x)

腳本會引導你建立伺服器設定與第一個使用者,也能在日後追加帳號。使用前請先向服务商確認已啟用 iptables 與 ppp 模組(SolusVM 面板也能開啟)。

#!/bin/bash
# Interactive pptp vpn install script for an OpenVZ VPS
# support : CentOS, Fedora 6.x
# v1.00

echo "######################################################"
echo "Interactive PoPToP Install Script for an OpenVZ VPS"
echo "Make sure to contact your provider and have them enable"
echo "IPtables and ppp modules prior to setting up PoPToP."
echo "PPP can also be enabled from SolusVM."
echo "You need to set up the server before creating more users."
echo "A separate user is required per connection or machine."
echo "######################################################"
echo "Select on option:"
echo "1) Set up new PoPToP server AND create one user"
echo "2) Create additional users"
echo "######################################################"
read x
if test $x -eq 1; then
  echo "Enter username that you want to create (eg. client1 or john):"
  read u
  echo "Specify password that you want the server to use:"
  read p

  echo "Downloading and Installing ppp and pptpd"
  yum install ppp -y
  rpm -Uvh https://poptop.sourceforge.net/yum/stable/rhel6/pptp-release-current.noarch.rpm
  yum install pptpd -y

  echo "Creating Server Config"
  cp /etc/ppp/options.pptpd /etc/ppp/options.pptpd.bak
  sed -i '70a ms-dns 8.8.8.8' /etc/ppp/options.pptpd

  # setting up pptpd.conf
  sed -i '101a localip 192.168.9.1' /etc/pptpd.conf
  sed -i '102a remoteip 192.168.9.11-30' /etc/pptpd.conf

  # adding new user
  echo "$u * $p *" >> /etc/ppp/chap-secrets

  echo "Forwarding IPv4 and Enabling it on boot"
  cat >> /etc/sysctl.conf <<END
net.ipv4.ip_forward=1
END
  sysctl -p

  echo "Updating IPtables Routing and Enabling it on boot"
  iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  iptables-save > /etc/iptables.conf

  cat > /etc/network/if-pre-up.d/iptables <<END
#!/bin/sh
iptables-restore < /etc/iptables.conf
END
  chmod +x /etc/network/if-pre-up.d/iptables
  cat >> /etc/ppp/ip-up <<END
ifconfig ppp0 mtu 1400
END

  echo "Restarting PoPToP"
  sleep 5
  /etc/init.d/pptpd restart

  echo "Server setup complete!"
  echo "Connect to your VPS with these credentials:"
  echo "Username:$u ##### Password: $p"
elif test $x -eq 2; then
  echo "Enter username that you want to create (eg. client1 or john):"
  read u
  echo "Specify password that you want the server to use:"
  read p
  echo "$u * $p *" >> /etc/ppp/chap-secrets
  echo "Addtional user added!"
  echo "Username:$u ##### Password: $p"
else
  echo "Invalid selection, quitting."
  exit
fi

Debian / Ubuntu 系列的一鍵腳本

把下面的內容存成 install.sh,執行 sh install.sh 即可。這支腳本只適用 Debian 與 Ubuntu,CentOS 請改用上面那支。

#!/bin/bash
# Interactive PoPToP install script for an OpenVZ VPS
# Tested on Debian 5, 6, and Ubuntu 11.04
# v1.11

echo "######################################################"
echo "Interactive PoPToP Install Script for an OpenVZ VPS"
echo "Make sure to contact your provider and have them enable"
echo "IPtables and ppp modules prior to setting up PoPToP."
echo "######################################################"
echo "Select on option:"
echo "1) Set up new PoPToP server AND create one user"
echo "2) Create additional users"
echo "######################################################"
read x
if test $x -eq 1; then
  echo "Enter username that you want to create (eg. client1 or john):"
  read u
  echo "Specify password that you want the server to use:"
  read p

  # get the VPS IP
  ip=`ifconfig venet0:0 | grep 'inet addr' | awk {'print $2'} | sed s/.*://`

  echo "Downloading and Installing PoPToP"
  apt-get update
  apt-get -y install pptpd

  echo "Creating Server Config"
  cat > /etc/ppp/pptpd-options <<END
name pptpd
refuse-pap
refuse-chap
refuse-mschap
require-mschap-v2
require-mppe-128
ms-dns 8.8.8.8
ms-dns 8.8.4.4
proxyarp
nodefaultroute
lock
nobsdcomp
END

  # setting up pptpd.conf
  echo "option /etc/ppp/pptpd-options" > /etc/pptpd.conf
  echo "logwtmp" >> /etc/pptpd.conf
  echo "localip $ip" >> /etc/pptpd.conf
  echo "remoteip 10.1.0.1-100" >> /etc/pptpd.conf

  # adding new user
  echo "$u * $p *" >> /etc/ppp/chap-secrets

  echo "Forwarding IPv4 and Enabling it on boot"
  cat >> /etc/sysctl.conf <<END
net.ipv4.ip_forward=1
END
  sysctl -p

  echo "Updating IPtables Routing and Enabling it on boot"
  iptables -t nat -A POSTROUTING -j SNAT --to $ip
  iptables-save > /etc/iptables.conf

  cat > /etc/network/if-pre-up.d/iptables <<END
#!/bin/sh
iptables-restore < /etc/iptables.conf
END
  chmod +x /etc/network/if-pre-up.d/iptables
  cat >> /etc/ppp/ip-up <<END
ifconfig ppp0 mtu 1400
END

  echo "Restarting PoPToP"
  sleep 5
  /etc/init.d/pptpd restart

  echo "Server setup complete!"
  echo "Username:$u ##### Password: $p"
elif test $x -eq 2; then
  echo "Enter username that you want to create (eg. client1 or john):"
  read u
  echo "Specify password that you want the server to use:"
  read p
  ip=`ifconfig venet0:0 | grep 'inet addr' | awk {'print $2'} | sed s/.*://`
  echo "$u * $p *" >> /etc/ppp/chap-secrets
  echo "Addtional user added!"
  echo "Username:$u ##### Password: $p"
else
  echo "Invalid selection, quitting."
  exit
fi

兩支腳本都會自動完成 ppp/pptpd 安裝、DNS 與位址池設定、IPv4 轉送、NAT 規則以及 MTU 調整,跑完就能直接連線。提醒一句:PPTP 的加密強度已不建議用於敏感用途,正式環境請优先考虑 WireGuard、OpenVPN 等方案。

Copyright © Linux教程網 All Rights Reserved