{"id":116,"date":"2021-01-05T15:30:45","date_gmt":"2021-01-05T15:30:45","guid":{"rendered":"https:\/\/dillonfletcher.co.uk\/?p=116"},"modified":"2021-01-05T16:06:23","modified_gmt":"2021-01-05T16:06:23","slug":"using-a-free-vps-as-a-dedicated-ip-for-virtual-machines","status":"publish","type":"post","link":"https:\/\/dillonfletcher.co.uk\/?p=116","title":{"rendered":"Using a free VPS as a dedicated IP for virtual machines."},"content":{"rendered":"\n<script src=\"https:\/\/cdn.lr-ingest.io\/LogRocket.min.js\" crossorigin=\"anonymous\"><\/script>\n<script>window.LogRocket && window.LogRocket.init('zxjtt2\/dillonfletchercouk');<\/script>\n\n\n\n<p class=\"wp-block-paragraph\">I recently wanted to setup an Ubuntu virtual machine on my <a href=\"https:\/\/www.proxmox.com\/en\/\">Proxmox<\/a> server so that my friends and I could share a server for hosting things on. The issue was that I am currently using the default ports for the services we wanted to run on my existing internet connection. So the obvious options were as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Run the new services on non-default ports.<\/li><li>Pay for a second internet connection\/second IP address.<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This either costs money, or causes unneccesary hassel. This is where the VPS comes in, im using an Amazon Web Services t2.micro instance based in London, (as close as possible to home to avoid performance issues), but you can use any server with a dedicated IP or DMZ which you aren&#8217;t using for any other services, ideally with a dedicated IP so not to cause double NAT issues.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, to start, I&#8217;m assuming you have both your client (my local VM) and your server (my remote VPS) setup with Ubuntu and connected to the internet. On <strong>both<\/strong> machines, begin by installing <a href=\"https:\/\/www.wireguard.com\/\">wireguard<\/a>, we are going to use wireguard to create a VPN tunnel between the VPS (server) and the VM (client).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt update\n$ sudo apt install wireguard<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now we need to generate a public and private key. This command uses generates a private key, with the &#8216;wg genkey&#8217; command, creates a file called privatekey, then pipes the output of &#8216;wg genkey&#8217; into &#8216;wg pubkey&#8217;, which takes the private key and outputs a matching public key, again, run the following command on <strong>both<\/strong> the client and the server.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ wg genkey | sudo tee \/etc\/wireguard\/privatekey | wg pubkey | sudo tee \/etc\/wireguard\/publickey<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You should now have 2 files in &#8216;\/etc\/wireguard&#8217;, &#8216;publickey&#8217;, and &#8216;privatekey&#8217;. You can view them using the &#8216;cat&#8217; command. You will need them later.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We now need to begin creating our config files for the wireguard VPN interface. We can start on the server, begin by running:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo vim \/etc\/wireguard\/wg0.conf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This creates the file wg0.conf in \/etc\/wireguard, in the command above im using vim, but you can use your preferred text editor. In the file you should insert the following, making sure to replace the <strong>PublicKey with the public key from the client<\/strong>, and <strong>PrivateKey with the private key from the server<\/strong>. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Interface]\nAddress = 10.66.66.1\/32\nListenPort = 51820\nPrivateKey = &lt;Server PrivateKey here&gt;\nPostUp = iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; ip6tables -t nat -A POSTROUTING -o eth0 -j MASQUERADE\nPostDown = iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE\n\n&#91;Peer]\nPublicKey = &lt;Client PublicKey here&gt;\nAllowedIPs = 10.66.66.2\/32<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The &#8216;PostUp&#8217; and &#8216;PostDown&#8217; sections enable NAT on the VPN to allow the client to connect to the internet via the server.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now open the same file on the <strong>client<\/strong> but instead insert this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Interface]\nPrivateKey = &lt;Client PrivateKey here&gt;\nAddress = 10.66.66.2\/32\n\n&#91;Peer]\nPublicKey = &lt;Server Privatekey here&gt;\nEndpoint = &lt;Server IP Address Here&gt;:51820\nAllowedIPs = 0.0.0.0\/0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Same thing as before, but we need to set the endpoint to the server, and the PrivateKey and PublicKey are swapped.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now run on both the client and the server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo wg-quick up wg0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This will set the interface &#8216;wg0&#8217; up and start wireguard, creating a tunnel between both machines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can check the connection is successful by seeing if you can ping the client from the server, run this on the client:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ping 10.66.66.1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you did everything correctly, you should see the following output!:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PING 10.66.66.1 (10.66.66.1) 56(84) bytes of data.\n64 bytes from 10.66.66.1: icmp_seq=1 ttl=64 time=14.6 ms\n64 bytes from 10.66.66.1: icmp_seq=2 ttl=64 time=15.3 ms\n64 bytes from 10.66.66.1: icmp_seq=3 ttl=64 time=15.5 ms\n64 bytes from 10.66.66.1: icmp_seq=4 ttl=64 time=15.5 ms\n--- 10.66.66.1 ping statistics ---\n4 packets transmitted, 4 received, 0% packet loss, time 3005ms\nrtt min\/avg\/max\/mdev = 14.612\/15.224\/15.500\/0.358 ms<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can also use &#8216;wg show&#8217; to check the connection. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ wg show\ninterface: wg0\n  public key: (hidden)\n  private key: (hidden)\n  listening port: 54832\n  fwmark: 0xca6c\n\npeer: \n  endpoint: (hidden)\n  allowed ips: 0.0.0.0\/0, ::\/0\n  latest handshake: 1 minute, 20 seconds ago\n  transfer: 663.55 MiB received, 87.98 MiB sent<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">On <strong>both<\/strong> the client and the server, run this command to start up the VPN on boot.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo systemctl enable wg-quick@wg0<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Almost there!<\/strong> We are done on the client now, we just need to enable some iptable rules on the server, to forward all ports from the server to the client via the VPN tunnel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run this script on the server:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo iptables -A FORWARD -i eth0 -o wg0 -p tcp --syn --dport 1:21 -m conntrack --ctstate NEW -j ACCEPT\nsudo iptables -A FORWARD -i eth0 -o wg0 -p tcp --syn --dport 23:51819 -m conntrack --ctstate NEW -j ACCEPT\nsudo iptables -A FORWARD -i eth0 -o wg0 -p tcp --syn --dport 51821:65535 -m conntrack --ctstate NEW -j ACCEPT\nsudo iptables -A FORWARD -i eth0 -o wg0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT\nsudo iptables -A FORWARD -i wg0 -o eth0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT\nsudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 1:21 -j DNAT --to-destination 10.66.66.2\nsudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 23:51819 -j DNAT --to-destination 10.66.66.2\nsudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 51821:65535 -j DNAT --to-destination 10.66.66.2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And thats it! Thanks for reading, the only things you need to know now are:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The only ports you won&#8217;t be able to use now are 51820, and 22, because they are in use by the server.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>You should now be able to access the clients ports and services on the IP address that was assigned to the server!<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>I would highly reccommend installing UFW firewall on the client.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>DO NOT<\/strong> use these rules in a production environment! I am using a disposable VPS<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>If you want to be extra safe you should manually forward the ports you need from the server.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently wanted to setup an Ubuntu virtual machine on my Proxmox server so that my friends and I could share a server for hosting things on. The issue was that I am currently using the default ports for the services we wanted to run on my existing internet connection. So the obvious options were [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[5,4,6,7],"class_list":["post-116","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-linux","tag-system-administration","tag-ubuntu","tag-vpn"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using a free VPS as a dedicated IP for virtual machines. - Dillon Fletcher<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/dillonfletcher.co.uk\/?p=116\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using a free VPS as a dedicated IP for virtual machines. - Dillon Fletcher\" \/>\n<meta property=\"og:description\" content=\"I recently wanted to setup an Ubuntu virtual machine on my Proxmox server so that my friends and I could share a server for hosting things on. The issue was that I am currently using the default ports for the services we wanted to run on my existing internet connection. So the obvious options were [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dillonfletcher.co.uk\/?p=116\" \/>\n<meta property=\"og:site_name\" content=\"Dillon Fletcher\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-05T15:30:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-05T16:06:23+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/dillonfletcher.co.uk\/?p=116\",\"url\":\"https:\/\/dillonfletcher.co.uk\/?p=116\",\"name\":\"Using a free VPS as a dedicated IP for virtual machines. - Dillon Fletcher\",\"isPartOf\":{\"@id\":\"https:\/\/dillonfletcher.co.uk\/#website\"},\"datePublished\":\"2021-01-05T15:30:45+00:00\",\"dateModified\":\"2021-01-05T16:06:23+00:00\",\"author\":{\"@id\":\"https:\/\/dillonfletcher.co.uk\/#\/schema\/person\/93633b34164a44661bb41e8a436d5535\"},\"breadcrumb\":{\"@id\":\"https:\/\/dillonfletcher.co.uk\/?p=116#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/dillonfletcher.co.uk\/?p=116\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/dillonfletcher.co.uk\/?p=116#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/dillonfletcher.co.uk\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using a free VPS as a dedicated IP for virtual machines.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/dillonfletcher.co.uk\/#website\",\"url\":\"https:\/\/dillonfletcher.co.uk\/\",\"name\":\"Dillon Fletcher - Personal Site\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/dillonfletcher.co.uk\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/dillonfletcher.co.uk\/#\/schema\/person\/93633b34164a44661bb41e8a436d5535\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/dillonfletcher.co.uk\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/7711d63308d3f4b9ce89cffa22159871b500b5bf670d38c80d828722cf42006a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/7711d63308d3f4b9ce89cffa22159871b500b5bf670d38c80d828722cf42006a?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/dillonfletcher.co.uk\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using a free VPS as a dedicated IP for virtual machines. - Dillon Fletcher","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/dillonfletcher.co.uk\/?p=116","og_locale":"en_US","og_type":"article","og_title":"Using a free VPS as a dedicated IP for virtual machines. - Dillon Fletcher","og_description":"I recently wanted to setup an Ubuntu virtual machine on my Proxmox server so that my friends and I could share a server for hosting things on. The issue was that I am currently using the default ports for the services we wanted to run on my existing internet connection. So the obvious options were [&hellip;]","og_url":"https:\/\/dillonfletcher.co.uk\/?p=116","og_site_name":"Dillon Fletcher","article_published_time":"2021-01-05T15:30:45+00:00","article_modified_time":"2021-01-05T16:06:23+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/dillonfletcher.co.uk\/?p=116","url":"https:\/\/dillonfletcher.co.uk\/?p=116","name":"Using a free VPS as a dedicated IP for virtual machines. - Dillon Fletcher","isPartOf":{"@id":"https:\/\/dillonfletcher.co.uk\/#website"},"datePublished":"2021-01-05T15:30:45+00:00","dateModified":"2021-01-05T16:06:23+00:00","author":{"@id":"https:\/\/dillonfletcher.co.uk\/#\/schema\/person\/93633b34164a44661bb41e8a436d5535"},"breadcrumb":{"@id":"https:\/\/dillonfletcher.co.uk\/?p=116#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dillonfletcher.co.uk\/?p=116"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/dillonfletcher.co.uk\/?p=116#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/dillonfletcher.co.uk\/"},{"@type":"ListItem","position":2,"name":"Using a free VPS as a dedicated IP for virtual machines."}]},{"@type":"WebSite","@id":"https:\/\/dillonfletcher.co.uk\/#website","url":"https:\/\/dillonfletcher.co.uk\/","name":"Dillon Fletcher - Personal Site","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/dillonfletcher.co.uk\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/dillonfletcher.co.uk\/#\/schema\/person\/93633b34164a44661bb41e8a436d5535","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/dillonfletcher.co.uk\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/7711d63308d3f4b9ce89cffa22159871b500b5bf670d38c80d828722cf42006a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/7711d63308d3f4b9ce89cffa22159871b500b5bf670d38c80d828722cf42006a?s=96&d=mm&r=g","caption":"admin"},"sameAs":["http:\/\/dillonfletcher.co.uk"]}]}},"_links":{"self":[{"href":"https:\/\/dillonfletcher.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/116","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dillonfletcher.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dillonfletcher.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dillonfletcher.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dillonfletcher.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=116"}],"version-history":[{"count":4,"href":"https:\/\/dillonfletcher.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions"}],"predecessor-version":[{"id":121,"href":"https:\/\/dillonfletcher.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/116\/revisions\/121"}],"wp:attachment":[{"href":"https:\/\/dillonfletcher.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dillonfletcher.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dillonfletcher.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}