Editing HOSTS File
Table of Contents
Editing your computer's HOSTS file will allow you to test your domain against a different server to what the live DNS would normally use. This is commonly used for testing a site prior to making any public DNS changes (ie. during a migration).
When editing the HOSTS file, you add two pieces of information, the target IP address and the domain name (bear in mind that www.example.com
is not the same as example.com
- these are two different entities as far as DNS is concerned).
So if you wanted to override the DNS for www.example.com
to point to 127.0.0.1
, you would add the following to your HOSTS file,
127.0.0.1 www.example.com
If you wanted to cover both example.com
and www.example.com
, you would add the following,
127.0.0.1 www.example.com
127.0.0.1 example.com
Windows 7, 8, 10
Windows 7, 8, 10 use User Account Control (UAC) so Notepad must be run as Administrator.
- Click Start
- Type
notepad
, right click Notepad and select Run as administrator - Click Continue on the "Windows needs your permission" UAC window.
- When Notepad opens Click File -> Open
- In the filename field type
C:\Windows\System32\Drivers\etc\hosts
- Click Open
Windows Vista
Windows Vista uses User Account Control (UAC) so Notepad must be run as Administrator.
- Click Start -> All Programs -> Accessories
- Right click Notepad and select Run as administrator
- Click Continue on the "Windows needs your permission" UAC window.
- When Notepad opens Click File -> Open
- In the filename field type
C:\Windows\System32\Drivers\etc\hosts
- Click Open
Windows NT/2000/XP
- Click Start -> All Programs -> Accessories -> Notepad
- Click File -> Open
- In the filename field type C:\Windows\System32\Drivers\etc\hosts
- Click Open
Linux
- Open a terminal window
- Type sudo nano /etc/hosts (you can substitute any text editor)
- Enter your password
Mac OS X 10.0 - 10.1.5
- Open /Applications/Utilities/NetInfo Manager.
- To allow editing the NetInfo database, click the padlock in the lower left corner of the window.
- Enter your Admin password and click OK
- In the second column of the browser view, select the node named "machines." You will see entries for -DHCP-, broadcasthost, and localhost in the third column.
- The quickest way to create a new entry is to duplicate an existing one. So select the "localhost" item in the third column.
- Choose Duplicate from the Edit menu. A confirmation alert appears.
- Click Duplicate. A new entry called "localhost copy" appears, and its properties are shown below the browser view.
- Double-click the value of the ip_address property and enter the IP address of the other computer.
- Double-click the value of the name property and enter the hostname you want for the other computer.
- Click the serves property and choose Delete from the Edit menu.
- Choose Save from the File menu. A confirmation alert appears.
- Click Update this copy.
- Repeat steps 6 through 12 for each additional host entry you wish to add.
- Choose Quit from the NetInfo Manager menu. You do not need to restart the computer.
Mac OS X 10.6 - 10.1.8
- Open Applications > Utilities > Terminal.
-
Open the hosts file by typing the following in the Terminal window:
sudo nano /private/etc/hosts
Type your user password when prompted
- Edit the Host File,The hosts file contains some comments (lines starting with the # symbol), as well as some default hostname mappings (e.g. 127.0.0.1 – local host). Append your new mappings underneath the default ones.
- Save the Host File, When done editing the hosts file, press Control+x to save the file.
-
Make your changes take effect by flushing the DNS cache with the following command:
dscacheutil -flushcache
- New mappings should now take effect.