centos7_wireguard_install.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/bin/bash
  2. # https://github.com/atrandys/wireguard/edit/master/wireguard_install.sh
  3. #判断系统
  4. if [ ! -e '/etc/redhat-release' ]; then
  5. echo "仅支持centos7"
  6. exit
  7. fi
  8. if [ -n "$(grep ' 6\.' /etc/redhat-release)" ] ;then
  9. echo "仅支持centos7"
  10. exit
  11. fi
  12. #更新内核
  13. update_kernel(){
  14. yum -y install epel-release curl
  15. sed -i "0,/enabled=0/s//enabled=1/" /etc/yum.repos.d/epel.repo
  16. yum remove -y kernel-devel
  17. rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
  18. rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
  19. yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
  20. yum -y --enablerepo=elrepo-kernel install kernel-ml
  21. sed -i "s/GRUB_DEFAULT=saved/GRUB_DEFAULT=0/" /etc/default/grub
  22. grub2-mkconfig -o /boot/grub2/grub.cfg
  23. wget https://elrepo.org/linux/kernel/el7/x86_64/RPMS/kernel-ml-devel-4.19.1-1.el7.elrepo.x86_64.rpm
  24. rpm -ivh kernel-ml-devel-4.19.1-1.el7.elrepo.x86_64.rpm
  25. yum -y --enablerepo=elrepo-kernel install kernel-ml-devel
  26. read -p "需要重启VPS,再次执行脚本选择安装wireguard,是否现在重启 ? [Y/n] :" yn
  27. [ -z "${yn}" ] && yn="y"
  28. if [[ $yn == [Yy] ]]; then
  29. echo -e "VPS 重启中..."
  30. reboot
  31. fi
  32. }
  33. #生成随机端口
  34. rand(){
  35. min=$1
  36. max=$(($2-$min+1))
  37. num=$(cat /dev/urandom | head -n 10 | cksum | awk -F ' ' '{print $1}')
  38. echo $(($num%$max+$min))
  39. }
  40. wireguard_update(){
  41. yum update -y wireguard-dkms wireguard-tools
  42. echo "更新完成"
  43. }
  44. wireguard_remove(){
  45. wg-quick down wg0
  46. yum remove -y wireguard-dkms wireguard-tools
  47. rm -rf /etc/wireguard/
  48. echo "卸载完成"
  49. }
  50. config_client(){
  51. cat > /etc/wireguard/client.conf <<-EOF
  52. [Interface]
  53. PrivateKey = $c1
  54. Address = 10.0.0.2/24
  55. DNS = 8.8.8.8
  56. MTU = 1420
  57. [Peer]
  58. PublicKey = $s2
  59. Endpoint = $serverip:$port
  60. AllowedIPs = 0.0.0.0/0, ::0/0
  61. PersistentKeepalive = 25
  62. EOF
  63. }
  64. #centos7安装wireguard
  65. wireguard_install(){
  66. curl -Lo /etc/yum.repos.d/wireguard.repo https://copr.fedorainfracloud.org/coprs/jdoss/wireguard/repo/epel-7/jdoss-wireguard-epel-7.repo
  67. yum install -y dkms gcc-c++ gcc-gfortran glibc-headers glibc-devel libquadmath-devel libtool systemtap systemtap-devel
  68. yum -y install wireguard-dkms wireguard-tools
  69. yum -y install qrencode
  70. mkdir /etc/wireguard
  71. cd /etc/wireguard
  72. wg genkey | tee sprivatekey | wg pubkey > spublickey
  73. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  74. s1=$(cat sprivatekey)
  75. s2=$(cat spublickey)
  76. c1=$(cat cprivatekey)
  77. c2=$(cat cpublickey)
  78. serverip=$(curl ipv4.icanhazip.com)
  79. port=$(rand 10000 60000)
  80. eth=$(ls /sys/class/net | awk '/^e/{print}')
  81. chmod 777 -R /etc/wireguard
  82. systemctl stop firewalld
  83. systemctl disable firewalld
  84. yum install -y iptables-services
  85. systemctl enable iptables
  86. systemctl start iptables
  87. iptables -P INPUT ACCEPT
  88. iptables -P OUTPUT ACCEPT
  89. iptables -P FORWARD ACCEPT
  90. iptables -F
  91. service iptables save
  92. service iptables restart
  93. echo 1 > /proc/sys/net/ipv4/ip_forward
  94. echo "net.ipv4.ip_forward = 1" > /etc/sysctl.conf
  95. cat > /etc/wireguard/wg0.conf <<-EOF
  96. [Interface]
  97. PrivateKey = $s1
  98. Address = 10.0.0.1/24
  99. PostUp = echo 1 > /proc/sys/net/ipv4/ip_forward; iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o $eth -j MASQUERADE
  100. PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o $eth -j MASQUERADE
  101. ListenPort = $port
  102. DNS = 8.8.8.8
  103. MTU = 1420
  104. [Peer]
  105. PublicKey = $c2
  106. AllowedIPs = 10.0.0.2/32
  107. EOF
  108. config_client
  109. wg-quick up wg0
  110. systemctl enable wg-quick@wg0
  111. content=$(cat /etc/wireguard/client.conf)
  112. echo "电脑端请下载client.conf,手机端可直接使用软件扫码"
  113. echo "${content}" | qrencode -o - -t UTF8
  114. }
  115. #开始菜单
  116. start_menu(){
  117. clear
  118. echo "========================="
  119. echo " 介绍:适用于CentOS7"
  120. echo " 作者:atrandys"
  121. echo " 网站:www.atrandys.com"
  122. echo " Youtube:atrandys"
  123. echo "========================="
  124. echo "1. 升级系统内核"
  125. echo "2. 安装wireguard"
  126. echo "3. 升级wireguard"
  127. echo "4. 卸载wireguard"
  128. echo "5. 显示客户端二维码"
  129. echo "0. 退出脚本"
  130. echo
  131. read -p "请输入数字:" num
  132. case "$num" in
  133. 1)
  134. update_kernel
  135. ;;
  136. 2)
  137. wireguard_install
  138. ;;
  139. 3)
  140. wireguard_update
  141. ;;
  142. 4)
  143. wireguard_remove
  144. ;;
  145. 5)
  146. content=$(cat /etc/wireguard/client.conf)
  147. echo "${content}" | qrencode -o - -t UTF8
  148. ;;
  149. 0)
  150. exit 1
  151. ;;
  152. *)
  153. clear
  154. echo "请输入正确数字"
  155. sleep 5s
  156. start_menu
  157. ;;
  158. esac
  159. }
  160. start_menu