How can I specify IP and ports for a hostname in the Windows hosts file?

19

7

I want to specify host names with two different ports in the Windows hosts file.

Is there a way to do it? Or is it not allowed by Windows itself?

I have been wasting my time searching for the solution for the last 8 hours.

Is it possible to specify ports in the host file, hosts? E.g.: 127.0.0.1:80 and 127.0.0.1:9211

SIA

Posted 2010-02-21T18:39:26.957

Reputation:

1The HOSTS file only associates names with IP addresses, not IPs+port(s). Do check superuser.com, however, by describing more of the specifics of what you are trying to do (is this for a web server etc. etc.) as there are many ways to achieve this type of mapping, but always depending on specifics at hand. – None – 2010-02-21T18:44:55.830

You need NGNIX or Apache HTTP server as a proxy server for forwarding http requests to appropriate application -> which listens particular port – Musa – 2018-02-08T18:08:01.357

Answers

18

You cannot associate a port number with a hostname mapped to an IP in the hosts file. You can achieve this with Fiddler though using FiddlerScript: 

if (oSession.HostnameIs("somesite.com")){
    oSession.bypassGateway = true;
    oSession["x-overrideHost"] = "1.2.3.4:8080";
}

John T

Posted 2010-02-21T18:39:26.957

Reputation: 149 037

Are you aware of any open source alternative (james, owasp zap, or other) which can be used to the same effect? – Marc.2377 – 2018-12-21T18:36:40.047

8Nice suggestion for tech users. Just to clarify,

  1. First install Fiddler
  2. Then open it and go to Rules Menu and pick Customize Rules option ( or press Ctrl + R on windows ). This will open a JS file in notepad.
  3. Find static function OnBeforeRequest and paste the script suggested by @John inside its body.

Also HostNameIs will not work, it should be HostnameIs.

Hope this helps. – Riz – 2013-08-13T07:40:11.213

18

Simply use IP addresses without ports. Example:

192.168.2.50  example.com

Then, to access 192.168.2.50:5555 from your browser (or other program):

http://example.com:5555/

The hosts file can be found at:

Linux /etc/hosts

Windows: C:\Windows\System32\drivers\etc\hosts

devi

Posted 2010-02-21T18:39:26.957

Reputation: 466

It will be helpful to add both path and the name of the file to be edited. – nyedidikeke – 2016-09-15T10:21:36.760

@nyedidikeke thanks for comment, i fixed this – devi – 2016-09-15T14:41:51.040

1Can you possibly update with the reason for NOT specifying the port number when editing the file at *C:\Windows\System32\drivers\etc\hosts* ? – nyedidikeke – 2016-09-15T19:57:13.527

That's covered in the other answers. – Scott – 2016-09-15T21:35:45.697

5

  • The hosts file is for host name resolution only
  • The browser, in the absence of directly specifying the port: i.e. <hostname>:<port>, defaults to port 80

Typical Problem Scenario

  1. applications typically set their servers to the same default ip address 127.0.0.1 aka localhost (defined in the hosts file).
  2. to avoid collision between possibly other existing/running servers, the application typically allows you to change the port, but not the ip address.

    2a. If you could change the servers ip address to another in the loopback reserved address space 127.0.0.0/8, then you probably wouldn't be attempting to set ports in the hosts file.

Possible solution

You can work around this using Windows included Networking tool netsh as a port proxy.


Overview

example.app
 |                               <--browser defaults to port 80
 +--> example.app:80
       |                         <--Hostname resolution by Hosts File
       +--> 127.65.43.21:80      
             |                   <--Link by netsh Utility
             +--> 127.0.0.1:8081

Actions

  • Start your server on localhost:8081
  • Add the "local DNS" in the hosts file as a new line
    • 127.65.43.21 example.app
      • Any free address in the network 127.0.0.0/8 can be used.
      • Note: I am assuming 127.65.43.21:80 is not occupied by another service.
      • You can check with netstat -a -n -p TCP | grep "LISTENING"
  • add the following network configuration with netsh command utility
    • netsh interface portproxy add v4tov4 listenport=80 listenaddress=127.65.43.21 connectport=8081 connectaddress=127.0.0.1
  • Access the server at http://example.app

Notes:
- These commands/file modifications need to be executed with Admin rights

- netsh portproxy needs ipv6 libraries even only to use v4tov4, typically they will also be included by default, otherwise install them using the following command: netsh interface ipv6 install


You can see the entry you have added with the command:

netsh interface portproxy show v4tov4

You can remove the entry with the following command:

netsh interface portproxy delete v4tov4 listenport=80 listenaddress=127.65.43.21


Links to Resources:

Note: this answer is a duplication of my answer discussed in this similar question/answer on stackoverflow.

Pau Coma Ramirez

Posted 2010-02-21T18:39:26.957

Reputation: 201