kwarner04
By:
kwarner04

Ubuntu - Host Entry for private IP

August 17, 2015 1k views
Networking Linux Basics Ubuntu

Is it possible to create a host entry on my Ubuntu VPS to reference another droplet but using the private IP address?

Here's what I've got:
1) web1 @ private IP #1
2) mysql1 @ private IP #2

I've blocked all the connections on mysql1 so that only web1 can access it via SSH (port 22) and mysql (port 3306).

I'd like to create a host entry so that when I'm on web1, I can just type "ssh username@mysql1" and not have to remember the private IP address.

2 Answers

Yes, just edit the /etc/hosts file and add them.

You would add the IP of the Droplet and then a name separated by a whitespace:

10.0.1.45 web1
10.0.1.46 mysql1

And the you can easily do what you need:

  • ssh username@mysql1

But maybe you'd prefer to configure a SSH config file instead?
For example, you could edit your ~/.ssh/config on the specific Droplet and add them individually:

Host web1
        HostName 10.0.1.45
        Port 22
        User username
        IdentityFile ~/.ssh/id_rsa
        IdentitiesOnly yes

Host mysql1
        HostName 10.0.1.46
        Port 22
        User username
        IdentityFile ~/.ssh/id_rsa
        IdentitiesOnly yes

That way you can just do:

  • ssh web1

And the SSH configuration will automatically connect with the right credentials as configured.

If you use the same SSH key for every Droplet, you don't have to add your SSH key to the Droplet, but can add it to your local machines SSH config and remove the IdentityFile ~/.ssh/id_rsa line from your Droplets SSH configuration. To your local SSH configuration you could do:

Host mydroplet
        HostName public-ip-of-your-droplet
        Port 22
        User username
        IdentityFile ~/.ssh/id_rsa
        IdentitiesOnly yes
        ForwardAgent yes

Notice the ForwardAgent yes which means that your SSH key will be forwarded in your SSH connection. Just remember to add it to your SSH Agent (pagent for Putty, ssh-add for others) or the forwarding won't work.

Have another answer? Share your knowledge.