ThisCalculator

Everyday

SSH to VPS by Domain Name Instead of IP

Updated · Standard formula, shown below

SSH to VPS by Domain Name Instead of IP

This guide shows you how to connect to your virtual private server (VPS) using a domain name instead of typing its IP address every time. You’ll set up a simple SSH configuration file that maps a short alias to your server, so you can type ssh myserver and get straight in. You’ll need your VPS’s IP address, your SSH username, and a domain name that already points to that IP (via an A or AAAA record). If you haven’t created the DNS record yet, we’ll cover that too.

What You’ll Need

  • A VPS with SSH enabled (standard port 22 or a custom port).
  • The server’s IP address (IPv4 or IPv6).
  • Your SSH username (often root or a sudo user).
  • A domain name that you control, with DNS management access.
  • An SSH client installed on your local machine (OpenSSH on Linux/macOS, or PuTTY/Windows OpenSSH on Windows).

Step-by-Step

  1. Point your domain to the VPS IP. Log in to your DNS provider (where you bought the domain or manage its nameservers). Create an A record for IPv4 or AAAA record for IPv6. For example, if your domain is example.com and your VPS IP is 203.0.113.10, add an A record with host @ (or blank) for the root domain, and optionally www for a subdomain. If you want to use a subdomain like vps.example.com, create an A record with host vps. Wait for DNS propagation (usually a few minutes to a few hours).
  2. Verify DNS resolution. On your local machine, run ping example.com or nslookup example.com. If it returns your VPS IP, the record is live. If not, wait a bit longer and check that you typed the record correctly.
  3. Test SSH with the domain name. Try connecting directly: ssh username@example.com. If your SSH server uses a non-standard port (e.g., 2222), use ssh -p 2222 username@example.com. This confirms the domain works before you set up shortcuts.
  4. Create the SSH config file. On your local machine, open (or create) ~/.ssh/config in a text editor. If the file doesn’t exist, create it with touch ~/.ssh/config.
  5. Add a host entry. In the config file, add something like:
    Host myserver
        HostName example.com
        User username
        Port 22

    Replace myserver with any alias you like, example.com with your domain, username with your SSH user, and Port with the actual SSH port (default 22). Save the file.

  6. Set correct permissions. Run chmod 600 ~/.ssh/config to ensure only you can read/write it. SSH will ignore the file if permissions are too open.
  7. Connect using the alias. Now simply run ssh myserver. SSH will read the config, resolve the domain, and connect. You can also use scp myserver:/path/to/file . or sftp myserver.
  8. Optional: set up SSH keys for passwordless login. If you haven’t already, generate a key pair with ssh-keygen, then copy the public key to the server with ssh-copy-id myserver (or manually append to ~/.ssh/authorized_keys). This makes future logins faster and more secure.

Common Mistakes

  • Using the wrong DNS record type. For a VPS with an IPv4 address, you need an A record, not a CNAME. A CNAME can’t point to an IP address.
  • Forgetting the port. If your VPS uses a custom SSH port, you must specify it in the config, otherwise SSH tries port 22 and fails.
  • Not waiting for DNS propagation. DNS changes can take time, especially if TTL is high. Test with dig or nslookup before assuming it’s broken.
  • Incorrect config file permissions. If ~/.ssh/config is readable by others, SSH will refuse to use it. Fix with chmod 600.
  • Typing the alias as a domain. If you set Host myserver, you must type ssh myserver, not ssh myserver.com.

Tips

  • Use a short, memorable alias like vps or prod to save time.
  • If you have multiple servers, create separate Host blocks for each.
  • You can also set IdentityFile in the config to specify which private key to use for that host.
  • Consider using a wildcard in DNS (e.g., *.example.com) if you plan to run multiple services on the same VPS.
  • Keep your SSH config organized with comments (# My main server) for clarity.

Troubleshooting

  • “Could not resolve hostname” – This means your computer can’t find the domain. Check DNS with nslookup example.com. If it fails, your DNS record may not be propagated yet, or you mistyped the domain. Try using a public DNS like 8.8.8.8 to test.
  • “Connection refused” – The server is reachable but SSH isn’t listening on that port. Verify the port in your config matches the server’s SSH port. Also check that the SSH service is running on the VPS (e.g., sudo systemctl status ssh).
  • “Permission denied (publickey)” – Your SSH key isn’t set up on the server. Use password authentication if enabled, or manually add your public key to ~/.ssh/authorized_keys on the server.
  • “Bad owner or permissions” – Your config file permissions are wrong. Run chmod 600 ~/.ssh/config and also check the ~/.ssh directory permissions (should be 700).
  • “Connection timed out” – The server may be firewalled. Ensure that your VPS’s firewall allows inbound SSH from your IP. Also check that the domain resolves to the correct IP (maybe you have multiple A records).
  • SSH works with IP but not domain – The DNS record is missing or wrong. Double-check the A/AAAA record and that it points to the exact IP you use successfully.

Frequently Asked Questions

Do I need a static IP for this to work?

Yes, your VPS must have a static IP address. If the IP changes, the DNS record will point to the old address and you’ll need to update it.

Can I use a subdomain instead of the root domain?

Absolutely. Just create an A record for the subdomain (e.g., vps.example.com) and use that in your SSH config.

Will this work if my VPS uses a non-standard SSH port?

Yes, just include the Port line in your config file. SSH will use that port automatically.

Is it more secure to use a domain name instead of an IP?

No, security is the same. The domain name is just a convenience; it doesn’t change encryption or authentication. Always use SSH keys and disable password login for better security.

What if I don’t have a domain name?

You can still use this method with a free dynamic DNS service (like DuckDNS), but those often have limitations. Alternatively, you can just use the IP in the config file – the same shortcut works with an IP address.

Can I use this on Windows?

Yes. OpenSSH is built into Windows 10/11. The config file is at C:\Users\YourName\.ssh\config. The steps are the same, but use notepad to edit and icacls or just set permissions via file properties.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.