debian_wg_vpn.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/bin/bash
  2. # https://www.wireguard.com/install/
  3. # 高速新VPN协议WireGuard服务端一键脚本
  4. # GCP香港 和 Vutrl 搬瓦工 测试可用,已经添加自动判断网卡名
  5. # OpenVZ PVE 不能用
  6. # Windows TunSafe版客户端
  7. # https://tunsafe.com/download
  8. # Debian9 安装 WireGuard 步骤
  9. debian_wireguard_install(){
  10. # Debian 默认往往都没有 linux-headers 内核,而安装使用 WireGuard 必须要
  11. # 更新软件包源
  12. apt update
  13. # 安装和 linux-image 内核版本相对于的 linux-headers 内核
  14. apt install linux-headers-$(uname -r) -y
  15. # Debian9 安装后内核列表
  16. dpkg -l|grep linux-headers
  17. # 安装WireGuard
  18. # 添加 unstable 软件包源,以确保安装版本是最新的
  19. echo "deb http://deb.debian.org/debian/ unstable main" > /etc/apt/sources.list.d/unstable.list
  20. printf 'Package: *\nPin: release a=unstable\nPin-Priority: 90\n' > /etc/apt/preferences.d/limit-unstable
  21. # 更新一下软件包源
  22. apt update
  23. # 开始安装 WireGuard ,和辅助库 resolvconf
  24. apt install wireguard resolvconf -y
  25. # 验证是否安装成功
  26. modprobe wireguard && lsmod | grep wireguard
  27. # 配置步骤 WireGuard服务端
  28. # 首先进入配置文件目录
  29. mkdir -p /etc/wireguard
  30. cd /etc/wireguard
  31. # 然后开始生成 密匙对(公匙+私匙)。
  32. wg genkey | tee sprivatekey | wg pubkey > spublickey
  33. wg genkey | tee cprivatekey | wg pubkey > cpublickey
  34. # 获得服务器ip
  35. serverip=$(curl -4 ip.sb)
  36. # 生成服务端配置文件
  37. echo "[Interface]
  38. # 私匙,自动读取上面刚刚生成的密匙内容
  39. PrivateKey = $(cat sprivatekey)
  40. # VPN中本机的内网IP,一般默认即可,除非和你服务器或客户端设备本地网段冲突
  41. Address = 10.0.0.1/24
  42. # 运行 WireGuard 时要执行的 iptables 防火墙规则,用于打开NAT转发之类的。
  43. # 如果你的服务器主网卡名称不是 eth0 ,那么请修改下面防火墙规则中最后的 eth0 为你的主网卡名称。
  44. PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  45. # 停止 WireGuard 时要执行的 iptables 防火墙规则,用于关闭NAT转发之类的。
  46. # 如果你的服务器主网卡名称不是 eth0 ,那么请修改下面防火墙规则中最后的 eth0 为你的主网卡名称。
  47. PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
  48. # 服务端监听端口,可以自行修改
  49. ListenPort = 9999
  50. # 服务端请求域名解析 DNS
  51. DNS = 8.8.8.8
  52. # 保持默认
  53. MTU = 1420
  54. [Peer]
  55. # 公匙,自动读取上面刚刚生成的密匙内容
  56. PublicKey = $(cat cpublickey)
  57. # VPN内网IP范围,一般默认即可,除非和你服务器或客户端设备本地网段冲突
  58. AllowedIPs = 10.0.0.2/32" > wg0.conf
  59. # 生成客户端配置文件
  60. echo "[Interface]
  61. # 私匙,自动读取上面刚刚生成的密匙内容
  62. PrivateKey = $(cat cprivatekey)
  63. # VPN内网IP范围
  64. Address = 10.0.0.2/24
  65. # 解析域名用的DNS
  66. DNS = 8.8.8.8
  67. # 保持默认
  68. MTU = 1300
  69. # Wireguard客户端配置文件加入PreUp,Postdown命令调用批处理文件
  70. PreUp = start .\route\routes-up.bat
  71. PostDown = start .\route\routes-down.bat
  72. #### 正常使用Tunsafe点击connect就会调用routes-up.bat将国内IP写进系统路由表,断开disconnect则会调用routes-down.bat删除路由表。
  73. #### 连接成功后可上 http://ip111.cn/ 测试自己的IP。
  74. [Peer]
  75. # 公匙,自动读取上面刚刚生成的密匙内容
  76. PublicKey = $(cat spublickey)
  77. # 服务器地址和端口,下面的 X.X.X.X 记得更换为你的服务器公网IP,端口根据服务端配置时的监听端口填写
  78. Endpoint = $serverip:9009
  79. # 转发流量的IP范围,下面这个代表所有流量都走VPN
  80. AllowedIPs = 0.0.0.0/0, ::0/0
  81. # 保持连接,如果客户端或服务端是 NAT 网络(比如国内大多数家庭宽带没有公网IP,都是NAT),
  82. # 那么就需要添加这个参数定时链接服务端(单位:秒),如果你的服务器和你本地都不是 NAT 网络,
  83. # 那么建议不使用该参数(设置为0,或客户端配置文件中删除这行)
  84. PersistentKeepalive = 25"|sed '/^#/d;/^\s*$/d' > client.conf
  85. # 赋予配置文件夹权限
  86. chmod 777 -R /etc/wireguard
  87. sysctl_config() {
  88. sed -i '/net.core.default_qdisc/d' /etc/sysctl.conf
  89. sed -i '/net.all.tcp_congestion_control/d' /etc/sysctl.conf
  90. echo "net.core.default_qdisc = fq" >> /etc/sysctl.conf
  91. echo "net.all.tcp_congestion_control = bbr" >> /etc/sysctl.conf
  92. sysctl -p >/dev/null 2>&1
  93. }
  94. # 开启 BBR
  95. sysctl_config
  96. lsmod | grep bbr
  97. # 打开防火墙转发功能
  98. echo 1 > /proc/sys/net/ipv4/ip_forward
  99. echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
  100. sysctl -p
  101. # 启动WireGuard
  102. wg-quick up wg0
  103. # 设置开机启动
  104. systemctl enable wg-quick@wg0
  105. # 查询WireGuard状态
  106. wg
  107. # 以上配置文本只做参考文档使用,实际生成 WireGuard 多用户配置
  108. # 一键 WireGuard 多用户配置共享脚本
  109. wget -qO- https://git.io/fpnQt | bash
  110. }
  111. # ======= 容错:检查系统,调用 Ubuntu Centos 系统对应安装脚本 ========
  112. check_sys(){
  113. if [[ -f /etc/redhat-release ]]; then
  114. release="centos"
  115. elif cat /etc/issue | grep -q -E -i "debian"; then
  116. release="debian"
  117. elif cat /etc/issue | grep -q -E -i "ubuntu"; then
  118. release="ubuntu"
  119. elif cat /etc/issue | grep -q -E -i "centos|red hat|redhat"; then
  120. release="centos"
  121. elif cat /proc/version | grep -q -E -i "debian"; then
  122. release="debian"
  123. elif cat /proc/version | grep -q -E -i "ubuntu"; then
  124. release="ubuntu"
  125. elif cat /proc/version | grep -q -E -i "centos|red hat|redhat"; then
  126. release="centos"
  127. fi
  128. bit=`uname -m`
  129. }
  130. wireguard_config(){
  131. if [[ ${release} == "centos" ]]; then
  132. yum install -y wget vim curl
  133. # CentOS 7 一键脚本安装WireGuard (官方脚本自动升级内核)
  134. wget -qO- git.io/fhnhS | bash
  135. fi
  136. if [[ ${release} == "ubuntu" ]]; then
  137. # 一键安装wireguard 脚本 Ubuntu (源:逗比网安装笔记)
  138. wget -qO- git.io/fpcnL | bash
  139. fi
  140. if [[ ${release} == "debian" ]]; then
  141. debian_wireguard_install
  142. fi
  143. }
  144. check_sys && wireguard_config