DEV Community

Nate
Nate

Posted on Originally published at shadeterm.com

Make SSH Config Work for You: Aliases, Keys, and Safe Defaults

If you type the same server address, username, port, and key every time you connect, OpenSSH can save those settings locally. A small client config turns a long command into ssh production—and the same host alias can work with OpenSSH's scp and sftp commands.

The key distinction: this file configures the SSH client on your computer. It does not configure the remote server or change which connections that server accepts.

Find the right config file

OpenSSH reads per-user client settings from ~/.ssh/config on macOS and Linux. Native Windows OpenSSH normally uses %USERPROFILE%\.ssh\config. Windows and WSL have separate home directories, so they generally use separate config files too.

On macOS or Linux, create the directory and file without replacing existing settings:

mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

On Windows, use PowerShell to create the directory and open the config file in Notepad:

$sshDir = Join-Path $env:USERPROFILE '.ssh'
New-Item -ItemType Directory -Path $sshDir -Force | Out-Null
$config = Join-Path $sshDir 'config'
if (-not (Test-Path $config)) {
    New-Item -ItemType File -Path $config | Out-Null
}
notepad $config
Enter fullscreen mode Exit fullscreen mode

Save the file as config, without a .txt extension. If an alias works in one terminal but not another, check which SSH executable and home directory that terminal uses.

Replace repeated connection options with a host alias

A Host block matches the name you type after ssh. HostName is the actual server address; User, Port, and IdentityFile supply connection details.

Host production
    HostName server.example.com
    User deploy
    Port 2222
    IdentityFile ~/.ssh/id_ed25519_prod
Enter fullscreen mode Exit fullscreen mode

Now connect with:

ssh production
Enter fullscreen mode Exit fullscreen mode

Replace the example values with your real server details. IdentityFile points to a private key on your computer, not the .pub file. The key must already exist, and its public key must be authorized for the remote account. A config entry does not create keys or grant access.

You can define separate blocks for separate environments:

Host dev
    HostName dev.example.com
    User developer

Host staging
    HostName staging.example.com
    User deploy
    Port 2202

Host production
    HostName prod.example.com
    User deploy
    IdentityFile ~/.ssh/id_ed25519_prod
Enter fullscreen mode Exit fullscreen mode

The aliases also work with OpenSSH file-transfer commands:

sftp production
scp ./report.txt production:/tmp/report.txt
Enter fullscreen mode Exit fullscreen mode

Inspect settings before connecting

When a connection behaves unexpectedly, first ask OpenSSH what it will actually use. ssh -G expands the configuration and exits without contacting the server:

ssh -G production | grep -E '^(hostname|user|port|identityfile|proxyjump) '
Enter fullscreen mode Exit fullscreen mode

In PowerShell, use Select-String instead of grep:

ssh -G production | Select-String -Pattern '^(hostname|user|port|identityfile|proxyjump) '
Enter fullscreen mode Exit fullscreen mode

This is especially useful when host patterns or shared defaults are involved. Most options use the first value OpenSSH obtains, so a broad Host * block placed before a specific host can affect the result. Put specific blocks first, then shared defaults:

Host production
    ServerAliveInterval 15

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3
Enter fullscreen mode Exit fullscreen mode

Here, production uses the earlier 15-second interval; other hosts can use the shared 60-second default. IdentityFile is an important exception to the usual first-value behavior: multiple entries can accumulate. Check the full ssh -G output if the client offers an unexpected key.

For connections through a bastion, put the jump host in its own block and reference it with ProxyJump:

Host bastion
    HostName bastion.example.com
    User admin
    IdentityFile ~/.ssh/id_ed25519_bastion

Host internal-app
    HostName 10.0.0.20
    User deploy
    ProxyJump bastion
Enter fullscreen mode Exit fullscreen mode

Then run ssh internal-app. The private destination address needs to be reachable from the bastion network, not necessarily from your computer. For more detail on this connection pattern, see this ProxyJump and SSH jump-host walkthrough.

Keep security settings intentional

IdentityFile selects your authentication key. StrictHostKeyChecking controls whether the client trusts the server's host key. They solve different problems: a correct login key does not prove that the server on the other end is the one you intended to reach.

For normal interactive use, OpenSSH can prompt about an unknown host key and reject a changed one. In automation, accept-new trusts a new key on first connection but rejects changes later; it cannot independently verify that the first connection reached the right server. For higher-assurance automation, verify host fingerprints through a trusted channel and provision them before connecting.

Avoid setting StrictHostKeyChecking no globally just to silence a warning. A changed host key can be a sign of a legitimate rebuild, but it can also indicate that you are connecting to an unexpected server. Investigate before accepting it.

Also avoid enabling agent forwarding globally without a specific need. If your config uses LocalForward to reach a remote-only service, bind the local side to 127.0.0.1 unless you intentionally want other machines on your network to access that port. This guide to local, reverse, and dynamic SSH tunnels explains how forwarding modes differ.

A quick troubleshooting sequence

If the alias does not work, check these in order:

  1. Confirm the file and alias. Make sure the active SSH client reads the config you edited, and that the Host value matches what you type.
  2. Inspect the resolved options. Run ssh -G production and check the hostname, user, port, key paths, and jump host.
  3. Read the connection error. ssh -v production shows config loading and connection attempts, but unlike ssh -G, it contacts the server.
  4. Check file permissions if SSH complains. On Unix-like systems, chmod 700 ~/.ssh and chmod 600 ~/.ssh/config are safe defaults. Windows uses NTFS ACLs rather than chmod; inspect the file's access rules instead.

A config can be correct while the server is unreachable or rejects your key. If ssh -G shows the expected settings, investigate connectivity or server-side authorization rather than changing unrelated client options. You do not need to restart the SSH server after editing your local client config; new SSH commands read it when they run.

I originally published a more detailed version of this guide on the ShadeTerm blog.

I'm also building ShadeTerm — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.

Top comments (0)