You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.4 KiB

  1. #!/bin/bash
  2. # This script can be used directly from bash after defining the IP address for the node with :
  3. # IP=x bash <(curl -s https://gitlab.altinea.fr/altinea/install-scripts/raw/branch/master/wireguard/deploy-wg-clients.sh)
  4. if ! [[ $IP =~ ^[0-9]{1,3}$ ]] ; then
  5. echo "error: Please set IP variable (with IP=1-254. See https://phpipam.altinea.fr" >&2; exit 1
  6. fi
  7. # Create keys subdir, generate private and derive public key
  8. mkdir -p /etc/wireguard/keys
  9. umask 077 && wg genkey > /etc/wireguard/keys/private.key && wg pubkey < /etc/wireguard/keys/private.key > /etc/wireguard/keys/public.key && umask 0022
  10. # Calculate IPv4 and IPv6 address
  11. IP4="10.17.25.$IP"
  12. IP6="fd42:42:42:25"`printf '%.2x\n' $IP`"::"
  13. # Create config file for wireguard interface
  14. echo "[Interface]
  15. Address = $IP4/32
  16. Address = $IP6/64
  17. SaveConfig = false
  18. PostUp = wg set %i private-key /etc/wireguard/keys/private.key
  19. PostUp = ping -c1 10.17.25.1
  20. [Peer]
  21. PublicKey = iu3I09FtiVDIOuiU83JvpfJkg4yiCxolqcFsXbz5Ixc=
  22. AllowedIPs = 10.17.24.0/22, fd42:42:42::/48
  23. Endpoint = vpn.altinea.fr:58212
  24. PersistentKeepalive = 25" > /etc/wireguard/wg0.conf
  25. # Enable and start interface (systemctl needed)
  26. systemctl enable wg-quick@wg0.service && systemctl daemon-reload && systemctl start wg-quick@wg0
  27. # Run a ping to make the interface usable
  28. ping -c1 192.168.25.1
  29. # Display the public key to add it on the wireguard concentrator
  30. echo -n "Public key : "
  31. cat /etc/wireguard/keys/public.key
  32. exit 0;