{"id":13569,"date":"2024-09-05T05:36:31","date_gmt":"2024-09-05T02:36:31","guid":{"rendered":"https:\/\/www.sunucun.com.tr\/blog\/?post_type=dt_articles&#038;p=13569"},"modified":"2026-11-15T22:59:07","modified_gmt":"2026-11-15T19:59:07","slug":"wireguard-vpn-setup","status":"publish","type":"post","link":"https:\/\/sunucun.com.tr\/blog\/wireguard-vpn-setup\/","title":{"rendered":"WireGuard VPN Setup: Step-by-Step on an Ubuntu Server (wg-quick)"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_90 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/sunucun.com.tr\/blog\/wireguard-vpn-setup\/#Requirements\" >Requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/sunucun.com.tr\/blog\/wireguard-vpn-setup\/#1_Install_WireGuard\" >1. Install WireGuard<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/sunucun.com.tr\/blog\/wireguard-vpn-setup\/#2_Generate_the_Keys\" >2. Generate the Keys<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/sunucun.com.tr\/blog\/wireguard-vpn-setup\/#3_Server_Configuration_wg0conf\" >3. Server Configuration (wg0.conf)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/sunucun.com.tr\/blog\/wireguard-vpn-setup\/#4_Enable_IP_Forwarding\" >4. Enable IP Forwarding<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/sunucun.com.tr\/blog\/wireguard-vpn-setup\/#5_Open_the_Port_in_the_Firewall\" >5. Open the Port in the Firewall<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/sunucun.com.tr\/blog\/wireguard-vpn-setup\/#6_Start_the_Tunnel\" >6. Start the Tunnel<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/sunucun.com.tr\/blog\/wireguard-vpn-setup\/#7_Client_Computer_or_Phone_Configuration\" >7. Client (Computer or Phone) Configuration<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/sunucun.com.tr\/blog\/wireguard-vpn-setup\/#If_the_Connection_Does_Not_Work\" >If the Connection Does Not Work<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/sunucun.com.tr\/blog\/wireguard-vpn-setup\/#Adding_More_Devices\" >Adding More Devices<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/sunucun.com.tr\/blog\/wireguard-vpn-setup\/#Security_Tips\" >Security Tips<\/a><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\"><strong>WireGuard<\/strong> is a fast, open source VPN protocol with a simple design that uses modern cryptography. In this guide, we explain how to install WireGuard with the official packages on a server running Ubuntu (for example a <a href=\"https:\/\/sunucun.com.tr\/vds-sunucu\" data-internallinksmanager029f6b8e52c=\"133\" title=\"Tahsisli kaynakl\u0131 sanal sunucu \u00e7\u00f6z\u00fcmleri\">VDS<\/a>) and connect a computer or phone to the internet through that server. The commands are based on WireGuard&#8217;s official installation page and the Ubuntu manual pages (<code>wg<\/code>, <code>wg-quick<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Requirements\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A server running Ubuntu (or Debian) with root\/sudo access and a static public IP address<\/li>\n<li>A UDP port that can be opened on the server (<strong>51820<\/strong> in this guide)<\/li>\n<li>The official WireGuard app for the device that will connect: Windows, macOS (App Store), Android (Google Play) or iOS (App Store)<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"1_Install_WireGuard\"><\/span>1. Install WireGuard<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">WireGuard is available in the official Ubuntu and Debian repositories:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt install wireguard<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2_Generate_the_Keys\"><\/span>2. Generate the Keys<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Each device has a private key and a public key. Generate the server keys with the one-line command from WireGuard&#8217;s quick start page:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cd \/etc\/wireguard\numask 077\nwg genkey | sudo tee server_private.key | wg pubkey | sudo tee server_public.key<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can generate the keys for the connecting device in the same way (for example <code>client_private.key<\/code> \/ <code>client_public.key<\/code>), or let the client app create them by choosing &#8220;add empty tunnel&#8221;. Never share private keys with anyone.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3_Server_Configuration_wg0conf\"><\/span>3. Server Configuration (wg0.conf)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For the command <code>wg-quick up wg0<\/code>, <code>wg-quick<\/code> first looks for the configuration in <code>\/etc\/wireguard\/wg0.conf<\/code>. Create the file (<code>sudo nano \/etc\/wireguard\/wg0.conf<\/code>) and fill it in with your own keys. Replace <code>eth0<\/code> with the name of your server&#8217;s internet-facing network interface (shown in the &#8220;default &#8230; dev&#8221; part of the <code>ip route<\/code> output):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[Interface]\nAddress = 10.8.0.1\/24\nListenPort = 51820\nPrivateKey = SERVER_PRIVATE_KEY\nPostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE\nPostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE\n[Peer]\n# The connecting device\nPublicKey = CLIENT_PUBLIC_KEY\nAllowedIPs = 10.8.0.2\/32<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Address:<\/strong> The server&#8217;s address inside the VPN.<\/li>\n<li><strong>AllowedIPs (on the server):<\/strong> Which source addresses are accepted for traffic from this device; give each device its own \/32 address.<\/li>\n<li><strong>PostUp \/ PostDown:<\/strong> Commands that run when the tunnel comes up and goes down; here they let VPN traffic reach the internet through the server&#8217;s IP (NAT).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4_Enable_IP_Forwarding\"><\/span>4. Enable IP Forwarding<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For the server to forward traffic from the VPN to the internet, IPv4 forwarding must be enabled:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"net.ipv4.ip_forward=1\" | sudo tee \/etc\/sysctl.d\/99-wireguard.conf\nsudo sysctl --system<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5_Open_the_Port_in_the_Firewall\"><\/span>5. Open the Port in the Firewall<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you use UFW, allow the WireGuard port (make sure your SSH port is also allowed):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo ufw allow 51820\/udp<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If your server provider also has a network firewall in its panel, open UDP 51820 there as well.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6_Start_the_Tunnel\"><\/span>6. Start the Tunnel<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo wg-quick up wg0\nsudo systemctl enable wg-quick@wg0\nsudo wg show<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>systemctl enable wg-quick@wg0<\/code> makes the tunnel start automatically when the server reboots. <code>wg show<\/code> displays the peers, the latest handshake time and the data transferred.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7_Client_Computer_or_Phone_Configuration\"><\/span>7. Client (Computer or Phone) Configuration<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Create a new tunnel in the WireGuard app and enter this configuration:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[Interface]\nPrivateKey = CLIENT_PRIVATE_KEY\nAddress = 10.8.0.2\/24\nDNS = 1.1.1.1\n[Peer]\nPublicKey = SERVER_PUBLIC_KEY\nEndpoint = SERVER_IP_ADDRESS:51820\nAllowedIPs = 0.0.0.0\/0\nPersistentKeepalive = 25<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>AllowedIPs = 0.0.0.0\/0:<\/strong> Sends all IPv4 traffic through the tunnel; if you only want to reach the server&#8217;s internal network, write <code>10.8.0.0\/24<\/code>.<\/li>\n<li><strong>PersistentKeepalive = 25:<\/strong> According to the wg manual, this is an example value for keeping the connection open for devices behind NAT; most users do not need it.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">On a phone, instead of typing this configuration by hand, you can turn it into a QR code on the server with the <code>qrencode<\/code> package and scan it from the app: <code>qrencode -t ansiutf8 &lt; client.conf<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"If_the_Connection_Does_Not_Work\"><\/span>If the Connection Does Not Work<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>No &#8220;latest handshake&#8221; in <code>wg show<\/code>:<\/strong> UDP 51820 is closed, the Endpoint IP is wrong or the keys have been swapped (the server must have the client&#8217;s <em>public<\/em> key).<\/li>\n<li><strong>Handshake works but there is no internet:<\/strong> IP forwarding is off or the network interface name in the PostUp line is wrong.<\/li>\n<li><strong>Sites do not open but access by IP works:<\/strong> Check the <code>DNS<\/code> line on the client.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Adding_More_Devices\"><\/span>Adding More Devices<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For each new device, generate a new key pair, add a new <code>[Peer]<\/code> section to the server&#8217;s <code>wg0.conf<\/code> with that device&#8217;s public key and its own address (for example <code>AllowedIPs = 10.8.0.3\/32<\/code>), and use that address in the device&#8217;s <code>Address<\/code> line. Then apply the change with <code>sudo systemctl restart wg-quick@wg0<\/code>. Do not use the same key pair on two devices; they would conflict with each other.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Security_Tips\"><\/span>Security Tips<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Make sure only root can read the private key files (the <code>umask 077<\/code> above takes care of this).<\/li>\n<li>Delete the <code>[Peer]<\/code> section of devices you no longer use and restart the tunnel.<\/li>\n<li>Update the server&#8217;s packages regularly: <code>sudo apt update &amp;&amp; sudo apt upgrade<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">For your own WireGuard server, you can use a <a href=\"https:\/\/sunucun.com.tr\/en\/vds-server\">VDS server<\/a> located in T\u00fcrkiye with root access.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>WireGuard is a fast, open source VPN protocol with a simple design that uses modern cryptography. In this guide, we explain how to install WireGuard with the official packages on a server running Ubuntu (for example a VDS) and connect a computer or phone to the internet through that server. The commands are based on&hellip;<\/p>\n","protected":false},"author":1,"featured_media":13568,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[1519],"tags":[1527],"class_list":["post-13569","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sanal-sunucu","tag-teknoloji"],"_links":{"self":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/13569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/comments?post=13569"}],"version-history":[{"count":1,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/13569\/revisions"}],"predecessor-version":[{"id":19879,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/posts\/13569\/revisions\/19879"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/media\/13568"}],"wp:attachment":[{"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/media?parent=13569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/categories?post=13569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sunucun.com.tr\/blog\/wp-json\/wp\/v2\/tags?post=13569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}