Skip to content

Instantly share code, notes, and snippets.

@zenorocha
Last active April 11, 2024 23:07
Show Gist options
  • Save zenorocha/18b10a14b2deb214dc4ce43a2d2e2992 to your computer and use it in GitHub Desktop.
Save zenorocha/18b10a14b2deb214dc4ce43a2d2e2992 to your computer and use it in GitHub Desktop.
/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.
@onlineinference
Copy link

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
Copy link

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

@JonBee
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
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
Copy link

thanks..it worked.

@DesiStuff
Copy link

@beanstalkim
Thanks your solution worked!

@x011
Copy link

x011 commented Feb 13, 2019

Don't forget to issue :

ipconfig /flushdns

To flush cached ip's

@htoonlynnkyaw
Copy link

thanks!

@MoraleJ
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..

Copy link

ghost 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
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
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
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
Copy link

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
Copy link

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
Copy link

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
Copy link

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.

@logbasex
Copy link

logbasex commented Jun 3, 2020

Thank you, but is there any idea about using the command line? I feel pretty uncomfortable with this solution.

@trainingbasket01
Copy link

Your post is very great.I read this post. It's very helpful. I will definitely go ahead and take advantage of this. Cheers for sharing with us your blog.
Python training in Noida
https://www.trainingbasket.in

@joaoldomingos
Copy link

Thank you, very good tutorial

Copy link

ghost commented Jun 16, 2020

Thank you, but is there any idea about using the command line? I feel pretty uncomfortable with this solution.

@hophiducanh : if you want to do everything from the command line, you need to install some form of 3rd party line editor (ported from Linux or other flavors of UNIX) to be able to edit the file. You would also need to make sure that the file is not in use by other apps or services, as well as making sure that the file is not write protected.

@loRes228
Copy link

hostname -I
😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment