{"id":1400,"date":"2021-12-16T12:52:29","date_gmt":"2021-12-16T09:52:29","guid":{"rendered":"https:\/\/files2.tojikon.net\/files-cloud\/2021\/07\/28\/how-to-edit-the-hosts-file-on-linux\/https:\/\/files2.tojikon.net\/files-cloud\/2021\/07\/28\/how-to-edit-the-hosts-file-on-linux\/"},"modified":"2021-12-16T12:52:29","modified_gmt":"2021-12-16T09:52:29","slug":"how-to-edit-the-hosts-file-on-linux","status":"publish","type":"post","link":"https:\/\/tojikon.net\/en\/1400-how-to-edit-the-hosts-file-on-linux\/","title":{"rendered":"How to edit the Hosts file on Linux"},"content":{"rendered":"<section id=\"related_posts\">\n<div class=\"block-head\">\n<h3>Related Articles<\/h3>\n<\/div>\n<\/section>\n<p>The Hosts file on Linux is responsible for mapping hostnames and IP addresses. It\u2019s a plain text file named \u201cHosts.\u201d If you\u2019ve ever run servers on Linux, you\u2019ll no doubt find yourself editing this file a lot.\u00a0<\/p>\n<p>In this guide, we\u2019ll show you how to access the Hosts file on Linux and how to back it up too. To start, ensure you have access to the root account. The Hosts file is a system-level file and cannot be accessed by a regular user.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-406109 size-full\" src=\"https:\/\/files2.tojikon.net\/files-cloud\/wp-content\/uploads\/2021\/12\/how-to-edit-the-hosts-file-on-linux.png\" alt=\"\" width=\"1200\" height=\"608\"\/><\/p>\n<h2>Making a backup of the Hosts file<\/h2>\n<p>If you plan to edit the Hosts file, it is good to create a backup of the original beforehand. That way, if you make changes to the file and want to revert to how things were before the edits, you don\u2019t have to re-install your system.<\/p>\n<p>To make a backup of the Hosts file, start by entering the<code>\/etc\/<\/code>\u00a0folder using the\u00a0<strong>CD\u00a0<\/strong>command below in the terminal window.<\/p>\n<pre>\ncd \/etc\/\n<\/pre>\n<p>Inside of the <code>\/etc\/<\/code> folder, make a copy of the Hosts file using the\u00a0<strong>cp\u00a0<\/strong>command. Copy the file to your home folder (~) as \u201cHosts.bak\u201d.<\/p>\n<pre>\ncp hosts ~\/hosts.bak\n<\/pre>\n<p>Once you\u2019ve made a backup of the file, close the terminal. Keep the \u201cHosts.bak\u201d file safe on a spare hard drive, flash drive, or Dropbox account.\u00a0<\/p>\n<h3>How to restore your backup<\/h3>\n<p>If you\u2019ve made edits to the Hosts files and need to restore them, here\u2019s how to do it. First, open up a terminal window. Then, with the terminal open, access the area where your \u201chosts.bak\u201d file is saved.<\/p>\n<p>For example, if you\u2019ve stored your \u201chosts.bak\u201d file in \/home\/USERNAME\/Documents, you\u2019d do the following to access it.<\/p>\n<pre>\ncd ~\/Documents\/\n<\/pre>\n<p>Once the terminal is where the \u201chosts.bak\u201d file is, log into the root account using <strong>sudo -s<\/strong>.\u00a0<\/p>\n<pre>\nsudo -s\n<\/pre>\n<p>Delete the existing file using the\u00a0<strong>rm\u00a0<\/strong>command.\u00a0<\/p>\n<pre>\nrm hosts\n<\/pre>\n<p>After deleting the original host file, restore the backup to your computer using the\u00a0<strong>cp\u00a0<\/strong>command. Using\u00a0<strong>cp\u00a0<\/strong>will allow you to create a copy of the backup file and place it in <code>\/etc\/<\/code>while keeping the backup where it is stored.<\/p>\n<pre>\ncp hosts.bak \/etc\/\n<\/pre>\n<p>When the copying process is complete, the original host file will be restored. From here, reboot your Linux PC. When your Linux PC turns back on, you\u2019ll be using your original hosts in the Hosts file.<\/p>\n<h2>Accessing the Linux Hosts file\u00a0<\/h2>\n<p>If you want to edit the Linux Hosts file, you\u2019ll need to access it first. The file is located in the <code>\/etc\/<\/code> folder, but a normal Linux user account can\u2019t access this file, as it is a system file.<\/p>\n<p>You can either log in with the <strong>su\u00a0<\/strong>command or use the <strong>sudo -s\u00a0<\/strong>command to access the root account. Generally, most Linux systems will be able to access root using the <strong>su\u00a0<\/strong>command. However, on Ubuntu and some other distributions, the root account is turned off, so you need to use <strong>sudo -s<\/strong>.<\/p>\n<p>First, open up a terminal window on the Linux desktop. Then, open up a terminal window on the Linux desktop and press <strong>Ctrl + Alt + T\u00a0<\/strong>on the keyboard. Or, search for \u201cTerminal\u201d in the app menu and launch it that way.<\/p>\n<p>Once the terminal is open, enter either of the two commands below to access the root account on your Linux PC.<\/p>\n<pre>\nsu -\n<\/pre>\n<p>Or, if the root account on your Linux system is disabled,\u00a0you can access root by elevating the session using <strong>sudo -s<\/strong>.<\/p>\n<pre>\nsudo -s\n<\/pre>\n<p>When you\u2019ve logged into root, use the\u00a0<strong>CD\u00a0<\/strong>command to access the <code>\/etc\/<\/code> folder.\u00a0<\/p>\n<pre>\ncd \/etc\/\n<\/pre>\n<p>Inside of the folder, open up the \u201cHosts\u201d file using the Nano text editor. In this tutorial, we will be using Nano. However, the \u201cHosts\u201d file can be opened with any text editor you wish.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-406109 size-full\" src=\"https:\/\/files2.tojikon.net\/files-cloud\/wp-content\/uploads\/2021\/12\/how-to-edit-the-hosts-file-on-linux.png\" alt=\"\" width=\"1200\" height=\"608\"\/><\/p>\n<pre>\nnano -w hosts\n<\/pre>\n<h2>Editing the Hosts file<\/h2>\n<p>To edit the Hosts file, you\u2019ll need to understand how it works. Each entry has an IP address and a hostname. Look through the file in the Nano text editor and take a look at the lines inside.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-406096 size-full\" src=\"https:\/\/files2.tojikon.net\/files-cloud\/wp-content\/uploads\/2021\/12\/how-to-edit-the-hosts-file-on-linux-1.png\" alt=\"\" width=\"1200\" height=\"661\"\/><\/p>\n<p>For example, a line in the Hosts file might look like the code below.<\/p>\n<p><code>192.168.1.205 my.hostname<\/code><\/p>\n<p>If you want to add new lines to the Hosts file, make a new line at the bottom and type out the IP address and the hostname. Then, save the edits in the Nano text editor by pressing <strong>Ctrl + O\u00a0<\/strong>on the keyboard.<\/p>\n<p>Feel free to add as many new lines as you wish to the host file. When you\u2019ve finished the editing process, press the\u00a0<strong>Crl + X<\/strong> button combination to exit the Nano text editor.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Related Articles The Hosts file on Linux is responsible for mapping hostnames and IP addresses. It\u2019s a plain text file named \u201cHosts.\u201d If you\u2019ve ever run servers on Linux, you\u2019ll no doubt find yourself editing this file a lot.\u00a0 In this guide, we\u2019ll show you how to access the Hosts file on Linux and how &hellip;<\/p>\n","protected":false},"author":1,"featured_media":1401,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[33],"_links":{"self":[{"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/posts\/1400"}],"collection":[{"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/comments?post=1400"}],"version-history":[{"count":0,"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/posts\/1400\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/media\/1401"}],"wp:attachment":[{"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/media?parent=1400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/categories?post=1400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tojikon.net\/en\/wp-json\/wp\/v2\/tags?post=1400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}