Skip to content

Instantly share code, notes, and snippets.

@setioaji
Last active January 18, 2024 07:03
Show Gist options
  • Save setioaji/996653655215c59dfed790c02938f269 to your computer and use it in GitHub Desktop.
Save setioaji/996653655215c59dfed790c02938f269 to your computer and use it in GitHub Desktop.
Setup ubuntu for wireguard server

Wireguard CLI for ubuntu

DOCS:

  1. https://www.digitalocean.com/community/tutorials/how-to-set-up-wireguard-on-ubuntu-20-04
  2. https://www.wireguard.com/#simple-network-interface

I. SERVER

apt -y update && \
apt -y install wireguard
# this will generate server private key & public key for server, run again for client on deferent device.
wg genkey | tee privateKey | wg pubkey > publicKey

cat /etc/wireguard/wg0.conf

[Interface]
# create an address according to your needs 
Address = 10.0.0.1/24
PostUp = ufw route allow in on wg0 out on enp1s0
PostUp = iptables -t nat -I POSTROUTING -o enp1s0 -j MASQUERADE
PostUp = ip6tables -t nat -I POSTROUTING -o enp1s0 -j MASQUERADE
PreDown = ufw route delete allow in on wg0 out on enp1s0
PreDown = iptables -t nat -D POSTROUTING -o enp1s0 -j MASQUERADE
PreDown = ip6tables -t nat -D POSTROUTING -o enp1s0 -j MASQUERADE
# you can use any udp port
ListenPort = 53133
PrivateKey = <<ServerPrivateKey>>

[Peer]
PublicKey = <<PublicKeyClient1>>
# create an subnet according to your needs 
AllowedIPs = 10.0.0.2/32

# multiple client
[Peer]
PublicKey = <<PublicKeyClient2>>
AllowedIPs = 10.0.0.3/32

Edit cat /etc/sysctl.conf

net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1

sysctl -p # to enable packet forwarding

II. START/STOP

systemctl stop wg-quick@wg0
systemctl start wg-quick@wg0
systemctl status wg-quick@wg0
journalctl -xf -n10 -u wg-quick@wg0.service
sudo wg

NB: you may have to install apt-get -y install openresolv if wire-guard is unable to start

III. EDITING CONFIG WG

For edit /etc/wireguard/wg0.conf you need to:

  • a. stop wg
  • b. edit files
  • c. start wg if you don't do that, changes not saved.

IV. Client

You can choose of client in https://www.wireguard.com/install/

V. Client File Configuration

Example file configuration on android/ios/mac/windows

[Interface]
Address = 10.0.0.3/32
DNS = 1.1.1.1
PrivateKey = <<PrivateKeyClient2>>
[Peer]
publickey = <<ServerPublicKey>>
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = <<IpServer>>:53133
@setioaji
Copy link
Author

inside wg0.conf file.
[Peer] is a client of wireguard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment