wireguard-install.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #!/bin/bash
  2. # WireGuard installer for Ubuntu 18.04 LTS, Debian 9 and CentOS 7.
  3. # Usage: wget -qO- https://git.io/wireguard.sh | bash
  4. # This script will let you setup your own VPN server in no more than a minute, even if you haven't used WireGuard before.
  5. # It has been designed to be as unobtrusive and universal as possible.
  6. wireguard_install(){
  7. if [ -e /etc/centos-release ]; then
  8. DISTRO="CentOS"
  9. elif [ -e /etc/debian_version ]; then
  10. DISTRO=$( lsb_release -is )
  11. else
  12. echo "Your distribution is not supported (yet)"
  13. exit
  14. fi
  15. if [ "$DISTRO" == "Ubuntu" ]; then
  16. apt update
  17. apt install software-properties-common -y
  18. echo .read | add-apt-repository ppa:wireguard/wireguard
  19. apt update
  20. apt install linux-headers-$(uname -r) wireguard resolvconf qrencode -y
  21. elif [ "$DISTRO" == "Debian" ]; then
  22. echo "deb http://deb.debian.org/debian/ unstable main" > /etc/apt/sources.list.d/unstable.list
  23. printf 'Package: *\nPin: release a=unstable\nPin-Priority: 90\n' > /etc/apt/preferences.d/limit-unstable
  24. apt update
  25. apt install linux-headers-$(uname -r) wireguard resolvconf qrencode -y
  26. elif [ "$DISTRO" == "CentOS" ]; then
  27. curl -Lo /etc/yum.repos.d/wireguard.repo https://copr.fedorainfracloud.org/coprs/jdoss/wireguard/repo/epel-7/jdoss-wireguard-epel-7.repo
  28. yum install -y epel-release
  29. yum install -y wireguard-dkms wireguard-tools
  30. yum -y install qrencode iptables-services
  31. systemctl stop firewalld && systemctl disable firewalld
  32. systemctl enable iptables && systemctl start iptables
  33. iptables -F && service iptables save && service iptables restart
  34. fi
  35. mkdir -p /etc/wireguard
  36. cd /etc/wireguard
  37. wg genkey | tee sprivatekey | wg pubkey > spublickey
  38. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  39. chmod 777 -R /etc/wireguard
  40. systemctl enable wg-quick@wg0
  41. }
  42. if [ ! -f '/usr/bin/wg' ]; then
  43. wireguard_install
  44. fi
  45. # WireGuard VPN Multi-user Configuration Script, Support IPV6
  46. #############################################################
  47. let port=$RANDOM/2+9999
  48. mtu=1420
  49. ip_list=(2 5 8 178 186 118 158 198 168 9)
  50. ipv6_range="fd08:620c:4df0:65eb::"
  51. # Get WireGuard Management Command : bash wgmtu
  52. wget -O ~/wgmtu https://raw.githubusercontent.com/hongwenjun/vps_setup/english/wgmtu.sh
  53. # Definition Display Text Color
  54. Green="\033[32m" && Red="\033[31m" && GreenBG="\033[42;37m" && RedBG="\033[41;37m"
  55. Font="\033[0m" && Yellow="\033[0;33m" && SkyBlue="\033[0;36m"
  56. echo_SkyBlue(){
  57. echo -e "${SkyBlue}$1${Font}"
  58. }
  59. echo_Yellow(){
  60. echo -e "${Yellow}$1${Font}"
  61. }
  62. echo_GreenBG(){
  63. echo -e "${GreenBG}$1${Font}"
  64. }
  65. echo_RedBG(){
  66. echo -e "${RedBG}$1${Font}"
  67. }
  68. #############################################################
  69. if [[ $# > 0 ]]; then
  70. num="$1"
  71. if [[ ${num} -ge 100 ]] && [[ ${num} -le 60000 ]]; then
  72. port=$num
  73. fi
  74. fi
  75. host=$(hostname -s)
  76. if [ ! -f '/usr/bin/curl' ]; then
  77. apt update && apt install -y curl
  78. fi
  79. # Auto Get Server IP addr
  80. if [ ! -e '/var/ip_addr' ]; then
  81. echo -n $(curl -4 ip.sb) > /var/ip_addr
  82. fi
  83. serverip=$(cat /var/ip_addr)
  84. #############################################################
  85. # Open ip4/ipv6 firewall forwarding function
  86. sysctl_config() {
  87. sed -i '/net.ipv4.ip_forward/d' /etc/sysctl.conf
  88. sed -i '/net.ipv6.conf.all.forwarding/d' /etc/sysctl.conf
  89. sed -i '/net.ipv6.conf.default.accept_ra/d' /etc/sysctl.conf
  90. echo 1 > /proc/sys/net/ipv4/ip_forward
  91. echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
  92. echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.conf
  93. echo "net.ipv6.conf.default.accept_ra=2" >> /etc/sysctl.conf
  94. sysctl -p >/dev/null 2>&1
  95. }
  96. sysctl_config
  97. # Wg configuration Path: /etc/wireguard
  98. mkdir -p /etc/wireguard
  99. chmod 777 -R /etc/wireguard
  100. cd /etc/wireguard
  101. # Start generating a key pair (public key + private key)
  102. wg genkey | tee sprivatekey | wg pubkey > spublickey
  103. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  104. # Generate a server configuration file
  105. cat <<EOF >wg0.conf
  106. [Interface]
  107. PrivateKey = $(cat sprivatekey)
  108. Address = 10.0.0.1/24, ${ipv6_range}1/64
  109. PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  110. PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
  111. ListenPort = $port
  112. DNS = 8.8.8.8, 2001:4860:4860::8888
  113. MTU = $mtu
  114. [Peer]
  115. PublicKey = $(cat cpublickey)
  116. AllowedIPs = 10.0.0.188/32, ${ipv6_range}188
  117. EOF
  118. # Generate a clean client configuration
  119. cat <<EOF >client.conf
  120. [Interface]
  121. PrivateKey = $(cat cprivatekey)
  122. Address = 10.0.0.188/24, ${ipv6_range}188/64
  123. DNS = 8.8.8.8, 2001:4860:4860::8888
  124. # MTU = $mtu
  125. # PreUp = start .\route\routes-up.bat
  126. # PostDown = start .\route\routes-down.bat
  127. [Peer]
  128. PublicKey = $(cat spublickey)
  129. Endpoint = $serverip:$port
  130. AllowedIPs = 0.0.0.0/0, ::0/0
  131. PersistentKeepalive = 25
  132. EOF
  133. # Add 2-9 multi-user configuration
  134. for i in {2..9}
  135. do
  136. ip=10.0.0.${ip_list[$i]}
  137. ip6=${ipv6_range}${ip_list[$i]}
  138. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  139. cat <<EOF >>wg0.conf
  140. [Peer]
  141. PublicKey = $(cat cpublickey)
  142. AllowedIPs = $ip/32, $ip6
  143. EOF
  144. cat <<EOF >wg_${host}_$i.conf
  145. [Interface]
  146. PrivateKey = $(cat cprivatekey)
  147. Address = $ip/24, $ip6/64
  148. DNS = 8.8.8.8, 2001:4860:4860::8888
  149. [Peer]
  150. PublicKey = $(cat spublickey)
  151. Endpoint = $serverip:$port
  152. AllowedIPs = 0.0.0.0/0, ::0/0
  153. PersistentKeepalive = 25
  154. EOF
  155. cat /etc/wireguard/wg_${host}_$i.conf | qrencode -o wg_${host}_$i.png
  156. done
  157. # If the vps network card is not eth0, modify it to the actual network card
  158. ni=$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)
  159. if [ $ni != "eth0" ]; then
  160. sed -i "s/eth0/${ni}/g" /etc/wireguard/wg0.conf
  161. fi
  162. # restart WG server
  163. wg-quick down wg0
  164. wg-quick up wg0
  165. # WG Operating status &
  166. wg
  167. ls /etc/wireguard/wg_*
  168. next() {
  169. printf "# %-70s\n" "-" | sed 's/\s/-/g'
  170. }
  171. echo
  172. echo_SkyBlue ":: Windows Client configuration, Please copy the conf text."
  173. cat /etc/wireguard/client.conf && next
  174. cat /etc/wireguard/wg_${host}_2.conf && next
  175. cat /etc/wireguard/wg_${host}_3.conf && next
  176. echo_RedBG " One-Step Automated Install WireGuard Script For Debian_9 Ubuntu Centos_7 "
  177. echo_GreenBG " Open Source Project: https://github.com/hongwenjun/vps_setup "
  178. echo_Yellow ":: WireGuard Management Command."
  179. echo_SkyBlue "Usage: ${GreenBG} bash wgmtu ${SkyBlue} [ setup | remove | vps | bench | -U ] "
  180. echo_SkyBlue " [ v2ray | vnstat | log | trace | -h ] "