1
0

debian_wg_vpn.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/bin/bash
  2. # 高速新VPN协议WireGuard服务端手动教程
  3. # Windows TunSafe版客户端
  4. # https://tunsafe.com/download
  5. # Debian9 安装 WireGuard 步骤
  6. # Debian 默认往往都没有 linux-headers 内核,而安装使用 WireGuard 必须要
  7. # 更新软件包源
  8. apt update
  9. # 安装和 linux-image 内核版本相对于的 linux-headers 内核
  10. apt install linux-headers-$(uname -r) -y
  11. # Debian9 安装后内核列表
  12. dpkg -l|grep linux-headers
  13. # 安装WireGuard
  14. # 添加 unstable 软件包源,以确保安装版本是最新的
  15. echo "deb http://deb.debian.org/debian/ unstable main" > /etc/apt/sources.list.d/unstable.list
  16. echo -e 'Package: *\nPin: release a=unstable\nPin-Priority: 150' > /etc/apt/preferences.d/limit-unstable
  17. # 更新一下软件包源
  18. apt update
  19. # 开始安装 WireGuard ,至于 resolvconf 我也不清楚这货具体是干嘛的,但是没有安装这个的系统会报错,但是具体会影响哪里使用我也不清楚,为了保险点不出错还是安装吧。一般 Debian9 都自带了。
  20. apt install wireguard resolvconf -y
  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. # 生成客户端配置文件
  31. echo "[Interface]
  32. # 私匙,自动读取上面刚刚生成的密匙内容
  33. PrivateKey = $(cat sprivatekey)
  34. # VPN中本机的内网IP,一般默认即可,除非和你服务器或客户端设备本地网段冲突
  35. Address = 10.0.0.1/24
  36. # 运行 WireGuard 时要执行的 iptables 防火墙规则,用于打开NAT转发之类的。
  37. # 如果你的服务器主网卡名称不是 eth0 ,那么请修改下面防火墙规则中最后的 eth0 为你的主网卡名称。
  38. PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -A FORWARD -o wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  39. # 停止 WireGuard 时要执行的 iptables 防火墙规则,用于关闭NAT转发之类的。
  40. # 如果你的服务器主网卡名称不是 eth0 ,那么请修改下面防火墙规则中最后的 eth0 为你的主网卡名称。
  41. PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -D FORWARD -o wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
  42. # 服务端监听端口,可以自行修改
  43. ListenPort = 9009
  44. # 服务端请求域名解析 DNS
  45. DNS = 8.8.8.8
  46. # 保持默认
  47. MTU = 1420
  48. [Peer]
  49. # 公匙,自动读取上面刚刚生成的密匙内容
  50. PublicKey = $(cat cpublickey)
  51. # VPN内网IP范围,一般默认即可,除非和你服务器或客户端设备本地网段冲突
  52. AllowedIPs = 10.0.0.2/32" > wg0.conf
  53. # 生成客户端配置文件
  54. echo "[Interface]
  55. # 私匙,自动读取上面刚刚生成的密匙内容
  56. PrivateKey = $(cat cprivatekey)
  57. # VPN内网IP范围
  58. Address = 10.0.0.2/24
  59. # 解析域名用的DNS
  60. DNS = 8.8.8.8
  61. # 保持默认
  62. MTU = 1420
  63. [Peer]
  64. # 公匙,自动读取上面刚刚生成的密匙内容
  65. PublicKey = $(cat spublickey)
  66. # 服务器地址和端口,下面的 X.X.X.X 记得更换为你的服务器公网IP,端口根据服务端配置时的监听端口填写
  67. Endpoint = sky.srgb.xyz:9009
  68. # 转发流量的IP范围,下面这个代表所有流量都走VPN
  69. AllowedIPs = 0.0.0.0/0, ::0/0
  70. # 保持连接(具体我也不清楚)
  71. PersistentKeepalive = 25" > client.conf
  72. # 赋予配置文件夹权限
  73. chmod 777 -R /etc/wireguard
  74. # 打开防火墙转发功能
  75. echo 1 > /proc/sys/net/ipv4/ip_forward
  76. echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
  77. sysctl -p
  78. # 启动WireGuard
  79. wg-quick up wg0
  80. # 设置开机启动
  81. systemctl enable wg-quick@wg0
  82. # 查询WireGuard状态
  83. wg
  84. # 显示配置文件,可以修改里面的实际IP
  85. cat /etc/wireguard/client.conf