Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

marked as duplicate by Chris S Sep 14 '12 at 1:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

10 Answers 10

up vote 18 down vote accepted

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

share|improve this answer
    
is there any work-around? –  Tony Aug 14 '09 at 18:29
7  
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
1  
Holy crap I did not know that. (later) @John Gardeniers Holy crap I did not know that either. –  boulder_ruby Oct 4 '12 at 20:28

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

:-)

share|improve this answer
3  
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
8  
The ifpw (mac friendly) way is sudo ipfw add 100 fwd 127.0.0.1,3000 tcp from any to any 80 in and to remove it sudo ipfw flush works like a charm! –  pferdefleisch Jan 6 '13 at 17:18
    
Why isn't this accepted answer? This should have been accepted. –  dhilipsiva Jul 23 '13 at 12:55
    
I get iptables command not found in Mavericks. –  Sarah Vessels Dec 5 '13 at 18:08

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

share|improve this answer
    
A simple solution as compared to modifying IP tables. Helps for development where sessions require the domain name to be specified. Whereas IP tables may help with production server. –  Tejas Jayasheel Sep 3 at 6:13
    
This is not what OP asked for. –  Niels Bom Sep 23 at 10:24

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

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

share|improve this answer

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