How to edit your hosts file on Mac and Windows

How to Update Hosts File Entry on Windows, Ubuntu and Mac 

[caption id="attachment_1401" align="aligncenter" width="300"]How to Update Hosts File Entry on Windows, Ubuntu and Mac How to Update Hosts File Entry on Windows, Ubuntu and Mac[/caption]

What is a Hosts File?

You can set IP addresses for a domain on your local system through editing hosts file that can overrule the authoritative DNS. It can allow you to check, what a domain will show if it’s pointed to a different server.
The hosts file will be edited to block certain hostnames (like advertising/malicious hosts), or used for web development purposes, i.e. to redirect domains to local addresses.

Windows

To access the hosts file in Windows XP, Vista, 7, 8x, 10 you have to use the following command in the Run Line to open the file with notepad.
STEP 1: Press Windows + R keys to Run Line.
STEP 2: Copy the below mentioned command and paste into the Run Line.
    notepad c:\windows\system32\drivers\etc\hosts

STEP 3: When notepad is open you can edit the hosts file.
For example, we will block Facebook. To block Facebook just enter following the line after the # mark in the hosts file.
0.0.0.0    www.facebook.com

STEP 4: Now you have edited your Hosts file and save it.

STEP 5: Now observe if we try to reach Facebook in IE browser we can’t get to the page.

STEP 6: We also were not able to access it in Google Chrome.

Ubuntu

You can change in the hosts file directly through the terminal on In Ubuntu 10.04 and most Linux distros. You can use your favored editor or even open your favorite GUI text editor. Like Windows 7x, Ubuntu’s hosts file placed in the /etc/ folder, however here it is the root of the drive. In order to edit the file, you should open it as root which is why we use sudo here.
STEP 1: We will use VIM for this example.

STEP 2: You will see that with Ubuntu there is similarly a section for IP6. For most needs, you will just need to edit it the top section and ignore the IP6.

STEP 3: Now save the file and try to open Facebook.com. Just like in windows we will see that we can’t get to the page.

Mac OS X (Any version)

STEP 1: In Mac OS X, editing the hosts file is similar to Ubuntu. Begin in terminal and use your preferred editor or even open your favorite GUI text editor, it is easier to do so from terminal.

STEP 2: The file will look like a Windows, simply with a little less explanation. Again we are going to redirect Facebook.

STEP 3: This time it looks like that 0.0.0.0 is a loopback and will redirect you to the PCs Apache test page.
------------------------------------------------------------------------------------------------------------------
Common practice for Web developers at Dirigo Design & Development is to push the latest build of a project to a test server for our clients and internal team to view. For a client to see our progress, they must edit the .hosts file on their computer (this allows a preview of a Web site on a server).
How it works When you view a Web site, the .hosts file is what links the site’s host name to an IP address. When a match is not found, a Domain Name System (DNS) lookup is issued by your computer.
By editing your computer’s .hosts file, this intercepts the DNS lookup and redirects your domain to another IP address. Theoretically you can redirect a call to www.google.com to another site. This all sounds pretty interesting, doesn’t it? Well think of all the fun you can have with this information when you are an undergrad and a self-proclaimed computer nerd who liked to play practical jokes on their roommate.
How to change your .hosts file  Below are detailed instructions on how to edit your computers hosts file depending on the operating system you have installed.
Microsoft Windows 7, 8, 8.1 & 10
  1. Click Start -> All Programs -> Accessories.
  2. Right click “Notepad” and select “Run as administrator”
  3. Click Yes on the UAC prompt.
  4. Click File -> Open.
  5. Change the file filter drop down box from “Text Documents (*.txt)” to “All Files (*.*)”.
  6. Browse to “C:\Windows\System32\Drivers\etc”.
  7. Select “hosts” and click “Open”.
  8. Insert the test servers IP and your domain name. For example: 100.100.100.100 mywebsite.com.
  9. Close Notepad. Click Save when prompted.
Microsoft Windows Vista
  1. Click Start -> All Programs -> Accessories.
  2. Right click “Notepad” and select “Run as administrator”
  3. Click Continue on the UAC prompt.
  4. Click File -> Open.
  5. Change the file filter drop down box from “Text Documents (*.txt)” to “All Files (*.*)”.
  6. Browse to “C:\Windows\System32\Drivers\etc”.
  7. Select “hosts” and click “Open”.
  8. Insert the test servers IP and your domain name. For example: 100.100.100.100 mywebsite.com.
  9. Close Notepad. Click Save when prompted.
Microsoft XP/2003/2000
  1. Browse to Start > Programs > Accessories > Notepad.
  2. Click File > Open.
  3. Browse to “C:\Windows\System32\Drivers\etc”.
  4. Select “All Files (*.*)” from the Files of Type drop-down menu.
  5. Select “hosts” and click “Open”.
  6. Insert the test servers IP and your domain name. For example: 100.100.100.100 mywebsite.com.
  7. Close Notepad. Save when prompted.
Apple OS X Lion, Snow Leopard & Leopard
  1. Open Terminal (Click Spotlight, or press Command+Space.
  2. Now type terminal.)
  3. Copy and paste the following code into Terminal:
    1. sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts
    2. Press Enter.
    3. Insert the test servers IP and your domain name.
      1. For example: 100.100.100.100 mywebsite.com.
Below is an example of what the hosts file will look like.
  1. Save by pressing Command+S, click Save.
  2. Restart your browser before attempting to view your site.
Example showing adding 100.100.100.100 mywebsite.com to hosts on Snow Leopard.
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
#DIRIGO Test Server IP and Site Name
100.100.100.100 mywebsite.com
Apple OSX 10.4.X Tiger
  1. Open Terminal (Click Spotlight, or press Command+Space. Now type terminal.)
  2. In Terminal type, ‘sudo pico /etc/hosts‘, press Enter.
  3. Insert the test servers IP and your domain name. For example: 100.100.100.100 mywebsite.com.
  4. Press Ctrl+X, then Y to save.
  5. Type ’sudo niload -v -m hosts . < /etc/hosts', press Enter
  6. Type ‘lookupd -flushcache’, press Enter.

Comments