74

Possible Duplicate:
How to use DNS to redirect domain to specific port on my server

I want to trick my browser into going to localhost:3000 instead of xyz.com. I went into /etc/hosts on OS X 10.5 and added the following entry:

127.0.0.1:3000 xyz.com

That does not work but without specifying the port the trick works. Is there a way to do this specifying the port?

Tony
  • 4,053
  • 10
  • 34
  • 29

10 Answers10

55

No, the hosts file is simply a way to statically resolve names when no DNS server is present.

Chief A-G
  • 806
  • 7
  • 6
  • 1
    is there any work-around? – Tony Aug 14 '09 at 18:29
  • 11
    That's not completely accurate. If the machine is configured to use hosts before DNS then a hosts entry will be used even if there is a DNS entry for the same destination. – John Gardeniers Aug 14 '09 at 21:35
  • 2
    Holy crap I did not know that. (later) @John Gardeniers Holy crap I did not know that either. – boulder_ruby Oct 04 '12 at 20:28
  • 6
    I was able to get this working using the `ifconfig` and `pfctl` commands on Mac 10.10.2. With the following approach I'm successfully mapping `127.0.0.1:3000` to `mydomain.com` locally on my machine. I couldn't post this here, but I was able to post a step-by-step on how to do it here: http://serverfault.com/questions/102416/iptables-equivalent-for-mac-os-x/673551#673551 – Kevin Leary Mar 06 '15 at 17:25
49

The hosts file is for DNS resolution. DNS resolve names to IP addresses, and has nothing to do with ports I am afraid. You will need to use something else in conjunction with the hosts file to redirect the port (Mangle the TCP header by altering the destination port).

With iptables:
Does MAC OS use iptables / netfilter (I didn't think it did)..? If OS X uses iptables you could point xyz.com to some ip in the hosts file like 157.166.226.25 and then:

sudo iptables -t nat -A OUTPUT -p tcp --dport 80 -d 157.166.226.25 -j DNAT --to-destination 127.0.0.1:3000

:-)

Kyle Brandt
  • 82,107
  • 71
  • 302
  • 444
17

You don't need to specify a port in the hosts file. just make the entry like you did omitting the port, as in 127.0.0.1 xyz.com, this will direct you to your local host, then simply add port 3000 to the end of your URL... http://xyz.com:3000

stuart Brand
  • 492
  • 3
  • 11
4

Assuming you're trying to intercept http and not https, you'd have to be listening on port 80 on your local machine, but then you might be able to use ssh's port forwarding features by ssh'ing to localhost with -L80:localhost:3000, but you'll have to do that as root.

Would probably be better to have whatever it is that is running on port 3000 just listen to port 80 instead.

If you control the router between you and xyz.com, you might be able to setup a port forwarding rule instead.

retracile
  • 1,260
  • 7
  • 10
3

The DNS solution for this is to use SRV records:

http://en.wikipedia.org/wiki/SRV_record

These are a way to allow DNS, which was originally a "name to number" or "number to name" distributed database to include "name to service endpoint", which could (optionally) include a protocol and port.

The bad news is that applications have to be developed to use SRV records, so it's not a drop-in solution for what you're trying to do.

James F
  • 6,549
  • 1
  • 25
  • 23
2

I think you need to use some kind of proxy server or maybe something with firewall software to redirect port connections...

Bart Silverstrim
  • 31,092
  • 9
  • 65
  • 87
2

Chief-AG is right in that the hosts file is used to statically resolve names (DNS presence is irrelevant). However, there may be a combination of things you could do.

  1. Set the record in the hosts file to 127.0.0.1 xyz.com
  2. Configure your machine for virtual hosts.
  3. For the virtual host you setup for xyz.com, create an html file that redirects to localhost:3000

Seems to be a fair bit of work, but it would accomplish what you're asking.

1

As an alternative to ;'s virtual hosts, you could create an ssh tunnel listening on port 80 and forwarding to localhost:3000.

evilchili
  • 101
  • 2
1

i'm assuming this is for rails development? if so, then run script/server -p 80 to make it run on the standard web port. then your xyz.com will work

Ryan
  • 268
  • 1
  • 7
0

If you are using Apache as a webserver on xyz.com, you could use Apache's ProxyPass to 'convert' to to a different port.

Amandasaurus
  • 30,211
  • 62
  • 184
  • 246