Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/etc/hosts on Windows

1. Get your IP Address

echo `ifconfig $(netstat -nr | grep -e default -e "^0\.0\.0\.0" | head -1 | awk '{print $NF}') | grep -e "inet " | sed -e 's/.*inet //' -e 's/ .*//' -e 's/.*\://'`

2. Modify your hosts file

notepad

For Windows 10 and 8
  1. Press the Windows key.
  2. Type Notepad in the search field.
  3. In the search results, right-click Notepad and select Run as administrator.
  4. From Notepad, open the following file: c:\Windows\System32\Drivers\etc\hosts
  5. Make the necessary changes to the file.
  6. Click File > Save to save your changes.
For Windows 7 and Vista
  1. Click Start > All Programs > Accessories.
  2. Right-click Notepad and select Run as administrator.
  3. Click Continue on the Windows needs your permission UAC window.
  4. When Notepad opens, click File > Open.
  5. In the File name field, type C:\Windows\System32\Drivers\etc\hosts.
  6. Click Open.
  7. Make the necessary changes to the file.
  8. Click File > Save to save your changes.
For Windows NT, Windows 2000, and Windows XP
  1. Click Start > All Programs > Accessories > Notepad.
  2. Click File > Open.
  3. In the File name field, type C:\Windows\System32\Drivers\etc\hosts.
  4. Click Open.
  5. Make the necessary changes to the file.
  6. Click File > Save to save your changes.
@ashikcse20

This comment has been minimized.

Copy link

ashikcse20 commented Sep 9, 2018

Thanks.

@metalgina

This comment has been minimized.

Copy link

metalgina commented Sep 27, 2018

wow, well, you guys seem to be having all this fun editing your 'hosts' file, the problem is, and I'm running windows 7 because windows 10 is absolute GARBAGE, I, for the life of me, can't SAVE the file!! I've tried it in DOS, I've tried using cmd, I've tried powershell, I've tried just using notepad, I've tried attribing it to different attribs to make it so I can write to the file, I've even tried editting the autoexec.bat to edit the file to add the correct address for my WMP and NOTHING!! I am so frustrated! I've been working on this thing for three hours I'm ready to light my computer on fire and run down the street naked!!! HELP!!!!

@Benjaminlooi

This comment has been minimized.

Copy link

Benjaminlooi commented Sep 29, 2018

@metalgina try cmd, powershell, notepad with Administrative privaledges?

@marieclairembanga

This comment has been minimized.

Copy link

marieclairembanga commented Oct 25, 2018

I can't see the repo etc in notepad. Additionally when i navigate on my explorer I see the file hosts but it's encoding when I open it

@Evil-cluster

This comment has been minimized.

Copy link

Evil-cluster commented Oct 27, 2018

To edit the hosts file in Windows 10 you need to do it from another OS, no matter if it is a live USB or just another OS in your computer, this is due to the fact that the system automatically opens this file just when you start your machine, and therefore it cannot be modified at the same time. To override this, you only need to do what I just mentioned, so that the hosts file doesn't get open and you can easily modify it. The same happens with the rest of the system files.
I hope I'm helpful.

@JoelAzaria

This comment has been minimized.

Copy link

JoelAzaria commented Oct 29, 2018

To edit the hosts file in Windows 10 you need to do it from another OS, no matter if it is a live USB or just another OS in your computer, this is due to the fact that the system automatically opens this file just when you start your machine, and therefore it cannot be modified at the same time. To override this, you only need to do what I just mentioned, so that the hosts file doesn't get open and you can easily modify it. The same happens with the rest of the system files.
I hope I'm helpful.

I'm on windows 10 and have customers on Win10. I see zero evidence that any of this has any basis in reality.
I edit my hosts file just fine from an admin cmd prompt.

@lopugit

This comment has been minimized.

Copy link

lopugit commented Nov 9, 2018

To edit the hosts file in Windows 10 you need to do it from another OS, no matter if it is a live USB or just another OS in your computer, this is due to the fact that the system automatically opens this file just when you start your machine, and therefore it cannot be modified at the same time. To override this, you only need to do what I just mentioned, so that the hosts file doesn't get open and you can easily modify it. The same happens with the rest of the system files.
I hope I'm helpful.

