Before Installing WireGuard, Port 51820 should be opened.
Shift To Root user and install packages and repositories
sudo su -
apt update
apt install software-properties-common
add-apt-repository ppa:wireguard/wireguard
When prompted, press Enter to continue. add-apt-repository will also automatically update the package list.
WireGuard runs as a kernel module, which is compiled as a DKMS module.
apt-get install wireguard-dkms wireguard-tools linux-headers-$(uname -r)
OR
apt install wireguard
WireGuard have two main command-line tools named wg and wg-quick that allow you to configure and manage the WireGuard interfaces.
wg genkey | sudo tee /etc/wireguard/privatekey | wg pubkey | sudo tee /etc/wireguard/publickey
For Accessing The Public Key
cat /etc/wireguard/publickey
For Accessing The Private Key
cat /etc/wireguard/privatekey
vi /etc/wireguard/wg0.conf
ADD these lines
[Interface]
Address = 10.0.0.1/24
SaveConfig = true
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o ens3 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o ens3 -j MASQUERADE
Replace ens3 to your public network interface, you can find it thru ifconfig or ip -o -4 route show to default | awk '{print $5}' cmd.
Example - Change ens3 to eth0
You can also Add Peers rightnow in the same file (/etc/wireguard/wg0.conf)
[Peer]
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.0.0.2/32
[Peer]
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.0.0.3/32
[Peer]
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.0.0.4/32
.....
Note - For any changes in peers you have to first stop wireguard and then do the changes.
wg-quick up wg0
wg-quick down wg0
wg show wg0
and
ip a show wg0
systemctl enable wg-quick@wg0
Edit /etc/sysctl.conf
Add or Uncomment the following line:
net.ipv4.ip_forward=1
To Check
sysctl -p
Easy Way
wg set wg0 peer CLIENT_PUBLIC_KEY allowed-ips 10.0.0.2
wg set wg0 peer CLIENT_PUBLIC_KEY allowed-ips 10.0.0.3
wg set wg0 peer CLIENT_PUBLIC_KEY allowed-ips 10.0.0.4
.....
- Download WireGuard from the APP Store or there website
- Lunch the app and click on add button [+]
- Add an Empty Tunnel
- Name it and Add the fallowing Lines and save
[Interface]
PrivateKey = .....
Address = 10.0.0.2/24
DNS = 1.1.1.1
[Peer]
PublicKey = SERVER_PUBLIC_KEY
AllowedIPs = 0.0.0.0/0
Endpoint = SERVER_PUBLIC_IP:51820
PersistentKeepalive = 30
- You woud also find your Public Key in above section only, which you have to add back to the server.
- Repeat this process for adding more peers.
Your WireGuard is all setuped.