{"id":101419,"date":"2024-02-13T15:13:06","date_gmt":"2024-02-13T15:13:06","guid":{"rendered":"https:\/\/linuxiac.com\/?p=101419"},"modified":"2024-02-13T15:24:33","modified_gmt":"2024-02-13T15:24:33","slug":"how-to-add-static-entries-to-hosts-file-on-linux","status":"publish","type":"post","link":"https:\/\/linuxiac.com\/how-to-add-static-entries-to-hosts-file-on-linux\/","title":{"rendered":"How to Add Static Entries to the Hosts File on Linux"},"content":{"rendered":"\n<p>Are you looking to take control of your domain name resolution? You&#8217;ve come to the right place! In this guide, we&#8217;ll walk you through the simple steps of adding static entries to the hosts file on Linux.<\/p>\n\n\n\n<p>Mastering the manipulation of the hosts file is a valuable skill. This seemingly small file holds the power to redirect network traffic, block unwanted sites, or even create shortcuts to your favorite servers. But before we go any further, we need to introduce some theory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-what-is-the-linux-hosts-file\">What Is the Linux Hosts File?<\/h2>\n\n\n\n<p>The hosts file is a plain text file that almost every operating system, including all flavors of Linux, uses to map hostnames (like example.com) to IP addresses. This file serves as a local override for DNS lookups.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file01.jpg\"><img decoding=\"async\" width=\"950\" height=\"329\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file01.jpg\" alt=\"The &quot;\/etc\/hosts&quot; file on Linux.\" class=\"wp-image-101476\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file01.jpg 950w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file01-380x132.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file01-768x266.jpg 768w\" sizes=\"(max-width: 950px) 100vw, 950px\" \/><\/a><figcaption class=\"wp-element-caption\">The &#8220;\/etc\/hosts&#8221; file on Linux.<\/figcaption><\/figure>\n\n\n\n<p>On Linux systems, the hosts file is typically located at &#8220;<em><strong>\/etc\/hosts<\/strong><\/em>&#8220;. This path makes it easy for system administrators and users to find and edit it when necessary. Accessing it requires root privileges, ensuring only authorized users can make changes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How Does the Hosts File Work?<\/h3>\n\n\n\n<p>When you enter a URL into your browser, try connecting to an <a href=\"https:\/\/linuxiac.com\/ssh-login-without-password\/\">SSH remote server<\/a> using a hostname, etc., your system uses the Domain Name System (DNS) to translate the web address or hostname into an IP address that computers can understand.<\/p>\n\n\n\n<p>However, if an entry exists in the hosts file for that address, the system uses the specified IP address from the hosts file instead of performing a DNS lookup. This process speeds up address resolution and allows for custom domain mappings.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"892\" height=\"709\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/how-linux-etc-host-file-work.png\" alt=\"Linux hosts file\" class=\"wp-image-101467\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/how-linux-etc-host-file-work.png 892w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/how-linux-etc-host-file-work-380x302.png 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/how-linux-etc-host-file-work-768x610.png 768w\" sizes=\"(max-width: 892px) 100vw, 892px\" \/><\/figure>\n\n\n\n<p>In simpler terms, when you try to visit a website, your computer will check the hosts file first to see if there&#8217;s a corresponding IP address there before asking the wider internet.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-top-use-cases\">Top Use Cases<\/h2>\n\n\n\n<p>Adding static entries to your host&#8217;s file on Linux can be super handy for a few reasons: all about controlling how your computer finds and connects to websites or networked devices:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Testing and Development<\/strong>: Developers working on websites that are not yet live can map the domain name of their project to the IP address of a local or staging server. This allows you to see how the website will look on the real internet without it actually being there.<\/li>\n\n\n\n<li><strong>Override DNS information<\/strong>: In cases where DNS information is incorrect or has not propagated yet, adding static entries to your host&#8217;s file can temporarily override DNS settings to allow you to access the correct server.<\/li>\n\n\n\n<li><strong>Block Unwanted Sites<\/strong>: If you want to keep certain websites off-limits, redirect their domain names to a \u201cdead end\u201d like 0.0.0.0. This way, when you try to visit them, your browser just can\u2019t find the way.<\/li>\n\n\n\n<li><strong>Speed Up Access<\/strong>: Your computer can find the website faster because it doesn&#8217;t have to ask for the address; it&#8217;s already written down in your local &#8220;<em>\/etc\/hosts<\/em>&#8221; file.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-add-static-entries-to-the-hosts-file-on-linux\">Add Static Entries to the Hosts File on Linux<\/h2>\n\n\n\n<p>To add entries to the hosts file, you need to edit it. This process requires administrative (sudo) privileges on Linux, as the hosts file is a system file protected from regular user changes.<\/p>\n\n\n\n<p>Open the &#8220;<em>\/etc\/hosts<\/em>&#8221; file using a <a href=\"https:\/\/linuxiac.com\/best-linux-terminal-text-editors-for-beginners\/\">terminal text editor<\/a> you&#8217;re comfortable with. Once the file is open, you&#8217;ll see existing entries. These typically include references to localhost.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code lang=\"bash\" class=\"hljs language-bash\">sudo nano \/etc\/hosts<\/code><\/span><\/pre>\n\n\n<div style=\"height:20px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>To add a new entry, navigate to the end of the file and enter the IP address, followed by at least one space or tab, and then the hostname you wish to map to that IP. For example, to map the &#8220;<em>example.com<\/em>&#8221; domain to the IP address &#8220;<em>10.0.1.69<\/em>,&#8221; the final version should look like this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file02.jpg\"><img decoding=\"async\" width=\"955\" height=\"448\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file02.jpg\" alt=\"Mapping a domain name to the IP address in the &quot;\/etc\/hosts&quot; file on Linux.\" class=\"wp-image-101485\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file02.jpg 955w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file02-380x178.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file02-768x360.jpg 768w\" sizes=\"(max-width: 955px) 100vw, 955px\" \/><\/a><figcaption class=\"wp-element-caption\">Mapping a domain name to the IP address in the &#8220;\/etc\/hosts&#8221; file on Linux.<\/figcaption><\/figure>\n\n\n\n<p>After adding your entries, save the changes and exit the editor. Now, it&#8217;s time to test your changes. You can ping the hostname you added to ensure your new mapping works. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code lang=\"bash\" class=\"hljs language-css language-bash\"><span class=\"hljs-selector-tag\">ping<\/span> <span class=\"hljs-selector-tag\">-c<\/span> 3 <span class=\"hljs-selector-tag\">example<\/span><span class=\"hljs-selector-class\">.com<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file03.jpg\"><img decoding=\"async\" width=\"949\" height=\"361\" src=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file03.jpg\" alt=\"Testing a local domain mapping.\" class=\"wp-image-101494\" srcset=\"https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file03.jpg 949w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file03-380x145.jpg 380w, https:\/\/linuxiac.com\/wp-content\/uploads\/2024\/02\/etc-hosts-file03-768x292.jpg 768w\" sizes=\"(max-width: 949px) 100vw, 949px\" \/><\/a><figcaption class=\"wp-element-caption\">Testing a local domain mapping.<\/figcaption><\/figure>\n\n\n\n<p>If everything is set up correctly, the ping command should resolve the hostname to the IP address you specified in the &#8220;<em>\/etc\/hosts<\/em>&#8221; file, proving that your system is now using the local hosts file entry for this hostname.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h2>\n\n\n\n<p>Adding entries to the hosts file in Linux is straightforward and invaluable for testing purposes. Following the steps outlined above, you can add static entries to the hosts file, allowing for precise control over hostname resolution on your system.<\/p>\n\n\n\n<p>Thanks for  your time! For more in-depth information, consult hosts file&nbsp;<a href=\"https:\/\/man7.org\/linux\/man-pages\/man5\/hosts.5.html\" target=\"_blank\" rel=\"noreferrer noopener\">manual page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to add static entries to the Linux hosts file to achieve precise control over domain mapping. Quick and simple guide!<\/p>\n","protected":false},"author":10,"featured_media":101425,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,4417],"tags":[3272,4470],"class_list":["post-101419","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips","category-linux-knowledge","tag-dns","tag-hostname"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/posts\/101419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/comments?post=101419"}],"version-history":[{"count":0,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/posts\/101419\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/media\/101425"}],"wp:attachment":[{"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/media?parent=101419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/categories?post=101419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxiac.com\/wp-json\/wp\/v2\/tags?post=101419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}