debian_wg_vpn.sh 5.1 KB

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