Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

A sandbox enviroment of a web app is reachable directly by an IP address: http://<my_ip_address> without a login name.

Is there a way to make a virtual host in my local machine, named as <my_ip_address>, and change /etc/hosts so it will "redirect" to my localhost? <my_ip_address>

So when I load the url http://<my_ip_address> the browser will point to my localhost? The way I can already redirect domain names.

share|improve this question
up vote 6 down vote accepted

Hosts file can only be used to associate a domain name with an IP; it cannot map IP to IP.

Altering IPs can be done by a firewall. On Linux, the default firewall is controlled by iptables commands. "man iptables" is the documentation. Google "explain iptables" for introductory explanations.

Some links:

Specifically, you want to change OUTGOING requests from your PC to an IP address (<my_ip_address>), so that they instead go to a different IP address, in this case You want to perform NAT (Network Address Translation), given the "destination IP" (<my_ip_address>; e.g., changing it to a different "destination IP" (

Try this (in place of, put the ip_address that needs to be altered):

iptables -t nat -A OUTPUT -p all -d -j DNAT --to-destination


-t nat = table for translating one address to another

-A OUTPUT = append to the list of rules for locally-generated, outgoing, packets.

-p all = apply to all protocols (tcp, udp, and icmp).

-d = the original IP address that the packet was going to (its destination).

-j DNAT = if the rule matches (in this case, if an outgoing packet has destination IP, then handle it with DNAT, which alters the destination.

--to-destination = tells DNAT what to do; replace the original destination with "".

(NOTE: If you had a more complex situation, such as intercepting requests for specific web pages, an alternative solution might be to use "proxy" software.)

share|improve this answer
Note that there is a security concern here. is commonly understood to be unreachable from any remote host. If you change that without adding filtering, you may unintentionally defeat a security measure. – Slartibartfast Mar 22 '14 at 21:23

Add to the line starting with in /etc/hosts, s.t. it reads       localhost.localdomain localhost

Verify that it works by trying

$ ping
share|improve this answer
it's obviously working, but i wonder how to replace an ip address – Mike Dec 16 '12 at 7:34
Not an answer to the question. OP already knows how to associate a domain name with an IP address. The question asks how to redirect one IP address to another. – ToolmakerSteve Mar 22 '14 at 19:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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