How to edit the hosts file (Mac, Windows & Linux)

The hosts file is a text file that maps hostnames to IP addresses. Upon typing a url address on the browser, the system is checking if there is a relevant entry on the hosts file and gets the corresponding IP address, else it resolves the IP via the active connection’s DNS servers. The hosts file can be edited to block certain hostnames (like ad-serving/malicious hosts), or used for web development purposes, i.e. to redirect domains to local addresses.\r\n
\r\nThe hosts file will look something like this:\r\n
\r\n[jed_panel heading=”above” heading_msg=”Example hosts file” border_color=”#3399ff”]\r\n##\r\n# Host Database\r\n#\r\n# localhost is used to configure the loopback interface\r\n# when the system is booting. Do not change this entry.\r\n##\r\n127.0.0.1 localhost\r\n255.255.255.255 broadcasthost\r\n# Custom hosts details\r\n95.211.13.120 www.test.com\r\n[/jed_panel]\r\n
\r\n\r\n[jed_tabs tabs_position=”top”]\r\n[jed_tab title=”Mac OS X”]\r\nTo edit the hosts file on mac os x you have to open the hosts file in Terminal. Please follow the steps below to do this.\r\nbr/>\r\n

    \r\n

  1. Open Terminal
  2. \r\n

  3. Run the following command: sudo nano /private/etc/hosts
  4. \r\n

  5. Enter your administrator password en press enter
  6. \r\n

  7. You now see a new screen where you can enter the details
  8. \r\n

  9. Press Ctrl + O to safe your modification
  10. \r\n

  11. Press Ctrl + X to close nano
  12. \r\n

      \r\n[/jed_tab]\r\n[jed_tab title=”Windows”]\r\nIf you want to change the hosts file on windows you have to change the hosts file with notepad in administrator mode. Please make sure you have administration right or able to run notepad as administrator.\r\n
      \r\n

        \r\n

      1. Go to Start -> All Programs -> Accessories
      2. \r\n

      3. Right click Notepad and select Run as administrator
      4. \r\n

      5. Accept to run the program as administrator if you see “Windows needs your permission”.
      6. \r\n

      7. In Notepad opens Click File -> Open
      8. \r\n

      9. Type the following in the filename filed and click open. C:WindowsSystem32Driversetchosts
      10. \r\n

      \r\n[/jed_tab]\r\n[jed_tab title=”Linux”]To edit the hosts file on Linux (ubuntu) you have to open the hosts file in Terminal. Please follow the steps below to do this.\r\nbr/>\r\n

        \r\n

      1. Open Terminal
      2. \r\n

      3. Run the following command: sudo vim etc/hosts
      4. \r\n

      5. Enter your administrator password (if needed) en press enter
      6. \r\n

      7. You now see a new screen where you can enter the details
      8. \r\n

      9. Press Ctrl + O to safe your modification
      10. \r\n

      11. Press Ctrl + X to close nano
      12. \r\n

          [/jed_tab]\r\n[/jed_tabs]

One comment on “How to edit the hosts file (Mac, Windows & Linux)

  1. Is there anyway to tie a hostname to a different hostname. For example, if I have a hostname d3253434.somehost.com the chances of me remembering the subnet are not good so I would like to add the entry “d3253434.somehost.com myserver”. This works on Linux (Ubuntu), but doesn’t seem to work on Mac. They must have an ip address to map the hostname to. This is fine if the ip address is static, but it is possible, especially if the host you are mapping to is a laptop that might change its ip address eventually.

Leave a Reply

Your email address will not be published. Required fields are marked *