2

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

5

/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?

1
  • 1
    This is a perfect solution. I was just trying one with -p all, but specifying the port is a more secure and efficient way. – user1532587 May 2 '18 at 11:50
0

Nope, won't work - when the system sees an IP, it skips DNS lookups (which would also include hosts file lookup). And if it WERE to work that'll (probably) break stuff. And it is backwards in the file anyway.

ip.add.re.ss  hostname

So you'd want

127.0.1.1  5x.2x.2xx.1xx 

And it won't work. And I still have a feeling it will break stuff.

What will work though is to use a name instead, and change the name as needed.

IE,

127.0.1.1 dbhost

And change it to

5x.2x.2xx.1xx dbhost

As needed. And always connect with mysql -h dbhost -u root -p no matter which IP the name happens to be pointing to...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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