vpn_centos.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/bash
  2. #
  3. # Author: yeho <lj2007331 AT gmail.com>
  4. # Blog: http://blog.linuxeye.com
  5. #
  6. # Installs a PPTP VPN-only system for CentOS
  7. # Check if user is root
  8. [ $(id -u) != "0" ] && { echo -e "\033[31mError: You must be root to run this script\033[0m"; exit 1; }
  9. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  10. clear
  11. printf "
  12. #######################################################################
  13. # OneinStack for CentOS/RadHat 5+ Debian 6+ and Ubuntu 12+ #
  14. # Installs a PPTP VPN-only system for CentOS #
  15. # For more information please visit http://oneinstack.com #
  16. #######################################################################
  17. "
  18. [ ! -e '/usr/bin/curl' ] && yum -y install curl
  19. VPN_IP=`curl ipv4.icanhazip.com`
  20. VPN_USER="linuxeye"
  21. VPN_PASS="linuxeye"
  22. VPN_LOCAL="192.168.0.150"
  23. VPN_REMOTE="192.168.0.151-200"
  24. while :
  25. do
  26. echo
  27. read -p "Please input username: " VPN_USER
  28. [ -n "$VPN_USER" ] && break
  29. done
  30. while :
  31. do
  32. echo
  33. read -p "Please input password: " VPN_PASS
  34. [ -n "$VPN_PASS" ] && break
  35. done
  36. clear
  37. if [ -f /etc/redhat-release -a -n "`grep ' 7\.' /etc/redhat-release`" ];then
  38. #CentOS_REL=7
  39. if [ ! -e /etc/yum.repos.d/epel.repo ];then
  40. cat > /etc/yum.repos.d/epel.repo << EOF
  41. [epel]
  42. name=Extra Packages for Enterprise Linux 7 - \$basearch
  43. #baseurl=http://download.fedoraproject.org/pub/epel/7/\$basearch
  44. mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=\$basearch
  45. failovermethod=priority
  46. enabled=1
  47. gpgcheck=0
  48. EOF
  49. fi
  50. for Package in wget make openssl gcc-c++ ppp pptpd iptables iptables-services
  51. do
  52. yum -y install $Package
  53. done
  54. echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
  55. elif [ -f /etc/redhat-release -a -n "`grep ' 6\.' /etc/redhat-release`" ];then
  56. #CentOS_REL=6
  57. for Package in wget make openssl gcc-c++ iptables ppp
  58. do
  59. yum -y install $Package
  60. done
  61. sed -i 's@net.ipv4.ip_forward.*@net.ipv4.ip_forward = 1@g' /etc/sysctl.conf
  62. rpm -Uvh http://poptop.sourceforge.net/yum/stable/rhel6/pptp-release-current.noarch.rpm
  63. yum -y install pptpd
  64. else
  65. echo -e "\033[31mDoes not support this OS, Please contact the author! \033[0m"
  66. exit 1
  67. fi
  68. echo "1" > /proc/sys/net/ipv4/ip_forward
  69. sysctl -p /etc/sysctl.conf
  70. [ -z "`grep '^localip' /etc/pptpd.conf`" ] && echo "localip $VPN_LOCAL" >> /etc/pptpd.conf # Local IP address of your VPN server
  71. [ -z "`grep '^remoteip' /etc/pptpd.conf`" ] && echo "remoteip $VPN_REMOTE" >> /etc/pptpd.conf # Scope for your home network
  72. if [ -z "`grep '^ms-dns' /etc/ppp/options.pptpd`" ];then
  73. echo "ms-dns 8.8.8.8" >> /etc/ppp/options.pptpd # Google DNS Primary
  74. echo "ms-dns 209.244.0.3" >> /etc/ppp/options.pptpd # Level3 Primary
  75. echo "ms-dns 208.67.222.222" >> /etc/ppp/options.pptpd # OpenDNS Primary
  76. fi
  77. echo "$VPN_USER pptpd $VPN_PASS *" >> /etc/ppp/chap-secrets
  78. ETH=`route | grep default | awk '{print $NF}'`
  79. [ -z "`grep '1723 -j ACCEPT' /etc/sysconfig/iptables`" ] && iptables -I INPUT 4 -p tcp -m state --state NEW -m tcp --dport 1723 -j ACCEPT
  80. [ -z "`grep 'gre -j ACCEPT' /etc/sysconfig/iptables`" ] && iptables -I INPUT 5 -p gre -j ACCEPT
  81. iptables -t nat -A POSTROUTING -o $ETH -j MASQUERADE
  82. iptables -I FORWARD -p tcp --syn -i ppp+ -j TCPMSS --set-mss 1356
  83. service iptables save
  84. sed -i 's@^-A INPUT -j REJECT --reject-with icmp-host-prohibited@#-A INPUT -j REJECT --reject-with icmp-host-prohibited@' /etc/sysconfig/iptables
  85. sed -i 's@^-A FORWARD -j REJECT --reject-with icmp-host-prohibited@#-A FORWARD -j REJECT --reject-with icmp-host-prohibited@' /etc/sysconfig/iptables
  86. service iptables restart
  87. service pptpd restart
  88. chkconfig pptpd on
  89. clear
  90. echo -e "You can now connect to your VPN via your external IP \033[32m${VPN_IP}\033[0m"
  91. echo -e "Username: \033[32m${VPN_USER}\033[0m"
  92. echo -e "Password: \033[32m${VPN_PASS}\033[0m"