bro you are cooked

@lopugit

This comment has been minimized.

Copy link

lopugit commented Nov 9, 2018

To edit the hosts file in Windows 10 you need to do it from another OS, no matter if it is a live USB or just another OS in your computer, this is due to the fact that the system automatically opens this file just when you start your machine, and therefore it cannot be modified at the same time. To override this, you only need to do what I just mentioned, so that the hosts file doesn't get open and you can easily modify it. The same happens with the rest of the system files.
I hope I'm helpful.

I'm on windows 10 and have customers on Win10. I see zero evidence that any of this has any basis in reality.
I edit my hosts file just fine from an admin cmd prompt.

@frankarma is cooked

@beanstalkim

This comment has been minimized.

Copy link

beanstalkim commented Nov 15, 2018

wow, well, you guys seem to be having all this fun editing your 'hosts' file, the problem is, and I'm running windows 7 because windows 10 is absolute GARBAGE, I, for the life of me, can't SAVE the file!! I've tried it in DOS, I've tried using cmd, I've tried powershell, I've tried just using notepad, I've tried attribing it to different attribs to make it so I can write to the file, I've even tried editting the autoexec.bat to edit the file to add the correct address for my WMP and NOTHING!! I am so frustrated! I've been working on this thing for three hours I'm ready to light my computer on fire and run down the street naked!!! HELP!!!!

I had the same problem. The workaround is pretty simple, copy the file to your desktop, edit it, then move it back.

Don't ask me why. :)

@priteshjha4u

This comment has been minimized.

Copy link

priteshjha4u commented Nov 26, 2018

Alright, Do i need to restart my system after making the changes? It doesn't seem to be working instantly.

@JonBee

This comment has been minimized.

Copy link

JonBee commented Nov 27, 2018

@beanstalkim @metalgina The hosts file is owned by SYSTEM. In order to edit it with notepad, you'll need to run notepad as an administrator.

@priteshjha4u Running "ipconfig /flushdns" in command prompt should force Windows to reload the hosts file, if I remember correctly.

@ncolsens

This comment has been minimized.

Copy link

ncolsens commented Dec 29, 2018

The hosts file works with all versions of windows, in most operating systems use a hosts file including your android phone. When editing the hosts file simply make the changes and then save the file, but since windows will not allow you to save it where it resides, save it to your desktop and then manually copy it to C:\windows\system32\drivers\etc. You will be prompted to overwrite the file select yes. Remember when you finish editing the hosts file to strip off the extension ".txt" for it to work. There is no need to reboot your system, just close and re-open your browser of choice. When editing the host file on android phones you will need to reboot the phone. for the changes to take effect

Additionally do not use your WAN IP in the hosts file. you should use either
0.0.0.0
or
127.0.0.1

Your IP address can change too often

@bala0406

This comment has been minimized.

Copy link

bala0406 commented Jan 19, 2019

thanks..it worked.

@BunnyImran

This comment has been minimized.

Copy link

BunnyImran commented Feb 12, 2019

@beanstalkim
Thanks your solution worked!

@x011

This comment has been minimized.

Copy link

x011 commented Feb 13, 2019

Don't forget to issue :

ipconfig /flushdns

To flush cached ip's

@htoonlynnkyaw

This comment has been minimized.

Copy link

htoonlynnkyaw commented May 26, 2019

thanks!

@MoraleJ

This comment has been minimized.

Copy link

MoraleJ commented Jun 22, 2019

Team, I don't know if my question is valid here or not but trying hard with Google i came here on this link... and i think i will get help here...

**I want to use API service... And to get the service i need to generate Access Token... To generate Access Token I need to provide

Redirect URL..

I am trying to use " http:/127.0.01/ as Redirect URL as i want it on local server

But its not working...**

Can someone help me ? If this is irrelevant Question here then i can delete it..

@illfated

This comment has been minimized.

Copy link

illfated commented Jun 24, 2019

@MoraleJ : If you want to publish anything from your own computer (1270.0.1), you need to install configure and run some form of HTTP server, like Apache or Microsoft Internet Information Server service.

@elmoiv

