WireGuard一键安装shell脚本

WireGuard是一种点对点VPN,可以用不同的方式使用。这里我们指的是VPN:客户端将通过加密隧道将其所有流量转发到服务器。服务器将对客户端的流量应用NAT,这样看起来就像客户端正在使用服务器的IP浏览web。

该脚本同时支持IPv4和IPv6。

  1. #!/bin/bash
  2. # Secure WireGuard server installer for Debian, Ubuntu, CentOS, Fedora and Arch Linux
  3. # https://github.com/angristan/wireguard-install
  4. function isRoot() {
  5.     if [ "${EUID}" -ne 0 ]; then
  6.         echo "You need to run this script as root"
  7.         exit 1
  8.     fi
  9. }
  10. function checkVirt() {
  11.     if [ "$(systemd-detect-virt)" == "openvz" ]; then
  12.         echo "OpenVZ is not supported"
  13.         exit 1
  14.     fi
  15.     if [ "$(systemd-detect-virt)" == "lxc" ]; then
  16.         echo "LXC is not supported (yet)."
  17.         echo "WireGuard can technically run in an LXC container,"
  18.         echo "but the kernel module has to be installed on the host,"
  19.         echo "the container has to be run with some specific parameters"
  20.         echo "and only the tools need to be installed in the container."
  21.         exit 1
  22.     fi
  23. }
  24. function checkOS() {
  25.     # Check OS version
  26.     if [[ -e /etc/debian_version ]]; then
  27.         source /etc/os-release
  28.         OS="${ID}" # debian or ubuntu
  29.         if [[ ${ID} == "debian" || ${ID} == "raspbian" ]]; then
  30.             if [[ ${VERSION_ID} -ne 10 ]]; then
  31.                 echo "Your version of Debian (${VERSION_ID}) is not supported. Please use Debian 10 Buster"
  32.                 exit 1
  33.             fi
  34.         fi
  35.     elif [[ -e /etc/fedora-release ]]; then
  36.         source /etc/os-release
  37.         OS="${ID}"
  38.     elif [[ -e /etc/centos-release ]]; then
  39.         source /etc/os-release
  40.         OS=centos
  41.     elif [[ -e /etc/arch-release ]]; then
  42.         OS=arch
  43.     else
  44.         echo "Looks like you aren't running this installer on a Debian, Ubuntu, Fedora, CentOS or Arch Linux system"
  45.         exit 1
  46.     fi
  47. }
  48. function initialCheck() {
  49.     isRoot
  50.     checkVirt
  51.     checkOS
  52. }
  53. function installQuestions() {
  54.     echo "Welcome to the WireGuard installer!"
  55.     echo "The git repository is available at: https://github.com/angristan/wireguard-install"
  56.     echo ""
  57.     echo "I need to ask you a few questions before starting the setup."
  58.     echo "You can leave the default options and just press enter if you are ok with them."
  59.     echo ""
  60.     # Detect public IPv4 or IPv6 address and pre-fill for the user
  61.     SERVER_PUB_IP=$(ip -4 addr | sed -ne 's|^.* inet \([^/]*\)/.* scope global.*$|\1|p' | head -1)
  62.     if [[ -z ${SERVER_PUB_IP} ]]; then
  63.         # Detect public IPv6 address
  64.         SERVER_PUB_IP=$(ip -6 addr | sed -ne 's|^.* inet6 \([^/]*\)/.* scope global.*$|\1|p' | head -1)
  65.     fi
  66.     read -rp "IPv4 or IPv6 public address: " -e -i "${SERVER_PUB_IP}" SERVER_PUB_IP
  67.     # Detect public interface and pre-fill for the user
  68.     SERVER_NIC="$(ip -4 route ls | grep default | grep -Po '(?<=dev )(\S+)' | head -1)"
  69.     until [[ ${SERVER_PUB_NIC} =~ ^[a-zA-Z0-9_]+$ ]]; do
  70.         read -rp "Public interface: " -e -i "${SERVER_NIC}" SERVER_PUB_NIC
  71.     done
  72.     until [[ ${SERVER_WG_NIC} =~ ^[a-zA-Z0-9_]+$ ]]; do
  73.         read -rp "WireGuard interface name: " -e -i wg0 SERVER_WG_NIC
  74.     done
  75.     until [[ ${SERVER_WG_IPV4} =~ ^([0-9]{1,3}\.){3} ]]; do
  76.         read -rp "Server's WireGuard IPv4: " -e -i 10.66.66.1 SERVER_WG_IPV4
  77.     done
  78.     until [[ ${SERVER_WG_IPV6} =~ ^([a-f0-9]{1,4}:){3,4}: ]]; do
  79.         read -rp "Server's WireGuard IPv6: " -e -i fd42:42:42::1 SERVER_WG_IPV6
  80.     done
  81.     # Generate random number within private ports range
  82.     RANDOM_PORT=$(shuf -i49152-65535 -n1)
  83.     until [[ ${SERVER_PORT} =~ ^[0-9]+$ ]] && [ "${SERVER_PORT}" -ge 1 ] && [ "${SERVER_PORT}" -le 65535 ]; do
  84.         read -rp "Server's WireGuard port [1-65535]: " -e -i "${RANDOM_PORT}" SERVER_PORT
  85.     done
  86.     # Adguard DNS by default
  87.     until [[ ${CLIENT_DNS_1} =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]; do
  88.         read -rp "First DNS resolver to use for the clients: " -e -i 176.103.130.130 CLIENT_DNS_1
  89.     done
  90.     until [[ ${CLIENT_DNS_2} =~ ^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ ]]; do
  91.         read -rp "Second DNS resolver to use for the clients (optional): " -e -i 176.103.130.131 CLIENT_DNS_2
  92.         if [[ ${CLIENT_DNS_2} == "" ]]; then
  93.             CLIENT_DNS_2="${CLIENT_DNS_1}"
  94.         fi
  95.     done
  96.     echo ""
  97.     echo "Okay, that was all I needed. We are ready to setup your WireGuard server now."
  98.     echo "You will be able to generate a client at the end of the installation."
  99.     read -n1 -r -p "Press any key to continue..."
  100. }
  101. function installWireGuard() {
  102.     # Run setup questions first
  103.     installQuestions
  104.     # Install WireGuard tools and module
  105.     if [[ ${OS} == 'ubuntu' ]]; then
  106.         apt-get update
  107.         apt-get install -y wireguard iptables resolvconf qrencode
  108.     elif [[ ${OS} == 'debian' ]]; then
  109.         if ! grep -rqs "^deb .* buster-backports" /etc/apt/; then
  110.             echo "deb http://deb.debian.org/debian buster-backports main" >/etc/apt/sources.list.d/backports.list
  111.             apt-get update
  112.         fi
  113.         apt update
  114.         apt-get install -y iptables resolvconf qrencode
  115.         apt-get install -y -t buster-backports wireguard
  116.     elif [[ ${OS} == 'fedora' ]]; then
  117.         if [[ ${VERSION_ID} -lt 32 ]]; then
  118.             dnf install -y dnf-plugins-core
  119.             dnf copr enable -y jdoss/wireguard
  120.             dnf install -y wireguard-dkms
  121.         fi
  122.         dnf install -y wireguard-tools iptables qrencode
  123.     elif [[ ${OS} == 'centos' ]]; then
  124.         yum -y install epel-release elrepo-release
  125.         if [[ ${VERSION_ID} -eq 7 ]]; then
  126.             yum -y install yum-plugin-elrepo
  127.         fi
  128.         yum -y install kmod-wireguard wireguard-tools iptables qrencode
  129.     elif [[ ${OS} == 'arch' ]]; then
  130.         # Check if current running kernel is LTS
  131.         ARCH_KERNEL_RELEASE=$(uname -r)
  132.         if [[ ${ARCH_KERNEL_RELEASE} == *lts* ]]; then
  133.             pacman -S --noconfirm linux-lts-headers
  134.         else
  135.             pacman -S --noconfirm linux-headers
  136.         fi
  137.         pacman -S --noconfirm wireguard-tools iptables qrencode
  138.     fi
  139.     # Make sure the directory exists (this does not seem the be the case on fedora)
  140.     mkdir /etc/wireguard >/dev/null 2>&1
  141.     chmod 600 -R /etc/wireguard/
  142.     SERVER_PRIV_KEY=$(wg genkey)
  143.     SERVER_PUB_KEY=$(echo "${SERVER_PRIV_KEY}" | wg pubkey)
  144.     # Save WireGuard settings
  145.     echo "SERVER_PUB_IP=${SERVER_PUB_IP}
  146. SERVER_PUB_NIC=${SERVER_PUB_NIC}
  147. SERVER_WG_NIC=${SERVER_WG_NIC}
  148. SERVER_WG_IPV4=${SERVER_WG_IPV4}
  149. SERVER_WG_IPV6=${SERVER_WG_IPV6}
  150. SERVER_PORT=${SERVER_PORT}
  151. SERVER_PRIV_KEY=${SERVER_PRIV_KEY}
  152. SERVER_PUB_KEY=${SERVER_PUB_KEY}
  153. CLIENT_DNS_1=${CLIENT_DNS_1}
  154. CLIENT_DNS_2=${CLIENT_DNS_2}" >/etc/wireguard/params
  155.     # Add server interface
  156.     echo "[Interface]
  157. Address = ${SERVER_WG_IPV4}/24,${SERVER_WG_IPV6}/64
  158. ListenPort = ${SERVER_PORT}
  159. PrivateKey = ${SERVER_PRIV_KEY}" >"/etc/wireguard/${SERVER_WG_NIC}.conf"
  160.     if pgrep firewalld; then
  161.         FIREWALLD_IPV4_ADDRESS=$(echo "${SERVER_WG_IPV4}" | cut -d"." -f1-3)".0"
  162.         FIREWALLD_IPV6_ADDRESS=$(echo "${SERVER_WG_IPV6}" | sed 's/:[^:]*$/:0/')
  163.         echo "PostUp = firewall-cmd --add-port ${SERVER_PORT}/udp && firewall-cmd --add-rich-rule='rule family=ipv4 source address=${FIREWALLD_IPV4_ADDRESS}/24 masquerade' && firewall-cmd --add-rich-rule='rule family=ipv6 source address=${FIREWALLD_IPV6_ADDRESS}/24 masquerade'
  164. PostDown = firewall-cmd --remove-port ${SERVER_PORT}/udp && firewall-cmd --remove-rich-rule='rule family=ipv4 source address=${FIREWALLD_IPV4_ADDRESS}/24 masquerade' && firewall-cmd --remove-rich-rule='rule family=ipv6 source address=${FIREWALLD_IPV6_ADDRESS}/24 masquerade'" >>"/etc/wireguard/${SERVER_WG_NIC}.conf"
  165.     else
  166.         echo "PostUp = iptables -A FORWARD -i ${SERVER_PUB_NIC} -o ${SERVER_WG_NIC} -j ACCEPT; iptables -A FORWARD -i ${SERVER_WG_NIC} -j ACCEPT; iptables -t nat -A POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE; ip6tables -A FORWARD -i ${SERVER_WG_NIC} -j ACCEPT; ip6tables -t nat -A POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE
  167. PostDown = iptables -D FORWARD -i ${SERVER_PUB_NIC} -o ${SERVER_WG_NIC} -j ACCEPT; iptables -D FORWARD -i ${SERVER_WG_NIC} -j ACCEPT; iptables -t nat -D POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE; ip6tables -D FORWARD -i ${SERVER_WG_NIC} -j ACCEPT; ip6tables -t nat -D POSTROUTING -o ${SERVER_PUB_NIC} -j MASQUERADE" >>"/etc/wireguard/${SERVER_WG_NIC}.conf"
  168.     fi
  169.     # Enable routing on the server
  170.     echo "net.ipv4.ip_forward = 1
  171. net.ipv6.conf.all.forwarding = 1" >/etc/sysctl.d/wg.conf
  172.     sysctl --system
  173.     systemctl start "wg-quick@${SERVER_WG_NIC}"
  174.     systemctl enable "wg-quick@${SERVER_WG_NIC}"
  175.     # Check if WireGuard is running
  176.     systemctl is-active --quiet "wg-quick@${SERVER_WG_NIC}"
  177.     WG_RUNNING=$?
  178.     # WireGuard might not work if we updated the kernel. Tell the user to reboot
  179.     if [[ ${WG_RUNNING} -ne 0 ]]; then
  180.         echo -e "\nWARNING: WireGuard does not seem to be running."
  181.         echo "You can check if WireGuard is running with: systemctl status wg-quick@${SERVER_WG_NIC}"
  182.         echo "If you get something like \"Cannot find device ${SERVER_WG_NIC}\", please reboot!"
  183.     fi
  184.     newClient
  185.     echo "If you want to add more clients, you simply need to run this script another time!"
  186. }
  187. function newClient() {
  188.     ENDPOINT="${SERVER_PUB_IP}:${SERVER_PORT}"
  189.     echo ""
  190.     echo "Tell me a name for the client."
  191.     echo "The name must consist of alphanumeric character. It may also include an underscore or a dash."
  192.     until [[ ${CLIENT_NAME} =~ ^[a-zA-Z0-9_-]+$ && ${CLIENT_EXISTS} == '0' ]]; do
  193.         read -rp "Client name: " -e CLIENT_NAME
  194.         CLIENT_EXISTS=$(grep -c -E "^### Client ${CLIENT_NAME}\$" "/etc/wireguard/${SERVER_WG_NIC}.conf")
  195.         if [[ ${CLIENT_EXISTS} == '1' ]]; then
  196.             echo ""
  197.             echo "A client with the specified name was already created, please choose another name."
  198.             echo ""
  199.         fi
  200.     done
  201.     for DOT_IP in {2..254}; do
  202.         DOT_EXISTS=$(grep -c "${SERVER_WG_IPV4::-1}${DOT_IP}" "/etc/wireguard/${SERVER_WG_NIC}.conf")
  203.         if [[ ${DOT_EXISTS} == '0' ]]; then
  204.             break
  205.         fi
  206.     done
  207.     if [[ ${DOT_EXISTS} == '1' ]]; then
  208.         echo ""
  209.         echo "The subnet configured supports only 253 clients."
  210.         exit 1
  211.     fi
  212.     until [[ ${IPV4_EXISTS} == '0' ]]; do
  213.         read -rp "Client's WireGuard IPv4: ${SERVER_WG_IPV4::-1}" -e -i "${DOT_IP}" DOT_IP
  214.         CLIENT_WG_IPV4="${SERVER_WG_IPV4::-1}${DOT_IP}"
  215.         IPV4_EXISTS=$(grep -c "$CLIENT_WG_IPV4" "/etc/wireguard/${SERVER_WG_NIC}.conf")
  216.         if [[ ${IPV4_EXISTS} == '1' ]]; then
  217.             echo ""
  218.             echo "A client with the specified IPv4 was already created, please choose another IPv4."
  219.             echo ""
  220.         fi
  221.     done
  222.     until [[ ${IPV6_EXISTS} == '0' ]]; do
  223.         read -rp "Client's WireGuard IPv6: ${SERVER_WG_IPV6::-1}" -e -i "${DOT_IP}" DOT_IP
  224.         CLIENT_WG_IPV6="${SERVER_WG_IPV6::-1}${DOT_IP}"
  225.         IPV6_EXISTS=$(grep -c "${CLIENT_WG_IPV6}" "/etc/wireguard/${SERVER_WG_NIC}.conf")
  226.         if [[ ${IPV6_EXISTS} == '1' ]]; then
  227.             echo ""
  228.             echo "A client with the specified IPv6 was already created, please choose another IPv6."
  229.             echo ""
  230.         fi
  231.     done
  232.     # Generate key pair for the client
  233.     CLIENT_PRIV_KEY=$(wg genkey)
  234.     CLIENT_PUB_KEY=$(echo "${CLIENT_PRIV_KEY}" | wg pubkey)
  235.     CLIENT_PRE_SHARED_KEY=$(wg genpsk)
  236.     # Home directory of the user, where the client configuration will be written
  237.     if [ -e "/home/${CLIENT_NAME}" ]; then # if $1 is a user name
  238.         HOME_DIR="/home/${CLIENT_NAME}"
  239.     elif [ "${SUDO_USER}" ]; then # if not, use SUDO_USER
  240.         HOME_DIR="/home/${SUDO_USER}"
  241.     else # if not SUDO_USER, use /root
  242.         HOME_DIR="/root"
  243.     fi
  244.     # Create client file and add the server as a peer
  245.     echo "[Interface]
  246. PrivateKey = ${CLIENT_PRIV_KEY}
  247. Address = ${CLIENT_WG_IPV4}/32,${CLIENT_WG_IPV6}/128
  248. DNS = ${CLIENT_DNS_1},${CLIENT_DNS_2}
  249. [Peer]
  250. PublicKey = ${SERVER_PUB_KEY}
  251. PresharedKey = ${CLIENT_PRE_SHARED_KEY}
  252. Endpoint = ${ENDPOINT}
  253. AllowedIPs = 0.0.0.0/0,::/0" >>"${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf"
  254.     # Add the client as a peer to the server
  255.     echo -e "\n### Client ${CLIENT_NAME}
  256. [Peer]
  257. PublicKey = ${CLIENT_PUB_KEY}
  258. PresharedKey = ${CLIENT_PRE_SHARED_KEY}
  259. AllowedIPs = ${CLIENT_WG_IPV4}/32,${CLIENT_WG_IPV6}/128" >>"/etc/wireguard/${SERVER_WG_NIC}.conf"
  260.     systemctl restart "wg-quick@${SERVER_WG_NIC}"
  261.     echo -e "\nHere is your client config file as a QR Code:"
  262.     qrencode -t ansiutf8 -l L <"${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf"
  263.     echo "It is also available in ${HOME_DIR}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf"
  264. }
  265. function revokeClient() {
  266.     NUMBER_OF_CLIENTS=$(grep -c -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf")
  267.     if [[ ${NUMBER_OF_CLIENTS} == '0' ]]; then
  268.         echo ""
  269.         echo "You have no existing clients!"
  270.         exit 1
  271.     fi
  272.     echo ""
  273.     echo "Select the existing client you want to revoke"
  274.     grep -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf" | cut -d ' ' -f 3 | nl -s ') '
  275.     until [[ ${CLIENT_NUMBER} -ge 1 && ${CLIENT_NUMBER} -le ${NUMBER_OF_CLIENTS} ]]; do
  276.         if [[ ${CLIENT_NUMBER} == '1' ]]; then
  277.             read -rp "Select one client [1]: " CLIENT_NUMBER
  278.         else
  279.             read -rp "Select one client [1-${NUMBER_OF_CLIENTS}]: " CLIENT_NUMBER
  280.         fi
  281.     done
  282.     # match the selected number to a client name
  283.     CLIENT_NAME=$(grep -E "^### Client" "/etc/wireguard/${SERVER_WG_NIC}.conf" | cut -d ' ' -f 3 | sed -n "${CLIENT_NUMBER}"p)
  284.     # remove [Peer] block matching $CLIENT_NAME
  285.     sed -i "/^### Client ${CLIENT_NAME}\$/,/^$/d" "/etc/wireguard/${SERVER_WG_NIC}.conf"
  286.     # remove generated client file
  287.     rm -f "${HOME}/${SERVER_WG_NIC}-client-${CLIENT_NAME}.conf"
  288.     # restart wireguard to apply changes
  289.     systemctl restart "wg-quick@${SERVER_WG_NIC}"
  290. }
  291. function uninstallWg() {
  292.     checkOS
  293.     systemctl stop "wg-quick@${SERVER_WG_NIC}"
  294.     systemctl disable "wg-quick@${SERVER_WG_NIC}"
  295.     if [[ ${OS} == 'ubuntu' ]]; then
  296.         apt-get autoremove --purge -y wireguard qrencode
  297.     elif [[ ${OS} == 'debian' ]]; then
  298.         apt-get autoremove --purge -y wireguard qrencode
  299.     elif [[ ${OS} == 'fedora' ]]; then
  300.         dnf remove -y wireguard-tools qrencode
  301.         if [[ ${VERSION_ID} -lt 32 ]]; then
  302.             dnf remove -y wireguard-dkms
  303.             dnf copr disable -y jdoss/wireguard
  304.         fi
  305.         dnf autoremove -y
  306.     elif [[ ${OS} == 'centos' ]]; then
  307.         yum -y remove kmod-wireguard wireguard-tools qrencode
  308.         yum -y autoremove
  309.     elif [[ ${OS} == 'arch' ]]; then
  310.         pacman -Rs --noconfirm wireguard-tools qrencode
  311.     fi
  312.     rm -rf /etc/wireguard
  313.     rm -f /etc/sysctl.d/wg.conf
  314.     # Reload sysctl
  315.     sysctl --system
  316.     # Check if WireGuard is running
  317.     systemctl is-active --quiet "wg-quick@${SERVER_WG_NIC}"
  318.     WG_RUNNING=$?
  319.     if [[ ${WG_RUNNING} -eq 0 ]]; then
  320.         echo "WireGuard failed to uninstall properly."
  321.         exit 1
  322.     else
  323.         echo "WireGuard uninstalled successfully."
  324.         exit 0
  325.     fi
  326. }
  327. function manageMenu() {
  328.     echo "Welcome to WireGuard-install!"
  329.     echo "The git repository is available at: https://github.com/angristan/wireguard-install"
  330.     echo ""
  331.     echo "It looks like WireGuard is already installed."
  332.     echo ""
  333.     echo "What do you want to do?"
  334.     echo "   1) Add a new user"
  335.     echo "   2) Revoke existing user"
  336.     echo "   3) Uninstall WireGuard"
  337.     echo "   4) Exit"
  338.     until [[ ${MENU_OPTION} =~ ^[1-4]$ ]]; do
  339.         read -rp "Select an option [1-4]: " MENU_OPTION
  340.     done
  341.     case "${MENU_OPTION}" in
  342.     1)
  343.         newClient
  344.         ;;
  345.     2)
  346.         revokeClient
  347.         ;;
  348.     3)
  349.         uninstallWg
  350.         ;;
  351.     4)
  352.         exit 0
  353.         ;;
  354.     esac
  355. }
  356. # Check for root, virt, OS...
  357. initialCheck
  358. # Check if WireGuard is already installed and load params
  359. if [[ -e /etc/wireguard/params ]]; then
  360.     source /etc/wireguard/params
  361.     manageMenu
  362. else
  363.     installWireGuard
  364. fi

使用方法:

1、复制上述文本内容保存为“install.sh”;

2、赋予install.sh可执行权限;

3、运行脚本,根据提示自动安装。

本脚本转自https://github.com/angristan/wireguard-install,如遇问题欢迎issue!

你想把广告放到这里吗?

发表评论

您必须 登录 才能发表留言!