How to map an IP address to localhost

18,322

/etc/hosts can be used if you want to map a specific DNS name to a different IP address than it really has, but if the IP address is already specified by the application, that and any other techniques based on manipulating hostname resolution will be useless: the application already has a perfectly good IP address to connect to, so it does not need any hostname resolution services.

If you want to redirect traffic that is going out to a specified IP address back to your local system, you'll need iptables for that.

sudo iptables -t nat -I OUTPUT --dst 5x.2x.2xx.1xx -p tcp --dport 3306 -j REDIRECT --to-ports 3306

This will redirect any outgoing connections from your system to the default MySQL port 3306 of 5x.2x.2xx.1xx back to port 3306 of your own system. Replace the 5x.2x.2xx.1xx and 3306 with the real IP address and port numbers, obviously.

The above command will be effective immediately, but will not persist over a reboot unless you do something else to make the settings persistent, but perhaps you don't even need that?

Share:
18,322

Related videos on Youtube

user1532587
Author by

user1532587

Updated on September 18, 2022

Comments

  • user1532587
    user1532587 almost 2 years

    So I have an IP Address 5x.2x.2xx.1xx I want to map to localhost. In my hosts file I have:

    cat /etc/hosts
    127.0.1.1 test test
    127.0.0.1 localhost
    
    # The following lines are desirable for IPv6 capable hosts
    ::1 ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    ff02::3 ip6-allhosts
    
    5x.2x.2xx.1xx 127.0.0.1
    

    What I want to accomplish is that when I connect in this machine to 5x.2x.2xx.1xx, I go to localhost.

    What I really want is to connect to MySQL using

    mysql -uroot 5x.2x.2xx.1xx -p and instead of pointing to that IP address I want to use the local MySQL server

    At the time it isn't working since it stills redirect to the server's IP (5x.2x.2xx.1xx)

    I've also tried: sudo service nscd restart with no luck