{"id":37,"date":"2024-07-24T08:01:07","date_gmt":"2024-07-24T08:01:07","guid":{"rendered":"http:\/\/192.168.40.2\/?p=37"},"modified":"2025-07-24T08:50:27","modified_gmt":"2025-07-24T08:50:27","slug":"set-up-wireguard-vpn-on-ubuntu-server-and-client","status":"publish","type":"post","link":"https:\/\/allogman.com\/?p=37","title":{"rendered":"Set Up WireGuard VPN on Ubuntu (Server and Client)"},"content":{"rendered":"\n<p>WireGuard is a simple, fast, and secure VPN that utilizes state-of-the-art cryptography. With a small source code footprint, it aims to be faster and leaner than other VPN protocols such as OpenVPN and IPSec. WireGuard is still under development, but even in its unoptimized state it is faster than the popular OpenVPN protocol.<\/p>\n\n\n\n<p>The WireGuard configuration is as simple as setting up SSH. A connection is established by an exchange of public keys between server and client. Only a client that has its public key in its corresponding server configuration file is allowed to connect. WireGuard sets up standard network interfaces (such as wg0 and wg1), which behave much like the commonly found eth0 interface. This makes it possible to configure and manage WireGuard interfaces using standard tools such as ifconfig and ip.<\/p>\n\n\n\n<p>This guide will configure a simple peer connection between Ubuntu server and client, as all the installation set up will be the same from all sides, server and the client, what will make difference is the configuration which we will go through in details.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"install-wireguard\">Install WireGuard<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Update your package index by running the following command:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt-get update\n<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>Install Wireguard<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install wireguard -y\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"configure-wireguard-server\">Configure WireGuard Server<\/h4>\n\n\n\n<ol class=\"wp-block-list\">\n<li>One of the best things about WireGuard is that it\u2019s security is based on SSH-like key pairs. So, the first thing to be done is to generate the necessary private and public key pair. Generate a private and public key pair for the WireGuard server:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mkdir ~\/.wireguard\n$ cd ~\/.wireguard\n$ umask 077\n$ wg genkey | tee privatekey | wg pubkey &gt; publickey\n<\/code><\/pre>\n\n\n\n<p>This will save both the private and public keys to your home directory; they can be viewed with cat&nbsp;<code>privatekey<\/code>&nbsp;and cat&nbsp;<code>publickey<\/code>&nbsp;respectively. 2. Next, you need to copy the contents of newly-generated private key with the command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat privatekey\n<\/code><\/pre>\n\n\n\n<p>The above command will print out a string of characters. You\u2019ll need to do this on both the server and the client (as you\u2019ll need the server private key and the client public key). 3. Copy that string to your clipboard and then create a new WireGuard configuration file with the command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/wireguard\/wg0.conf\n<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>In that file, paste the following:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Interface]\nAddress = 10.0.0.1\/24\nListenPort = 41194\nPrivateKey = SERVER_PRIVATE_KEY\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Address<\/strong>\u00a0defines the private IPv4 and IPv6 addresses for the WireGuard server. Each peer in the VPN network should have a unique value for this field.<\/li>\n\n\n\n<li><strong>ListenPort<\/strong>\u00a0specifies which port WireGuard will use for incoming connections.<strong>NOTE:<\/strong>\u00a0If you want to have many servers connected to one or more clients, you need to have different\u00a0<strong>ListenPort<\/strong>, and allow it in your server ufw.<\/li>\n<\/ul>\n\n\n\n<p>Now, save and close the file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"set-up-ufw-firewall-rules-to-open-required-ports\">Set up UFW firewall rules to open required ports<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo ufw allow 41194\/udp\n$ sudo ufw status\n<\/code><\/pre>\n\n\n\n<p>If the&nbsp;<code>ufw<\/code>&nbsp;is inactive, you can enable it by the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw enable\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"enable-and-start-wireguard-service\">Enable and start WireGuard Service<\/h3>\n\n\n\n<p>Turn the WireGuard service at boot time using the systemctl command, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl enable wg-quick@wg0\n<\/code><\/pre>\n\n\n\n<p>Start the service, execute:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl start wg-quick@wg0\n<\/code><\/pre>\n\n\n\n<p>Get the service status, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl status wg-quick@wg0\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>Note You can turn off the wg0 interface with&nbsp;<code>sudo systemctl stop wg-quick@wg0<\/code><\/p>\n<\/blockquote>\n\n\n\n<p>Verify that interface named wg0 is up and running on Ubuntu server using the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo wg\n$ sudo ip a show wg0\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"wireguard-client\">Wireguard Client<\/h3>\n\n\n\n<p>The process for setting up a client is exactly same as setting up the server. When using Ubuntu as your client\u2019s operating system, the only difference between the client and the server is the contents of the configuration file. If your client uses Ubuntu, follow the steps provided in the above sections and in this section.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Generate a key pair for the client:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mkdir ~\/.wireguard\n$ cd ~\/.wireguard\n$ umask 077\n$ wg genkey | tee privatekey | wg pubkey &gt; publickey\n<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>Next, copy the contents of newly-generated private key with the command:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>cat privatekey\n<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li>Copy that string to your clipboard and then create a new WireGuard configuration file with this command -we are using nano in this example, but feel free to use whatever text editor you prefer-:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/wireguard\/wg0.conf\n<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li>In that file, paste the following:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Interface]\nAddress = 10.0.0.2\/32\nPrivateKey = CLIENT_PRIVATE_KEY\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"connect-the-client-and-server\">Connect the Client and Server<\/h3>\n\n\n\n<p>First of all, you need to stop the interface on the server, by issuing the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl stop wg-quick@wg0\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Edit the client\u2019s wg0.conf file by adding the server\u2019s public key, public IP address, and port:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Peer]\nPublicKey = SERVER_PUBLIC_KEY\nEndpoint = SERVER_PUBLIC_IP:41194\nAllowedIPs = 10.0.0.0\/24\nPersistentKeepalive = 15\n<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>Edit the server\u2019s wg0.conf file by adding the client\u2019s public key, and port:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Peer]\nPublicKey =  CLIENT_PUBLIC_KEY\nAllowedIPs = 10.0.0.2\/0\n<\/code><\/pre>\n\n\n\n<p>Enable and start VPN client\/peer connection, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo systemctl enable wg-quick@wg0\n$ sudo systemctl start wg-quick@wg0\n$ sudo systemctl status wg-quick@wg0\n<\/code><\/pre>\n\n\n\n<p>Then, run&nbsp;<code>sudo wg<\/code>&nbsp;and you will be able to see their connection! The last two lines of the output from running the&nbsp;<code>wg<\/code>&nbsp;command should be similar to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>latest handshake: 1 minute, 17 seconds ago\ntransfer: 98.86 KiB received, 43.08 KiB sent\n<\/code><\/pre>\n\n\n\n<p>This indicates that you now have a private connection between the server and client. You can also ping the client from the server to verify that the connection works both ways.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"verification\">Verification<\/h3>\n\n\n\n<p>That is all. By now, both Ubuntu servers and clients must be connected securely using a peer-to-peer VPN called WireGuard. Let us test the connection. Type the following ping command on your client machine\/desktop system:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ping 10.0.0.1\n$ sudo wg\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h3>\n\n\n\n<p>Congratulation! You just learned about setting up a WireGuard VPN server on Ubuntu server and peer (client machine) . For more information, I strongly suggest that you read&nbsp;<a href=\"https:\/\/www.wireguard.com\/\">WireGuard<\/a>&nbsp;project documentation here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WireGuard is a simple, fast, and secure VPN that utilizes state-of-the-art cryptography. With a small source code footprint, it aims to be faster and leaner than other VPN protocols such<\/p>\n<p><a href=\"https:\/\/allogman.com\/?p=37\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\">Set Up WireGuard VPN on Ubuntu (Server and Client)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-network-security"],"_links":{"self":[{"href":"https:\/\/allogman.com\/index.php?rest_route=\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/allogman.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/allogman.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/allogman.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/allogman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":1,"href":"https:\/\/allogman.com\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":38,"href":"https:\/\/allogman.com\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions\/38"}],"wp:attachment":[{"href":"https:\/\/allogman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/allogman.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/allogman.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}