How to Install and Set Up WireGuard?


Install WireGuard

Install WireGuard on Ubuntu Server

sudo apt update
sudo apt install -y wireguard wireguard-tools

After installation, check the WireGuard version


wg —version

Create Keys

Generate Keys

Both the cloud server and local server generate keys using the following commands:

cd /etc/wireguard
umask 077
wg genkey | tee private.key | wg pubkey > public.key

View Keys

View commands:

cat private.key
cat public.key

Create Configuration Files

Cloud server configuration file creation

Execute:

nano /etc/wireguard/wg0.conf

File content:

[Interface]
Address = <Cloud Server Custom Internal IP>  # 10.88.88.1/24
ListenPort = <Cloud Server Port> # 51820
PrivateKey = <Cloud Server Private Key> # EAiM1J8EKDMb5jeFKxnWt6SMzkldgEM=

[Peer]
PublicKey = <Local Server Public Key> # 2W/npFHw9apNZN4hmhemFfK9gicfmY1kYL6hWAQ8jCE=
AllowedIPs = <Local Server Custom Internal IP> # 10.88.88.2/32

Local server configuration file creation Execute:

nano /etc/wireguard/wg0.conf

File content:

[Interface]
PrivateKey = <Local Server Private Key> # pK9gicfmY1kYL6hWAQ8jCE=
Address = <Local Server Custom Internal IP> # 10.88.88.2/24
ListenPort = <Cloud Server Port> # 51820

[Peer]
PublicKey = <Cloud Server Public Key> # icfmY1kYL6hWAQ8jCE=
Endpoint = <Cloud Server Public IP>:<Cloud Server Port> # xx.x.xxx.x:51820
AllowedIPs = <Cloud Server Custom Internal IP> # 10.88.88.1/32
PersistentKeepalive = 25

Start and Stop (Temporary)

The following commands are only effective while the server is running; WireGuard will not restart automatically after shutdown or reboot.

Stop WG:

sudo wg-quick down wg0

Start:

sudo wg-quick up wg0

Auto-start on Boot

To set WireGuard to auto-start on boot on Ubuntu Server:

sudo systemctl enable wg-quick@wg0

Start immediately:

sudo systemctl start wg-quick@wg0

Check status:

systemctl status wg-quick@wg0

Tips

WireGuard Status

After WireGuard is started on both the cloud server and local server, you can check the WireGuard status to see if it’s Active: active.

Use ping to test

For example, if the cloud server can ping the local server, then everything is fine (conversely, the local server can also ping the cloud server’s custom IP). Example in this article:

ping 10.88.88.2

Intermittent Disconnection

Recently, I’ve noticed that WireGuard often disconnects every few days, requiring me to stop and start WireGuard to fix it.

However, WireGuard’s purpose is to allow public network access to the internal network. Once it disconnects, public access to the internal network is lost, and I can only restart it by being physically at the local server and executing commands.

When using frp as an SSH proxy, this situation rarely occurs. Even if it disconnects for some reason, frp will automatically reconnect. Therefore, it is recommended to use frp as a backup to ensure that you can still SSH into the local server from the public network to execute commands if WireGuard disconnects.