For Industrial & IoT, go to portainer.industries · For AI, go to portainer.ai
How to

How to run Portainer behind a Wireguard VPN

How to run Portainer behind a Wireguard VPN

How-To run Portainer behind a Wireguard VPN

In this blog post you will learn how to run Portainer behind a Wireguard VPN. This will be based on a point-to-point connection between two machines. You can also watch this how-to as a video below.

Pre-reqs

Prepare the Portainer machine
  1. The first step is to generate the wireguard keys for both the main host and the client host that will connect to Portainer via the VPN by running the following:
    1. wg genkey | tee srv.privatekey | wg pubkey > srv.publickey
      We will use these on the Portainer host
    2. wg genkey | tee clt.privatekey | wg pubkey > clt.publickey
      We will use these on the client host
  2. After running the key generation commands above you can continue by running the following commands on the host running Portainer. Please make sure to copy&paste the entire code block below:
server_priv=`cat srv.privatekey`
server_publ=`cat srv.publickey`
client_priv=`cat clt.privatekey`
client_publ=`cat clt.publickey`

cat <<EOF> server.conf
[Interface]
# Name: server
Address = 192.168.10.10/24
PrivateKey = $server_priv
ListenPort = 51820

[Peer]
# Name: client
PublicKey = $client_publ
AllowedIPs = 192.168.10.11/32
EOF

cat <<EOF> client.conf
[Interface]
# Name: client
PrivateKey = $client_priv 
Address = 192.168.10.11/24

[Peer]
# Name: server
PublicKey = $server_publ
EndPoint  = [SERVER IP]:51820
AllowedIPs = 192.168.10.10/32, 192.168.10.0/24
PersistentKeepalive = 25
EOF

rm *.privatekey
rm *.publickey

This script will generate two wiregaurd configuration the base files that will be used by the wireguard clients to connect to each other:

The VPN subnet chosen for this example is 192.168.10.0

Wireguard configuration
  1. On the Portainer host install wireguard according to the linux version you are running. Please following the instructions on the Wireguard documentation page: https://www.wireguard.com/install/
  2. Move/Copy the server.conf file to the /etc/wireguard folder.
  3. On the client host move/copy the client.conf file to the same /etc/wireguard folder if it is a linux machine or use the Import option if you are running Wireguard on MacOS or Windows.
Start Wireguard and access Portainer
  1. On the Portainer host you can start wireguard by running:
    wg-quick up server
  2. On the client host you can start wireguard by running:
    wg-quick up client if it is a linux machine or use the corresponding Activate option on MacOS/Windows clients.
    You should be able to ping from the client host the Portainer machine by running ping 192.168.10.10
  3. Finally you can open your browser on the client machine using the following address:
    1. https://192.168.10.10:9000 - if you deployed Portainer on Docker or
    2. https://192.168.10.10:30777 - if you deployed Portainer on Kubernetes

Try Portainer with 3 Nodes Free

If you're ready to get started with Portainer Business, 3 nodes free is a great place to begin. If you'd prefer to get in touch with us, we'd love to hear from you!


Get 3 nodes free More from the resource hub