Editing the .\etc\hosts file on Windows 10

0

I tried multiple ways to edit hosts file on Windows but failed.

First the hosts file seemed to appear, when using PowerShell, but the whole \etc\ directory disappeared when using any other tool. But because I don't know how to open as admin in PowerShell (I guess it's not possible), I just created a new etc directory with notepad run as an admin.

But now the information provided in hosts isn't used by any program (browser or putty). As a Unix nerd, this is very frustrating to me. How can I edit it on Windows 10? I also wouldn't mind some GUI thing hidden somewhere deep in the Settings app providing the same functionality.

hgiesel

Posted 2017-03-24T12:43:21.140

Reputation: 133

/etc/hosts is something that exists in linux, not windows 10 as far as I'm aware. Where are you trying to edit this file? Your question is currently un clear. – djsmiley2k TMW – 2017-03-24T12:46:47.367

3It exists under C:\windows\system32\drivers\etc\hosts on every Windows machine. – hgiesel – 2017-03-24T12:48:06.380

If you want to run something as administrator in PowerShell, then start PowerShell itself as administrator. – LPChip – 2017-03-24T12:56:56.683

You have to open your text editor as an Administrator that's the only way. – Ramhound – 2017-03-24T12:59:06.427

@Ramhound its not the only way. It is possible to set the rights properly and then you don't need to. There's a plugin for Chrome which will request changing rights in order for it to work, and then you don't need administrative permissions anymore. – LPChip – 2017-03-24T13:44:08.630

@LPChip - I was not aware of that. – Ramhound – 2017-03-24T15:03:51.017

Answers

5

You don't have to invoke PowerShell as elevated administrator, but it helps if you have to do multiple operations that require admin permission.

To edit the hosts file from a normal PowerShell session use:

Start-Process -FilePath notepad.exe -Verb runas -ArgumentList "$env:SystemRoot\system32\drivers\etc\hosts"

You will be prompted for administrator credentials and then notepad.exe will be started elevated and you can edit the hosts file.

Note the -Verb runas in the command, it tells PowerShell to start the process with with a higher Integrity level.

Don't try to change the NTFS permissions on the hosts file to edit it as a normal user. This is a security risk and the permissions may be reset by the next Windows Update or a repair process.

Peter Hahndorf

Posted 2017-03-24T12:43:21.140

Reputation: 10 677

0

First, note that you must invoke Powershell as Admin. you are correct there is no command like sudo/su, but you can right click powershell and run as Admin.

Second windows ships with a %systemroot%\system32\drivers\etc\hosts file by default, but in some recent versions, a token of that path (drivers I believe) is hidden.

Third, after modifying the file, be sure to reboot.

Fourth, note that tools like NSLookup will always check DNS first, so when testing use Ping or another simmilar utility that echoes out the resolved IP, rather than using nslookup.

Frank Thomas

Posted 2017-03-24T12:43:21.140

Reputation: 29 039

No need to reboot. I found out that most of the time the changes are instant, and in the cases where it isn't, an ipconfig /flushdns is sufficient. Also, don't forget the browser's cache. – LPChip – 2017-03-24T13:06:54.777

If your using the hosts file to block content your blocking content in an inefficient way. You can use hosts to define, hosts on your network, when it's not applicable to add the hosts to your dns. If you define hundreds or thousands of hosts entries you will slow your browsing down – Ramhound – 2017-03-24T13:10:51.513

I generally use the host file for redirecting traffic into infrastructure tunnels, and for mocking out test environments for servers where code is deployed that uses DNS names in conjunction with Active Directory (so we can't point to a mock dns server). For instance, right now I'm testing a bunch of web apps on a new server, that will replace an existing box when its ready, and host file rules are what makes the production links point to test resources while testing commences. As for reboots, I've set up hosts file on 3 boxes in the last few months, and all required rebooting for me. – Frank Thomas – 2017-03-24T13:52:56.163

0

As mentioned in other answers, the hosts file is stored at C:\windows\system32\drivers\etc\hosts . However, in Windows Vista and above, you have to change permissions. Some of my web developer users want to manually change it every hour, so my process was : 1. Open an elevated explorer.exe to that directory 2. Copy the original hosts file 3. Rename the original hosts file to .old 4. Rename the new file to 'hosts' 5. Edit permissions to explicitly allow the relevant user full access. Don't allow 'Everyone'.

Christopher Hostage

Posted 2017-03-24T12:43:21.140

Reputation: 4 751