wg5clients.sh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/bash
  2. # WireGuard VPN多用户服务端 自动配置脚本
  3. # 本脚本(WireGuard 多用户配置)一键安装短网址
  4. # wget -qO- https://git.io/fpnQt | bash
  5. # 本脚本适合已经安装 WireGuard VPN 的vps
  6. # 如果你的vps没有安装 WireGuard ,可以用下行命令先安装
  7. # 一键安装wireguard 脚本 debian 9
  8. # wget -qO- git.io/fptwc | bash
  9. # vultr 服务商的主机默认网卡是 ens3,脚本执行完成,还要替换网卡名
  10. # sed -i "s/eth0/ens3/g" /etc/wireguard/wg0.conf
  11. #############################################################
  12. # 定义修改端口号,适合已经安装WireGuard而不想改端口
  13. port=9009
  14. # 获得服务器ip,自动获取
  15. serverip=$(curl -4 icanhazip.com)
  16. #############################################################
  17. # 转到wg配置文件目录
  18. cd /etc/wireguard
  19. # 然后开始生成 密匙对(公匙+私匙)。
  20. wg genkey | tee sprivatekey | wg pubkey > spublickey
  21. wg genkey | tee cprivatekey1 | wg pubkey > cpublickey1
  22. wg genkey | tee cprivatekey2 | wg pubkey > cpublickey2
  23. wg genkey | tee cprivatekey3 | wg pubkey > cpublickey3
  24. wg genkey | tee cprivatekey4 | wg pubkey > cpublickey4
  25. wg genkey | tee cprivatekey5 | wg pubkey > cpublickey5
  26. # 生成服务端 多用户配置文件
  27. cat <<EOF >wg0.conf
  28. [Interface]
  29. PrivateKey = $(cat sprivatekey)
  30. Address = 10.0.0.1/24
  31. PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  32. PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
  33. ListenPort = $port
  34. DNS = 8.8.8.8
  35. MTU = 1300
  36. [Peer]
  37. PublicKey = $(cat cpublickey1)
  38. AllowedIPs = 10.0.0.2/32
  39. [Peer]
  40. PublicKey = $(cat cpublickey2)
  41. AllowedIPs = 10.0.0.8/32
  42. [Peer]
  43. PublicKey = $(cat cpublickey3)
  44. AllowedIPs = 10.0.0.18/32
  45. [Peer]
  46. PublicKey = $(cat cpublickey4)
  47. AllowedIPs = 10.0.0.88/32
  48. [Peer]
  49. PublicKey = $(cat cpublickey5)
  50. AllowedIPs = 10.0.0.188/32
  51. EOF
  52. # 生成简洁的客户端配置
  53. cat <<EOF >client.conf
  54. [Interface]
  55. PrivateKey = $(cat cprivatekey1)
  56. Address = 10.0.0.2/24
  57. DNS = 8.8.8.8
  58. MTU = 1300
  59. [Peer]
  60. PublicKey = $(cat spublickey)
  61. Endpoint = $serverip:$port
  62. AllowedIPs = 0.0.0.0/0, ::0/0
  63. PersistentKeepalive = 25
  64. EOF
  65. cat <<EOF >client_2.conf
  66. [Interface]
  67. PrivateKey = $(cat cprivatekey2)
  68. Address = 10.0.0.8/24
  69. DNS = 8.8.8.8
  70. MTU = 1300
  71. [Peer]
  72. PublicKey = $(cat spublickey)
  73. Endpoint = $serverip:$port
  74. AllowedIPs = 0.0.0.0/0, ::0/0
  75. PersistentKeepalive = 25
  76. EOF
  77. cat <<EOF >client_3.conf
  78. [Interface]
  79. PrivateKey = $(cat cprivatekey3)
  80. Address = 10.0.0.18/24
  81. DNS = 8.8.8.8
  82. MTU = 1300
  83. [Peer]
  84. PublicKey = $(cat spublickey)
  85. Endpoint = $serverip:$port
  86. AllowedIPs = 0.0.0.0/0, ::0/0
  87. PersistentKeepalive = 25
  88. EOF
  89. cat <<EOF >client_4.conf
  90. [Interface]
  91. PrivateKey = $(cat cprivatekey4)
  92. Address = 10.0.0.88/24
  93. DNS = 8.8.8.8
  94. MTU = 1300
  95. [Peer]
  96. PublicKey = $(cat spublickey)
  97. Endpoint = $serverip:$port
  98. AllowedIPs = 0.0.0.0/0, ::0/0
  99. PersistentKeepalive = 25
  100. EOF
  101. cat <<EOF >client_5.conf
  102. [Interface]
  103. PrivateKey = $(cat cprivatekey5)
  104. Address = 10.0.0.188/24
  105. DNS = 8.8.8.8
  106. MTU = 1300
  107. [Peer]
  108. PublicKey = $(cat spublickey)
  109. Endpoint = $serverip:$port
  110. AllowedIPs = 0.0.0.0/0, ::0/0
  111. PersistentKeepalive = 25
  112. EOF
  113. # 重启wg服务器
  114. wg-quick down wg0
  115. wg-quick up wg0
  116. wg
  117. # 打包客户端 配置
  118. tar cvf wg5clients.tar client*
  119. echo '正在上传配置文件到共享服务器,请稍等...... '
  120. curl --upload-file ./wg5clients.tar https://transfer.sh/wg5clients.tar
  121. echo ' <----- 按提示的网址下载客户端包,保留2星期'