keepcalculator

06 Numbers and Powers

SSH Aliases: Create Shortcuts for Frequent Connections

Card No. 06·68185 · Updated · Standard formula, shown below

Yes, you can create SSH shortcuts using aliases. By defining an alias in your SSH configuration file, you can connect to frequently used servers with a simple command like ssh myserver instead of typing the full ssh user@hostname -p port every time. This article explains how to set up these aliases, what options you can include, and how to troubleshoot common issues.

What Are SSH Aliases?

SSH aliases are shorthand names you define for SSH connections. They live in your SSH client configuration file, typically ~/.ssh/config on Linux, macOS, and Windows (with OpenSSH). Instead of remembering IP addresses, usernames, and non-standard ports, you assign a memorable alias to each host. When you type ssh myalias, SSH reads the config file, finds the matching Host block, and uses the associated settings.

This is not a shell alias (like alias ll='ls -l'). Shell aliases work too, but SSH config aliases are more powerful because they can store keys, port numbers, and other connection parameters.

How to Create an SSH Alias

Creating an SSH alias is straightforward. You edit the ~/.ssh/config file (create it if it doesn’t exist) and add a Host block. Here’s a minimal example:

Host myserver
    HostName 192.168.1.100
    User john

After saving, you can connect with ssh myserver. The HostName can be an IP address or a domain name. The User is optional if your local username matches the remote one.

You can also specify a non-default port, an identity file, or even a jump host (a proxy server) within the same block:

Host production
    HostName prod.example.com
    User deploy
    Port 2222
    IdentityFile ~/.ssh/id_ed25519_prod
    ProxyJump bastion

Choosing an Alias Name

Your alias can be any string that doesn’t contain spaces. Good practice is to keep it short, lowercase, and memorable. Avoid using names that conflict with existing commands (e.g., ssh test is fine, but ssh ls would be confusing). You can use wildcards in the Host line to match multiple hostnames, but for simple shortcuts, a literal name works best.

Common Options and Use Cases

Here are the most useful options you can put inside a Host block:

  • HostName – the actual hostname or IP address.
  • User – the remote username.
  • Port – the SSH port (default is 22).
  • IdentityFile – path to your private key for this host.
  • ProxyJump – a chain of hosts to connect through (e.g., a bastion server).
  • ServerAliveInterval – send keepalive messages to avoid disconnects.
  • Compression – set to yes on slow links.
  • LogLevel – set to QUIET to suppress banners.

You can also set global defaults at the top of the config file (e.g., ServerAliveInterval 60) that apply to all hosts unless overridden.

Common Problems and Fixes

If your alias doesn’t work, check these:

  • File permissions: On Unix-like systems, the config file must not be world-readable. Run chmod 600 ~/.ssh/config to fix.
  • Wrong file path: SSH reads ~/.ssh/config by default. If you use a different location, you must specify it with -F.
  • Typos: Ensure the Host line matches exactly what you type (case-sensitive).
  • First match wins: SSH uses the first matching Host block. If you have a wildcard like Host * before your alias, it may override settings.
  • Option precedence: Command-line options override config file options. If you’re testing with -p 22, that will override your config’s Port.

Alternatives to SSH Config Aliases

While SSH config aliases are the standard method, there are alternatives:

  • Shell aliases: Define alias myserver='ssh user@host -p 2222' in your .bashrc or .zshrc. Simpler but less flexible.
  • SSH client tools: Programs like sshfs can use the same config file, so aliases work for mounting remote filesystems too.
  • Connection managers: GUI tools like Termius or PuTTY (on Windows) store profiles, but they are not interchangeable with the OpenSSH config.

For most users, the SSH config file is the best choice because it works with all OpenSSH-based tools (scp, sftp, rsync) without extra setup.

Security and Best Practices

Using SSH aliases is safe as long as you follow basic security hygiene:

  • Keep your ~/.ssh directory permissions strict (chmod 700 ~/.ssh).
  • Use SSH keys instead of passwords. The config file can point to your key with IdentityFile.
  • Never put passwords in the config file – SSH doesn’t support that anyway.
  • If you use ProxyJump, ensure the jump host is trusted.

Remember that the config file is read by all users on the system if it’s in a system-wide location (like /etc/ssh/ssh_config), but your personal file is private.

Frequently Asked Questions

Can I use wildcards in SSH aliases?

Yes. You can use Host *.example.com to match any subdomain. This is handy for setting defaults for a group of hosts. However, for simple shortcuts, a literal name is clearer.

Why doesn’t my alias work when I use sudo?

When you run sudo ssh alias, SSH runs as root and reads /root/.ssh/config, not your user’s config. Use sudo -E ssh alias to preserve your environment, or add the alias to root’s config.

Can I create an alias for scp or sftp?

Yes. Since scp and sftp use the same SSH config, you can do scp file myserver:/path and it will use the alias settings. This is one of the biggest advantages over shell aliases.

How do I see what options are active for a host?

Run ssh -G myserver to print the effective configuration for that host. This shows all resolved options, which is great for debugging.

Is it okay to have multiple Host blocks for the same server?

Yes. You can define different aliases for the same server with different users, ports, or keys. For example, one alias for admin and one for regular access.

Can I use environment variables in the config file?

No, the SSH config file does not expand environment variables. You can use ~ for your home directory, but for dynamic values, you’d need to generate the config or use shell aliases.

Related

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

More cards in this drawer

The whole Numbers and Powers drawer →