Edit Hosts file in Windows 7

After just having (finally) upgraded to Windows 7, I’ve come across a number of issues that aren’t necessarily typical from a Windows machine. This is the first.

I normally run a batch file to patch the hosts file on various machines. On Windows 7, it had no effect – despite the fact that I had logged on with an account that has administrative privelages.

You may received a warning resembling one of the following if you try to edit hosts manually.

Access to C:\Windows\System32\drivers\etc\ hosts was denied

or

Cannot create the C:\Windows\System32\drivers\etc\hosts file

Solution?

1. Open ‘Notepad’ from the Start Menu
2. Right click on Notepad and select ’Run as administrator’
3. Click ‘File->Open’ and browse to the hosts file at this location:

C:\windows\system32\drivers\etc\hosts

Open the file and edit. Click ‘Save’.

You cannot drag the hosts file into notepad and edit directly. You may encounter the same problem with other files (as I have), and this same method applies.

If you liked this article, you may also like:

  1. Unzip a file with PHP
  2. Simple PHP file counter
  3. Java SDK & Android SDK : Java SE Development Kit (SDK) not found
  4. Zip a file or files with PHP
About Marty

is a passionate web developer from Sydney, Australia. He owns about 400 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. You can follow Marty on Twitter or Google+.

Comments

  1. Phil says:

    I tried that and it didn’t work. cannot save the file

    • Marty says:

      Did you open the file as an administrator? Remember, drag and drop won’t work – you’ll have to hunt down the file from the open menu.

  2. Craig says:

    None of that matters. The problem lies in that you don’t “own” the hosts file. Save the following as “takeown.bat” and then run it to fix the issue:
    @echo off
    cls
    %systemdrive%
    cd “%windir%\system32\drivers\etc\”
    ver | findstr /i “6\.0\.” > nul
    IF %ERRORLEVEL% EQU 0 goto :takeown
    ver | findstr /i “6\.1\.” > nul
    IF %ERRORLEVEL% EQU 0 goto :takeown
    ver | findstr /i “6\.2\.” > nul
    IF %ERRORLEVEL% EQU 0 goto :takeown
    ver | findstr /i “7\.0\.” > nul
    IF %ERRORLEVEL% EQU 0 goto :takeown
    goto :exit
    :takeown
    attrib -s -h -r -a “%windir%\system32\drivers\etc\*.*” /s /d
    takeown /f “%windir%\system32\drivers\etc” /r /d y
    icacls “%windir%\system32\drivers\etc” /grant administrators:F /t
    :exit
    exit

Speak Your Mind

*