{"id":738,"date":"2019-05-03T08:51:39","date_gmt":"2019-05-03T08:51:39","guid":{"rendered":"http:\/\/blogs.gentoo.org\/lu_zero\/?p=738"},"modified":"2019-05-03T17:09:13","modified_gmt":"2019-05-03T17:09:13","slug":"using-wireguard","status":"publish","type":"post","link":"https:\/\/blogs.gentoo.org\/lu_zero\/2019\/05\/03\/using-wireguard\/","title":{"rendered":"Using Wireguard"},"content":{"rendered":"<p><a href=\"https:\/\/www.wireguard.com\/\">wireguard<\/a> is a modern, secure and fast vpn tunnel that is extremely simple to setup and works already nearly everywhere.<\/p>\n<p>Since I spent a little bet to play with it because <a href=\"https:\/\/git.zx2c4.com\/wireguard-rs\/about\/\">this<\/a> looked quite interesting, I thought of writing a small tutorial.<\/p>\n<p>I normally use <a href=\"https:\/\/gentoo.org\">Gentoo<\/a> (and <a href=\"https:\/\/apple.com\">macos<\/a>) so this guide is about <strong>Gentoo<\/strong>.<\/p>\n<h2>General concepts<\/h2>\n<p>Wireguard sets up <strong>peers<\/strong> identified by an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Public-key_cryptography\">public key<\/a> and manages a <a href=\"https:\/\/en.wikipedia.org\/wiki\/TUN\/TAP\">virtual network interface<\/a> and the routing across them (optionally).<\/p>\n<p>The <strong>server<\/strong> is just a <strong>peer<\/strong> that knows about loots of peers while a <strong>client<\/strong> knows how to directly reach the <strong>server<\/strong> and that&#8217;s it.<\/p>\n<h2>Setting up in Gentoo<\/h2>\n<p>Wireguard on Linux is implemented as a kernel module.<\/p>\n<p>So in general you have to build the module and the userspace tools (<code>wg<\/code>).<br \/>\nIf you want to have some advanced feature make sure that your kernel has the following settings:<\/p>\n<blockquote><p><code>IP_ADVANCED_ROUTER<\/code><br \/>\n<code>IP_MULTIPLE_TABLES<\/code><br \/>\n<code>NETFILTER_XT_MARK<\/code><\/p><\/blockquote>\n<p>After that using <code>emerge<\/code> will get you all you need:<\/p>\n<div class=\"codehilite\">\n<pre>$ emerge wireguard\n<\/pre>\n<\/div>\n<h3>Tools<\/h3>\n<p>The default distribution of tools come with the <code>wg<\/code> command and an helper script called <code>wg-quick<\/code> that makes easier to bring up and down the virtual network interface.<\/p>\n<div class=\"codehilite\">\n<pre>wg help\nUsage: wg &lt;cmd&gt; [&lt;args&gt;]\n\nAvailable subcommands:\n  show: Shows the current configuration and device information\n  showconf: Shows the current configuration of a given WireGuard interface, for use with `setconf'\n  set: Change the current configuration, add peers, remove peers, or change peers\n  setconf: Applies a configuration file to a WireGuard interface\n  addconf: Appends a configuration file to a WireGuard interface\n  genkey: Generates a new private key and writes it to stdout\n  genpsk: Generates a new preshared key and writes it to stdout\n  pubkey: Reads a private key from stdin and writes a public key to stdout\nYou may pass `--help' to any of these subcommands to view usage.\n<\/pre>\n<\/div>\n<div class=\"codehilite\">\n<pre><span class=\"n\">Usage<\/span><span class=\"o\">:<\/span> <span class=\"n\">wg<\/span><span class=\"o\">-<\/span><span class=\"n\">quick<\/span> <span class=\"o\">[<\/span> <span class=\"n\">up<\/span> <span class=\"o\">|<\/span> <span class=\"n\">down<\/span> <span class=\"o\">|<\/span> <span class=\"n\">save<\/span> <span class=\"o\">|<\/span> <span class=\"n\">strip<\/span> <span class=\"o\">]<\/span> <span class=\"o\">[<\/span> <span class=\"n\">CONFIG_FILE<\/span> <span class=\"o\">|<\/span> <span class=\"n\">INTERFACE<\/span> <span class=\"o\">]<\/span>\n\n  <span class=\"n\">CONFIG_FILE<\/span> <span class=\"k\">is<\/span> <span class=\"n\">a<\/span> <span class=\"n\">configuration<\/span> <span class=\"n\">file<\/span><span class=\"o\">,<\/span> <span class=\"n\">whose<\/span> <span class=\"n\">filename<\/span> <span class=\"k\">is<\/span> <span class=\"n\">the<\/span> <span class=\"kd\">interface<\/span> <span class=\"n\">name<\/span>\n  <span class=\"n\">followed<\/span> <span class=\"n\">by<\/span> <span class=\"err\">`<\/span><span class=\"o\">.<\/span><span class=\"na\">conf<\/span><span class=\"s1\">'. Otherwise, INTERFACE is an interface name, with<\/span>\n<span class=\"s1\">  configuration found at \/etc\/wireguard\/INTERFACE.conf. It is to be readable<\/span>\n<span class=\"s1\">  by wg(8)'<\/span><span class=\"n\">s<\/span> <span class=\"err\">`<\/span><span class=\"n\">setconf<\/span><span class=\"s1\">' sub-command, with the exception of the following additions<\/span>\n<span class=\"s1\">  to the [Interface] section, which are handled by wg-quick:<\/span>\n\n<span class=\"s1\">  - Address: may be specified one or more times and contains one or more<\/span>\n<span class=\"s1\">    IP addresses (with an optional CIDR mask) to be set for the interface.<\/span>\n<span class=\"s1\">  - DNS: an optional DNS server to use while the device is up.<\/span>\n<span class=\"s1\">  - MTU: an optional MTU for the interface; if unspecified, auto-calculated.<\/span>\n<span class=\"s1\">  - Table: an optional routing table to which routes will be added; if<\/span>\n<span class=\"s1\">    unspecified or `auto'<\/span><span class=\"o\">,<\/span> <span class=\"n\">the<\/span> <span class=\"k\">default<\/span> <span class=\"n\">table<\/span> <span class=\"k\">is<\/span> <span class=\"n\">used<\/span><span class=\"o\">.<\/span> <span class=\"n\">If<\/span> <span class=\"err\">`<\/span><span class=\"n\">off<\/span><span class=\"s1\">', no routes<\/span>\n<span class=\"s1\">    are added.<\/span>\n<span class=\"s1\">  - PreUp, PostUp, PreDown, PostDown: script snippets which will be executed<\/span>\n<span class=\"s1\">    by bash(1) at the corresponding phases of the link, most commonly used<\/span>\n<span class=\"s1\">    to configure DNS. The string `%i'<\/span> <span class=\"k\">is<\/span> <span class=\"n\">expanded<\/span> <span class=\"n\">to<\/span> <span class=\"n\">INTERFACE<\/span><span class=\"o\">.<\/span>\n  <span class=\"o\">-<\/span> <span class=\"n\">SaveConfig<\/span><span class=\"o\">:<\/span> <span class=\"k\">if<\/span> <span class=\"kd\">set<\/span> <span class=\"n\">to<\/span> <span class=\"err\">`<\/span><span class=\"kc\">true<\/span><span class=\"err\">'<\/span><span class=\"o\">,<\/span> <span class=\"n\">the<\/span> <span class=\"n\">configuration<\/span> <span class=\"k\">is<\/span> <span class=\"n\">saved<\/span> <span class=\"n\">from<\/span> <span class=\"n\">the<\/span> <span class=\"n\">current<\/span>\n    <span class=\"n\">state<\/span> <span class=\"n\">of<\/span> <span class=\"n\">the<\/span> <span class=\"kd\">interface<\/span> <span class=\"n\">upon<\/span> <span class=\"n\">shutdown<\/span><span class=\"o\">.<\/span>\n\n<span class=\"n\">See<\/span> <span class=\"n\">wg<\/span><span class=\"o\">-<\/span><span class=\"n\">quick<\/span><span class=\"o\">(<\/span><span class=\"mi\">8<\/span><span class=\"o\">)<\/span> <span class=\"k\">for<\/span> <span class=\"n\">more<\/span> <span class=\"n\">info<\/span> <span class=\"n\">and<\/span> <span class=\"n\">examples<\/span><span class=\"o\">.<\/span>\n<\/pre>\n<\/div>\n<h2>Creating a configuration<\/h2>\n<p>Wireguard is quite straightforward, you can either prepare a configuration with your favourite text editor or generate one by setting by hand the <em>virtual network device<\/em> and then saving the result <code>wg showconf<\/code> presents.<\/p>\n<p>A configuration file then can be augmented with <code>wg-quick<\/code>-specific options (such as <code>Address<\/code>) or just passed to <code>wg setconf<\/code> while the other networking details are managed by your usual tools (e.g. <a href=\"https:\/\/wiki.gentoo.org\/wiki\/Iproute2\">ip<\/a>).<\/p>\n<h3>Create your keys<\/h3>\n<p>The first step is to create the public-private key pair that identifies your <em>peer<\/em>.<\/p>\n<ul>\n<li><code>wg genkey<\/code> generates a private key for you.<\/li>\n<li>You feed it to <code>wg pubkey<\/code> to have your public key.<\/li>\n<\/ul>\n<p>In a single line:<\/p>\n<div class=\"codehilite\">\n<pre>$ wg genkey <span class=\"p\">|<\/span> tee privkey <span class=\"p\">|<\/span> wg pubkey &gt; pubkey\n<\/pre>\n<\/div>\n<h3>Prepare a configuration file<\/h3>\n<p>Both <code>wg-quick<\/code> and <code>wg setconf<\/code> use an <em>ini-like<\/em> configuration file.<\/p>\n<p>If you put it in <code>\/etc\/wireguard\/${ifname}.conf<\/code> then <code>wg-quick<\/code> would just need the interface name and would look it up for you.<\/p>\n<p>The minimum configuration needs an <code>[Interface]<\/code> and a <code>[Peer]<\/code> set.<br \/>\nYou may add additional <em>peers<\/em> later.<br \/>\nA <em>server<\/em> would specify its <code>ListenPort<\/code> and identify the <em>peers<\/em> by their <code>PublicKey<\/code>.<\/p>\n<div class=\"codehilite\">\n<pre><span class=\"k\">[Interface]<\/span>\n<span class=\"na\">Address<\/span> <span class=\"o\">=<\/span> <span class=\"s\">192.168.2.1\/24<\/span>\n<span class=\"na\">ListenPort<\/span> <span class=\"o\">=<\/span> <span class=\"s\">51820<\/span>\n<span class=\"na\">PrivateKey<\/span> <span class=\"o\">=<\/span> <span class=\"s\">&lt;key&gt;<\/span>\n\n<span class=\"k\">[Peer]<\/span>\n<span class=\"na\">PublicKey<\/span> <span class=\"o\">=<\/span> <span class=\"s\">&lt;key&gt;<\/span>\n<span class=\"na\">AllowedIPs<\/span> <span class=\"o\">=<\/span> <span class=\"s\">192.168.2.2\/32<\/span>\n<\/pre>\n<\/div>\n<p>A <em>client<\/em> would have a <em>peer<\/em> with an <code>EndPoint<\/code> defined and optionally not specify the <code>ListenPort<\/code> in its <em>interface<\/em> description.<\/p>\n<div class=\"codehilite\">\n<pre><span class=\"k\">[Interface]<\/span>\n<span class=\"na\">PrivateKey<\/span> <span class=\"o\">=<\/span> <span class=\"s\">&lt;key&gt;<\/span>\n<span class=\"na\">Address<\/span> <span class=\"o\">=<\/span> <span class=\"s\">192.168.2.2\/24<\/span>\n\n<span class=\"k\">[Peer]<\/span>\n<span class=\"na\">PublicKey<\/span> <span class=\"o\">=<\/span> <span class=\"s\">&lt;key&gt;<\/span>\n<span class=\"na\">AllowedIPs<\/span> <span class=\"o\">=<\/span> <span class=\"s\">192.168.2.0\/24<\/span>\n<span class=\"na\">Endpoint<\/span> <span class=\"o\">=<\/span> <span class=\"s\">&lt;ip&gt;:&lt;port&gt;<\/span>\n<\/pre>\n<\/div>\n<p>The <code>AllowedIPs<\/code> mask let you specify how much you want to route over the <em>vpn<\/em>.<br \/>\nBy setting <code>0.0.0.0\/0<\/code> you tell you want to route ALL the traffic through it.<\/p>\n<blockquote><p><strong>NOTE<\/strong>: <code>Address<\/code> is a <code>wg-quick<\/code>-specific option.<\/p><\/blockquote>\n<h3>Using a configuration<\/h3>\n<p><code>wg-quick<\/code> is really simple to use, assuming you have created <code>\/etc\/wireguard\/wg0.conf<\/code>:<\/p>\n<div class=\"codehilite\">\n<pre>$ wg-quick up wg0\n<\/pre>\n<\/div>\n<div class=\"codehilite\">\n<pre>$ wg-quick down wg0\n<\/pre>\n<\/div>\n<p>If you are using <a href=\"https:\/\/wiki.gentoo.org\/wiki\/Netifrc\">netifrc<\/a> from version <code>0.6.1<\/code> wireguard is supported and you can have a configuration such as:<\/p>\n<div class=\"codehilite\">\n<pre>config_wg0=\"192.168.2.4\/24\"\nwireguard_wg0=\"\/etc\/wireguard\/wg0.conf\"\n<\/pre>\n<\/div>\n<p>With the <code>wg0.conf<\/code> file like the above but stripped of the <code>wg-quick<\/code>-specific options.<\/p>\n<h2>Summing up<\/h2>\n<p>Wireguard is a breeze to set up compared to nearly all the other <em>vpn<\/em> solutions.<\/p>\n<p>Non-linux systems can currently use a <a href=\"https:\/\/git.zx2c4.com\/wireguard-go\/about\/\">go implementation<\/a> and in the future a <a href=\"https:\/\/git.zx2c4.com\/wireguard-rs\/about\/\">rust implementation<\/a> (help welcome).<\/p>\n<p><a href=\"https:\/\/play.google.com\/store\/apps\/details?id=com.wireguard.android&amp;hl=en_US\">Android<\/a> and <a href=\"https:\/\/itunes.apple.com\/us\/app\/wireguard\/id1451685025\">macos<\/a> have already some pretty front-ends that make the setup easy even on those platforms.<\/p>\n<p>I hope you enjoyed it \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>wireguard is a modern, secure and fast vpn tunnel that is extremely simple to setup and works already nearly everywhere. Since I spent a little bet to play with it because this looked quite interesting, I thought of writing a small tutorial. I normally use Gentoo (and macos) so this guide is about Gentoo. General &hellip; <a href=\"https:\/\/blogs.gentoo.org\/lu_zero\/2019\/05\/03\/using-wireguard\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using Wireguard<\/span><\/a><\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[1],"tags":[35,36],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1aGWH-bU","_links":{"self":[{"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/posts\/738"}],"collection":[{"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/comments?post=738"}],"version-history":[{"count":2,"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/posts\/738\/revisions"}],"predecessor-version":[{"id":740,"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/posts\/738\/revisions\/740"}],"wp:attachment":[{"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/media?parent=738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/categories?post=738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.gentoo.org\/lu_zero\/wp-json\/wp\/v2\/tags?post=738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}