This comment has been minimized.

Copy link

elmoiv commented Aug 4, 2019

I can't see the repo etc in notepad. Additionally when i navigate on my explorer I see the file hosts but it's encoding when I open it

See my answer here

@tmusabaika

This comment has been minimized.

Copy link

tmusabaika commented Aug 13, 2019

Another way to edit the hosts file directly is to use I/O redirection from cmd. It will not restrict you from editing the hosts file like the gui.

  1. Launch cmd with elevated privileges (as administrator)

  2. cd / navigate to the etc folder then issue your host entries using the following syntax

echo host_entry >> target_file

e.g. echo 127.0.0.1 localhost >> hosts

This will append your host entries at the end of the hosts file. Make sure you use the double greater than sign >> because using a single one > results in ovewriting whatever is already in the hosts file.

@Wagner-Rob

This comment has been minimized.

Copy link

Wagner-Rob commented Sep 11, 2019

I had trouble saving mine too, but it was actually a very simple reason. Right-click on the hosts file, opened properties, unchecked the "read-only" box, then saved it without issue. Now it resolves through DNS.

@KingRider

This comment has been minimized.

Copy link

KingRider commented Dec 10, 2019

Team, I don't know if my question is valid here or not but trying hard with Google i came here on this link... and i think i will get help here...

**I want to use API service... And to get the service i need to generate Access Token... To generate Access Token I need to provide

Redirect URL..

I am trying to use " http:/127.0.01/ as Redirect URL as i want it on local server

But its not working...**

Can someone help me ? If this is irrelevant Question here then i can delete it..

Possible redirect and go install extension is work fine:
https://chrome.google.com/webstore/detail/url-auto-redirector/mckfcfnegaimgcgepikhdnajpkkhdnkn

@durhambell

This comment has been minimized.

Copy link

durhambell commented Dec 13, 2019

Team, I don't know if my question is valid here or not but trying hard with Google i came here on this link... and i think i will get help here...
I want to use API service... And to get the service i need to generate Access Token... To generate Access Token I need to provide
Redirect URL..
I am trying to use " http:/127.0.01/ as Redirect URL as i want it on local server
But its not working...

Can someone help me ? If this is irrelevant Question here then i can delete it..

Possible redirect and go install extension is work fine:
https://chrome.google.com/webstore/detail/url-auto-redirector/mckfcfnegaimgcgepikhdnajpkkhdnkn

It doesn't seem like any of the comments here are relevant anyway, 90% of them are actually about Windows filesystem (although the people talking about it probably don't know they are talking about it). Ironically the reason some people are having trouble editing their hosts files is because it can be a really bad idea to edit the hosts file if you don't know what you are doing, so Windows makes it very difficult to do on purpose.

(aside: Hosts File Editor is a pretty nice little utility to do this, and it has some neat features like pinging endpoints for verification, "hot swapping" hosts files, and enabling/disabling them from the system tray)

But In Response to Your Issue

It sounds like you are trying to register for some service that wants you to provide a URL in order to get an access token. I think Twilio does things like this, as do some email marketing services. If I understand correctly, you will not be able to do this using your local machine as a redirect, because the service will want a publicly facing URL, probably for verification. The IP address 127.0.0.1 is a loopback address (as is any IP address beginning with 127.), so it is only accessible from the machine itself by design.

@durhambell

This comment has been minimized.

Copy link

durhambell commented Dec 13, 2019

Yeah, just to give it's own thread, there is a great little app to edit Windows hosts files (as well as swap them on the fly, ping endpoints, cut/copy/paste multiple entries, and disable/enable entries using check-boxes). It's called (brace yourselves, it's a super creative title): Hosts File Editor

image

@cacarr-pdxweb

This comment has been minimized.

Copy link

cacarr-pdxweb commented Jan 4, 2020

Yeah, just to give it's own thread, there is a great little app to edit Windows hosts files (as well as swap them on the fly, ping endpoints, cut/copy/paste multiple entries, and disable/enable entries using check-boxes). It's called (brace yourselves, it's a super creative title): Hosts File Editor

image

Very nice. If I'm ever stuck on Windows (so straightforward on Linux), I'll install that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.