ubuntu_wireguard_install.sh 4.6 KB

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