up vote 1 down vote favorite
3
share [g+] share [fb]

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?

link

feedback

locked by Chris S Jan 27 at 4:20

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. More info: FAQ.

10 Answers

up vote 10 down vote accepted

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

link
is there any work-around? – Tony Aug 14 '09 at 18:29
6  
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
feedback

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

:-)

link
2  
OS X descends from FreeBSD, so it uses ipfw and natd. – outis Aug 22 '09 at 19:42
If I could upvote this 1000 times I would. Using the arbitrary port is genius. Thanks - works perfectly. – Paul Biggar Jan 13 '10 at 0:56
feedback

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.

link
feedback

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

link
feedback

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.

link
feedback

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

link
feedback

The DNS solution for this is to use SRV records:

http://en.wikipedia.org/wiki/SRV%5Frecord

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.

link
feedback

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

link
feedback

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

link
feedback

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

link
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.