This is the third part of a three-part tutorial.

Closing this port will make your server unmaintainable. Allowing access only to your private IP address will not work if you use dynamic IPs. Unfortunately, this is common practice in Germany, for example.‌‌Regardless of that, you won't be able to access your beloved server while on vacation.

The solution I am showing here allows you to access your server from anywhere.

For this we use Wireguard. Wireguard is a free, modern and very secure VPN solution that is also very resource-efficient (unlike OpenVPN, for example).

Install and configure wireguard

For simplicity's sake, I'm showing this for Linux. The configuration on Windows, Mac, iOS and Android differs only slightly.

Install Wireguard on your server:

apt install wireguard

Install wireguard on all of your clients.

On your server:

Generate wireguard keys:

wg genkey | tee privatekey | wg pubkey > publickey
generate wireguard keys

Your private key is in


Your public key is in:‌


Create a preshared key:

cd /etc/wireguard/
mkdir psk
cd psk
wg genpsk > presharedkey

Your preshared key is in /etc/wireguard/psk/presharedkey

Create a server configuration:

Address = 
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT;\
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT;\
iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 51820 
PrivateKey = <copy and paste from \
# /etc/wireguard/privatekey on your server> 

# Client configuration 
[Peer] PublicKey = <publickey from client config> 
PresharedKey = <copy and paste from \
AllowedIPs =
PersistentKeepalive = 25

Enable portforwarding:

# /etc/sysctl.conf
# comment out the following line

And activate your changes:

sysctl -p

Start and enable your config:

systemctl enable --now [email protected]

On your client:

If not already done. Install wireguard on the client as well.‌‌Repeat the step from above to generate the keys.

Create a client configuration:

PrivateKey = <copy and paste from /etc/wireguard/privatekey\
# on your clinet> 
Address = 

# Server is your clients peer
PublicKey = <copy and paste from /etc/wireguard/publickey\
#from your server> 
PresharedKey = <copy and paste from \
#/etc/wireguard/psk/presharedkey from your server> 
AllowedIPs = 
Endpoint = <YourServerIP>:51820
PersistentKeepalive = 25

You have now created the missing client key and can add it to the configuration on the server side:

PublicKey = <publickey from client config>
/etc/wireguard/wg0.conf on your server‌

Start and enable your config:

systemctl enable --now [email protected]

Test connection:

You should see a connection on both with the following command:

wg show

If not, check if the firewall blocks port 51820/UPD or if the Wireguard service is running:

# should show show active
systemctl status wg-quick@wg0

Often there is a typo in the configuration.

Configure SSH on your server

This part is a little tricky.

Please be sure to check beforehand whether wireguard can establish a stable connection. Otherwise you will be locked out.

It's best to make a backup/snapshot beforehand.

The SSH process must be started after the Wireguard process.

We can set this via SystemD. ‌‌Add [email protected] as required to sshd.service.

Description=OpenBSD Secure Shell server 
Documentation=man:sshd(8) man:sshd_config(5) auditd.service 
# make SSH start after Wireguard 
[email protected] 

ExecStartPre=/usr/sbin/sshd -t 
ExecStart=/usr/sbin/sshd -D $SSHD_OPTS 
ExecReload=/usr/sbin/sshd -t 
ExecReload=/bin/kill -HUP $MAINPID 
KillMode=process Restart=on-failure 

[Install] Alias=sshd.service

This setting is not to be executed on the client!

Now change your SSH listener:

# /etc/ssh/sshd_config 

Restart SSH:

systemctl restart ssh.service

Your server is now only accessible from outside via Wireguard over SSH.

Since SSH only listens to this IP, you don't need to do any firewall settings. You can leave port 22 open for SSH worldwide.