wireguard-instant.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/bash
  2. ### Purpose of this script
  3. # Setup a central WireGuard instance (vpn server) to
  4. # - serve as exit node
  5. # - to generate key material / configurations for clients
  6. # - help me to memorize WireGuard basics
  7. ### Files generated:
  8. # <hostname>.key - private key
  9. # <hostname>.pub - public key
  10. # <hostname>.psk - pre-shared key (PSK) per client
  11. # <hostname>.conf - configuration file for the client
  12. ### Client configuration
  13. # - Linux
  14. # - Save <hostname>.conf under /etc/wireguard/wg0.conf
  15. # - Run wg-quick up wg0
  16. # - Android / iOS
  17. # - Scan QR-Code
  18. ### References
  19. # https://wiki.archlinux.org/title/WireGuard
  20. ### Installation
  21. # Install wireguard and QR-Code generator
  22. apt-get install --yes wireguard qrencode
  23. ### Variables
  24. hostname="vpn.example.com"
  25. server_ip='192.168.130.1/24'
  26. network='192.168.130.0/24'
  27. confdir='/etc/wireguard'
  28. interface='ens3'
  29. ### Create key material
  30. cd $confdir
  31. # Function to generate private and public keys
  32. wgkeypair() { wg genkey | (umask 0077 && tee $1.key) | wg pubkey > $1.pub; }
  33. # Function to generate the pre-shared key (PSK) for clients
  34. wgpsk() { wg genpsk | (umask 0077 && cat > $1.psk) }
  35. # Generate key-pair for the central instance
  36. wgkeypair $hostname
  37. # Generate key-pairs and PSKs for the clients (peers)
  38. peers="alice bob"
  39. for peer in $peers; do wgkeypair $peer && wgpsk $peer; done
  40. ### Configuration
  41. # Setup wireguard network interface on the client
  42. ip link add dev wg0 type wireguard
  43. ip addr add $server_ip dev wg0
  44. wg set wg0 listen-port 51871 private-key $confdir/$hostname.key
  45. # Function to generate
  46. # - configure peers on the server
  47. # - prepare config files for the peers
  48. wgsetpeer() {
  49. peer=$1
  50. ip=$2
  51. wg set wg0 \
  52. peer `cat $confdir/$peer.pub` \
  53. preshared-key $confdir/$peer.psk \
  54. allowed-ips $ip
  55. ip link set wg0 up
  56. cat >$confdir/$peer.conf <<EOL
  57. # Client config for $peer
  58. [Interface]
  59. PrivateKey = `cat $confdir/$peer.key`
  60. Address = $ip
  61. #DNS = 8.8.8.8
  62. [Peer]
  63. PublicKey = `cat $confdir/host.pub`
  64. AllowedIPs = $network
  65. Endpoint = $hostname:51871
  66. #PersistentKeepalive = 15
  67. PresharedKey = `cat $confdir/$peer.psk`
  68. EOL
  69. qrencode -t ANSIUTF8 < $confdir/$peer.conf
  70. }
  71. wgsetpeer alice 192.168.130.2
  72. wgsetpeer bob 192.168.130.3
  73. # https://www.cyberciti.biz/faq/how-to-set-up-wireguard-firewall-rules-in-linux/
  74. iptables -t nat -I POSTROUTING 1 -s $network -o $interface -j MASQUERADE
  75. iptables -I INPUT 1 -i wg0 -j ACCEPT
  76. iptables -I FORWARD 1 -i $interface -o wg0 -j ACCEPT
  77. iptables -I FORWARD 1 -i wg0 -o $interface -j ACCEPT
  78. iptables -I INPUT 1 -i $interface -p udp --dport 51871 -j ACCEPT
  79. sysctl -w net.ipv4.ip_forward=1