Edit hosts file in Windows 7

By default you cannot browse to C:\Windows\System32\drivers\etc and edit the hosts file directly in Windows 7. The edit the hosts file do the following:

1. From the start bar search for notepad and right-click on it and select Run as administrator per the below:


2. Browse to C:\Windows\System32\drivers\etc, select the hosts file and click Open:


3. Once open edit the hosts file per your requirements:


4. To save time and superfluous clicks, you can accomplish Steps 1-3 above by running a single command, see below:

runas /user:MYPC\administrator "notepad C:\Windows\System32\drivers\etc\hosts"

Replace MYPC\administrator with your PC name and admin account. Once you hit enter you will then be prompted for the password of the specified account, enter it and the hosts file will load up.

If you envisage running this command regularly and want to avoid entering the password in every time use the /savecred parameter, per the below:

runas /user:MYPC\administrator /savecred "notepad C:\Windows\System32\drivers\etc\hosts"

You will no longer be prompted for the password whenever running the command above thereafter. The syntax for the runas command can be found here. You can also dump the command in a batch file and run it when needed.

5. Save the changes and then open the command prompt and then run the following command:

c:\>ipconfig /flushdns

This will purge the DNS cache and the settings in your host will take effect from this point onwards:


Note: This is only required if the domain added to the hosts file (www.yourdomain.com) was visited prior to editing the hosts file. It is good practice to purge the dns cache in case any stale DNS information resides there.

6. Load www.yourdomain.com in your browser and the connection will direct to the IP address stated in the hosts file.