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