Search code examples
sshdnsubuntu-16.04linode

SSH with domain instead of IP address


I have a ubuntu 16.04 @ linode.com.

I wanna be able to login through a subdomain (master.mydomainname.com) instead of an IP (192.0.2.1).

> hostname
domainname_master

> cat /etc/hosts
127.0.0.1     localhost
127.0.1.1     ubuntu.members.linode.com ubuntu
192.0.2.1     master.mydomainname.com mydomainname_master

I have a working A & AAAA record subdomain at cloudflare, which reachable through a webbrowser (nginx)

but when I try

[email protected]

I cant connect: ssh: connect to host master.mydomainname.com port 22: No route to host

With the IP itself I have no troubles to connect via SSH.

Did I miss something?


Solution

  • I wouldn't recommend doing this in /etc/hosts unless you have other reasons to do so. Instead, I'd recommend editing ~/.ssh/config (or a new file in /etc/ssh/ssh_config.d for all users) to add an entry to make this work for anything that uses SSH with your account (including scp, sftp, and rsync among others).

    Host master master.mydomainname.com mydomainname_master
      Hostname 192.0.2.1
      User root
    

    This content in your config file (learn more with man ssh_config) will allow you to run ssh master or ssh master.mydomainname.com or ssh mydomainname_master without needing to specify the host IP or even the username (root) on the command line. You always override the username by running e.g. ssh jan@master

    (If you're installing this system-wide, you probably don't want to specify the username.)