&NewLine;<h2 class&equals;"wp-block-heading">Introduction<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p>The basic advice you&&num;8217&semi;ll see everywhere on the web is to always use a VPN for your torrenting needs&period; To avoid any possible way to track your client and its localization&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">VPN<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p>You have multiple VPN protocol that exists&comma; going from IPsec with LT2P&comma; OpenVPN to Wireguard&period; This guide is going to focus on Wireguard&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">Wireguard<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<blockquote class&equals;"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>WireGuard<sup>®<&sol;sup> is an extremely simple yet fast and modern VPN that utilizes <strong>state-of-the-art <a href&equals;"https&colon;&sol;&sol;www&period;wireguard&period;com&sol;protocol&sol;">cryptography<&sol;a><&sol;strong>&period; It aims to be <a href&equals;"https&colon;&sol;&sol;www&period;wireguard&period;com&sol;performance&sol;">faster<&sol;a>&comma; <a href&equals;"https&colon;&sol;&sol;www&period;wireguard&period;com&sol;quickstart&sol;">simpler<&sol;a>&comma; leaner&comma; and more useful than IPsec&comma; while avoiding the massive headache&period; It intends to be considerably more performant than OpenVPN&period; WireGuard is designed as a general purpose VPN for running on embedded interfaces and super computers alike&comma; fit for many different circumstances&period; Initially released for the Linux kernel&comma; it is now cross-platform &lpar;Windows&comma; macOS&comma; BSD&comma; iOS&comma; Android&rpar; and widely deployable&period; It is currently under heavy development&comma; but already it might be regarded as the most secure&comma; easiest to use&comma; and simplest VPN solution in the industry&period;<&sol;p><cite>WireGuard <a href&equals;"https&colon;&sol;&sol;www&period;wireguard&period;com&sol;">website<&sol;a><&sol;cite><&sol;blockquote>&NewLine;&NewLine;&NewLine;&NewLine;<p>This Guide is going to focus on how to set up a Wireguard Client&comma; a Network Namespace and having only your desired application having its traffic redirected to your VPN server &sol; provider&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">Network Namespace<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p>With network namespace you are able to create a full virtual network stack directly in your OS&period; This feature of the Linux kernel is used by Docker and other container engine&period; The idea being&comma; you can segregate a full network out of the one of your host&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>This tutorial is going to rely on this feature to be able to have a network that will redirect all its traffic to Wireguard VPN&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">Creating Namespace<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>First thing first&comma; we need to create the namespace&period; To interact with the namespace feature of the Linux Kernel we&&num;8217&semi;re going to use the <em>ip<&sol;em> tool&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-verse">Important tip&colon; all the commands we're going to use need to be run as <em>root<&sol;em>&period; Either run a shell with root&comma; or prepend them with <em>sudo&period;<&sol;em><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"EnlighterJSRAW" data-enlighter-language&equals;"shell" data-enlighter-theme&equals;"" data-enlighter-highlight&equals;"" data-enlighter-linenumbers&equals;"" data-enlighter-lineoffset&equals;"" data-enlighter-title&equals;"" data-enlighter-group&equals;"">ip netns add vpn<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">Creating Virtual Ethernet Interfaces<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>To be able to connect our root namespace where you have internet access to our VPN namespace where you don&&num;8217&semi;t have it&comma; we need a virtual interface&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>You can see below a quick schema with our 2 namespaces and the 2 virtual interfaces we&&num;8217&semi;re going to create&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-image"><img src&equals;"https&colon;&sol;&sol;www&period;aaflalo&period;me&sol;wp-content&sol;uploads&sol;2019&sol;08&sol;Namespaces&lowbar;veth&period;png" alt&equals;"" class&equals;"wp-image-1796"&sol;><figcaption>VPN Namespace connected to Root namespace with Virtual Ethernet Interface<&sol;figcaption><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"EnlighterJSRAW" data-enlighter-language&equals;"shell" data-enlighter-theme&equals;"" data-enlighter-highlight&equals;"" data-enlighter-linenumbers&equals;"" data-enlighter-lineoffset&equals;"" data-enlighter-title&equals;"" data-enlighter-group&equals;"">&num; create the interface&NewLine;ip link add v-eth1 type veth peer name v-peer1&NewLine;&NewLine;&num; add the v-peer1 to the namespace vpn&NewLine;ip link set v-peer1 netns vpn&NewLine;&NewLine;&num; set IP to the interface in root namespace&NewLine;ip addr add 10&period;200&period;1&period;1&sol;24 dev v-eth1&NewLine;&NewLine;&num; make the interface active&NewLine;ip link set v-eth1 up &NewLine;&NewLine;&num; add ip to the interface in the vpn namespace with a corresponding netmask&period;&NewLine;ip netns exec vpn ip addr add 10&period;200&period;1&period;2&sol;24 dev v-peer1 &NewLine;&NewLine;&num; make the interface active&NewLine;ip -n vpn link set v-peer1 up &NewLine;&NewLine; &num; add a loopback interface in vpn namespace&NewLine;ip -n vpn link set lo up&NewLine;&NewLine;&num; make the traffic in vpn namespace go to root namespace through veth&NewLine;ip -n vpn route add default via 10&period;200&period;1&period;1<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>Great&comma; now you have a namespace configured and a link between the two namespace&period; We need now to use iptables to let use forward traffic into our virtual interfaces&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">Traffic Forwarding between veth<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p>First you need to tell the kernel to enable IP Forwarding&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">Enable IP-forwarding&period;<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"EnlighterJSRAW" data-enlighter-language&equals;"generic" data-enlighter-theme&equals;"" data-enlighter-highlight&equals;"" data-enlighter-linenumbers&equals;"" data-enlighter-lineoffset&equals;"" data-enlighter-title&equals;"" data-enlighter-group&equals;"">echo 1 > &sol;proc&sol;sys&sol;net&sol;ipv4&sol;ip&lowbar;forward&NewLine;echo "net&period;ipv4&period;ip&lowbar;forward &equals; 1" >> &sol;etc&sol;sysctl&period;conf<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading">Iptables<&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"EnlighterJSRAW" data-enlighter-language&equals;"generic" data-enlighter-theme&equals;"" data-enlighter-highlight&equals;"" data-enlighter-linenumbers&equals;"" data-enlighter-lineoffset&equals;"" data-enlighter-title&equals;"" data-enlighter-group&equals;"">&NewLine;&num; Flush forward rules&comma; policy DROP by default&period;&NewLine;iptables -P FORWARD DROP&NewLine;iptables -F FORWARD&NewLine;&NewLine;&num; Flush nat rules&period;&NewLine;iptables -t nat -F&NewLine;&NewLine;&num; Enable masquerading of 10&period;200&period;1&period;0&period;&NewLine;iptables -t nat -A POSTROUTING -s 10&period;200&period;1&period;0&sol;24 -o eth0 -j MASQUERADE&NewLine;&NewLine;&num; Allow forwarding between eth0 and v-eth1&period;&NewLine;iptables -A FORWARD -i eth0 -o v-eth1 -j ACCEPT&NewLine;iptables -A FORWARD -o eth0 -i v-eth1 -j ACCEPT&NewLine;&NewLine;&num; Allow all output traffic&NewLine;iptables -P OUTPUT ACCEPT<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">DNS Configuration<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p>You can configure the DNS server that will be used by the applications in your namespace&period; In my case&comma; I&&num;8217&semi;m using those of CloudFlare&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"EnlighterJSRAW" data-enlighter-language&equals;"generic" data-enlighter-theme&equals;"" data-enlighter-highlight&equals;"" data-enlighter-linenumbers&equals;"" data-enlighter-lineoffset&equals;"" data-enlighter-title&equals;"" data-enlighter-group&equals;"">mkdir -p &sol;etc&sol;netns&sol;vpn&NewLine;echo "nameserver 1&period;1&period;1&period;1" > &sol;etc&sol;netns&sol;vpn&sol;resolv&period;conf&NewLine;echo "nameserver 1&period;0&period;0&period;1" >> &sol;etc&sol;netns&sol;vpn&sol;resolv&period;conf<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">Test namespace<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p>You should be able to ping the outside world now&period; If it doesn&&num;8217&semi;t work something must be wrong with your iptable configuration&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"EnlighterJSRAW" data-enlighter-language&equals;"generic" data-enlighter-theme&equals;"" data-enlighter-highlight&equals;"" data-enlighter-linenumbers&equals;"" data-enlighter-lineoffset&equals;"" data-enlighter-title&equals;"" data-enlighter-group&equals;"">ping 1&period;1&period;1&period;1<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">Wireguard Client<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p>Now that you have your VPN namespace that can access the internet&comma; we only need to use <em>wg-quick<&sol;em> tool provided by wiregard to initialize our connection to the server&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>You&&num;8217&semi;ll see the usual command prepended by <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>ip netns exec vpn<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>This is to tell the OS to use our namespace to run the wanted program&period; In this case&comma; wg-quick&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"EnlighterJSRAW" data-enlighter-language&equals;"generic" data-enlighter-theme&equals;"" data-enlighter-highlight&equals;"" data-enlighter-linenumbers&equals;"" data-enlighter-lineoffset&equals;"" data-enlighter-title&equals;"" data-enlighter-group&equals;"">ip netns exec vpn wg-quick up NAME&lowbar;OF&lowbar;CONFIG&lowbar;FILE<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h2 class&equals;"wp-block-heading">Run your client<&sol;h2>&NewLine;&NewLine;&NewLine;&NewLine;<p>And here is the last part&comma; I&&num;8217&semi;m sure you already have an idea on how to do this part&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Basically&comma; we want the OS to run our app the in VPN Network Namespace&comma; the same way we wanted wg-quick to run&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Only small difference&comma; we don&&num;8217&semi;t want the torrent client to run as root&comma; this is dangerous&period; We&&num;8217&semi;re going to use root to run the <em>ip<&sol;em> tool and <em>runuser<&sol;em> to run the application as the wanted user&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"EnlighterJSRAW" data-enlighter-language&equals;"shell" data-enlighter-theme&equals;"" data-enlighter-highlight&equals;"" data-enlighter-linenumbers&equals;"" data-enlighter-lineoffset&equals;"" data-enlighter-title&equals;"" data-enlighter-group&equals;"">sudo ip netns exec vpn runuser &dollar;USER -c "MY&lowbar;TORRENT&lowbar;CLIENT&lowbar;COMMAND"<&sol;pre>&NewLine;