{"id":276,"date":"2020-03-05T16:58:23","date_gmt":"2020-03-05T14:58:23","guid":{"rendered":"https:\/\/binaryvikings.io\/?p=276"},"modified":"2020-07-26T09:27:52","modified_gmt":"2020-07-26T07:27:52","slug":"setting-up-ddns-with-bash-script-linux","status":"publish","type":"post","link":"https:\/\/binaryvikings.io\/?p=276","title":{"rendered":"Setting up DDNS with bash script Linux"},"content":{"rendered":"<h2>Dynamic DNS, Y dis?<\/h2>\n<p>Setting up Dynamic DNS from eg your home or small business office can come in very handy if your ISP changes your IP often. Some ISPs either don&#8217;t offer static IPs, or they simply charge too much for it. You may also run a game server and if you already own a domain, you can have people connecting to <strong>myexamplegame.gg<\/strong> instead of an IP.<\/p>\n<p>So here&#8217;s a quick guide on how to set up a script that will allow you to update your DNS records automatically from a local server. I&#8217;ll be basing this on UnoEuro&#8217;s setup, but I assume most domain providers are similar.<\/p>\n<h2>Example #1 &#8211; Use eth0 IP<\/h2>\n<p>According to Simply.com &#8211; former UnoEuro&#8217;s <a href=\"https:\/\/www.unoeuro.com\/en\/docs\/ddns\/\">documentation<\/a>, they allow you to update DNS every <em>15 minutes<\/em>. Remember to check this at the domain registrar you&#8217;re using, as you don&#8217;t want to spam their service and risk getting a timeout\/ban.<\/p>\n<div class=\"hcb_wrap\">\n<pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>#!\/bin\/bash \n#Return your current IP (Leave the space after 'inet ', to make sure you don't get IPV6 as well. \nifconfig eth0 | awk '\/inet \/ {gsub(\"addr:\", \"\", $2); print $2}' curl -s -L \"https:\/\/api.unoeuro.com\/ddns.php?apikey=YOURAPI&amp;domain=YOURDOMAIN.COM&amp;hostname=dev&amp;myip=$2\"<\/code><\/pre>\n<\/div>\n<p><strong><em>apikey<\/em> <\/strong>&#8211; Find this in your control panel.\u00a0<em><strong>domain<\/strong><\/em><em> &#8211; is your domain.\u00a0<\/em><em><strong>hostname<\/strong><\/em> &#8211; is your subdomain, eg (dev.yourdomain.com), like shown above.<\/p>\n<h2>Example #2 &#8211; Use public IP<\/h2>\n<p><!--?prettify linenums=true?--><\/p>\n<pre class=\"prettyprint\">#!\/bin\/bash\n\nip=$(curl -s ifconfig.co)\ncurl -s -L \"https:\/\/api.simply.com\/ddns.php?apikey=YOURAPI&amp;domain=YOURDOMAIN.COM&amp;hostname=dev&amp;myip=$ip\"<\/pre>\n<h2>Example #3 &#8211; The Unoeuro way for public IP<\/h2>\n<p><!--?prettify linenums=true?--><\/p>\n<pre class=\"prettyprint\">#!\/bin\/bash\n\ncurl -s -L \"https:\/\/api.simply.com\/ddns.php?apikey=YOURAPI&amp;domain=YOURDOMAIN.COM&amp;hostname=dev\"<\/pre>\n<p>The\u00a0<strong>IP field<\/strong> is optional, at least at Simply, which means that by leaving out the IP field, it will default to the public IP of the machine making the request. (This might also be the case with other DNS suppliers).<\/p>\n<p>When you&#8217;ve made your bash script with the code above, make sure you run <strong>chmod +x scriptname.sh<\/strong><\/p>\n<p>Also note down where you save the file, as you&#8217;ll need the full path with filename when you run the script.<\/p>\n<h3>Cronjob<\/h3>\n<p>The easiest way to run the script, is probably using a cronjob, since it&#8217;s probably not super important if the task is skipped once or twice. Even though I&#8217;m allowed to update DNS every 15 minutes, I&#8217;ve set mine to 20 minutes, just to have 5 extra minutes to avoid spamming.<\/p>\n<ol>\n<li>Type the following on your Linux machine.\n<pre class=\"prettyprint\">crontab -e<\/pre>\n<\/li>\n<li>Now add the following line at the bottom of the crontab window.\n<pre class=\"prettyprint\">*\/20 * * * * \/customscripts\/scriptname.sh<\/pre>\n<\/li>\n<li>Save the cronjob and you&#8217;re good to go.<\/li>\n<\/ol>\n<p>If you want to update every hour, once a day or some other time, I recommend generating the string you need, using this website:\u00a0<a href=\"https:\/\/crontab.guru\/every-15-minutes\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/crontab.guru\/<\/a><\/p>\n<p>Good luck!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Dynamic DNS, Y dis? Setting up Dynamic DNS from eg your home or small business office can come in very handy if your ISP changes<\/p>\n","protected":false},"author":1,"featured_media":258,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":"","footnotes":""},"categories":[12,19,20],"tags":[],"class_list":["post-276","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-guides","category-linux","category-scripts"],"_links":{"self":[{"href":"https:\/\/binaryvikings.io\/index.php?rest_route=\/wp\/v2\/posts\/276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/binaryvikings.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/binaryvikings.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/binaryvikings.io\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/binaryvikings.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=276"}],"version-history":[{"count":7,"href":"https:\/\/binaryvikings.io\/index.php?rest_route=\/wp\/v2\/posts\/276\/revisions"}],"predecessor-version":[{"id":321,"href":"https:\/\/binaryvikings.io\/index.php?rest_route=\/wp\/v2\/posts\/276\/revisions\/321"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/binaryvikings.io\/index.php?rest_route=\/wp\/v2\/media\/258"}],"wp:attachment":[{"href":"https:\/\/binaryvikings.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/binaryvikings.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/binaryvikings.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}