Software & OS

SSH Config File: Location, Syntax and Examples (~/.ssh/config)

The SSH config file is a plain-text file at ~/.ssh/config (on Windows, C:\Users\<you>\.ssh\config) that stores per-host settings, so ssh web can replace ssh -p 2222 -i ~/.ssh/id_ed25519_work [email protected]. Each Host block lists options such as HostName, User, Port and IdentityFile, and for every option ssh uses the first value it finds, so specific hosts go at the top and defaults at the bottom.

A minimal SSH config example

Host web
    HostName 203.0.113.10
    User alex
    Port 2222
    IdentityFile ~/.ssh/id_ed25519_work

Host *
    ServerAliveInterval 60

Now ssh web, scp file.txt web:/tmp/ and sftp web all use that address, user, port and key. The indentation is only for readability; a Host block runs until the next Host or Match line.

Where is the SSH config file?

SystemPer-user fileSystem-wide file
Linux, macOS~/.ssh/config/etc/ssh/ssh_config
Windows (built-in OpenSSH)%userprofile%\.ssh\config%programdata%\ssh\ssh_config

The file doesn't exist until you create it. On Linux or macOS:

mkdir -p ~/.ssh
touch ~/.ssh/config
chmod 600 ~/.ssh/config

The ssh manual requires strict permissions: "read/write for the user, and not writable by others". If ssh complains about the permissions of the file, chmod 600 fixes it. On Windows, create the file in PowerShell with New-Item -ItemType File $HOME\.ssh\config and edit it in any text editor, keeping the name exactly config with no extension.

Command-line options override both files, and the per-user file is read before the system-wide one. To use a different file for one command, pass -F path; that also skips the system-wide file.

How ssh picks a value: first match wins

This is the rule that trips people up. ssh reads the file top to bottom, and for each option it keeps the first value it finds in any matching block. Later values for the same option are ignored. Here is a config written in the wrong order:

Host *
    User root

Host web
    HostName 203.0.113.10
    User alex

Check what ssh will actually use with -G, which prints the final configuration after evaluating Host and Match blocks, then exits without connecting:

ssh -G web | grep -E '^(user|hostname) '

Output (OpenSSH 9.7):

user root
hostname 203.0.113.10

User alex never applies, because Host * matched first and already set User. Move Host * to the bottom and it becomes a fallback, which is what you almost always want. On Windows PowerShell, use ssh -G web | Select-String '^(user|hostname) '.

Host patterns and wildcards

  • * matches zero or more characters; Host * alone applies to every host.
  • ? matches exactly one character.
  • Several patterns on one line are alternatives: Host web db cache.
  • ! excludes: Host *.example.com !bastion.example.com matches every host in the domain except the bastion.

Patterns match the host name you type on the command line, not the resolved address. So Host 10.0.1.* applies to ssh 10.0.1.5 but not to ssh web, even if web points to 10.0.1.5.

The options you'll use most

OptionWhat it doesExample
HostNameReal host name or IP to connect to (lets the Host name be an alias)HostName 203.0.113.10
UserRemote login nameUser alex
PortRemote port (default 22)Port 2222
IdentityFilePrivate key to try; can be listed more than onceIdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnlyUse only the configured keys, even if the agent offers moreIdentitiesOnly yes
ProxyJumpConnect through one or more jump hostsProxyJump bastion
ServerAliveIntervalSeconds of silence before the client checks the server is still thereServerAliveInterval 60
ServerAliveCountMaxUnanswered checks before disconnecting (default 3)ServerAliveCountMax 3
AddKeysToAgentAdd the key to ssh-agent on first useAddKeysToAgent yes
LocalForwardForward a local port through the serverLocalForward 5432 localhost:5432
ForwardAgentLet the remote host use your local agent (use with caution)ForwardAgent no

Example: different keys for two GitHub accounts

Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes

Host github.com
    User git
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes

Clone work repositories with the alias: git clone git@github-work:company/repo.git. IdentitiesOnly yes matters here: without it, ssh-agent may offer the personal key first and GitHub accepts it, logging you in as the wrong account. If you get Permission denied (publickey), the publickey troubleshooting guide walks through it; to create the keys, see how to generate an Ed25519 SSH key.

Example: reach a private server through a jump host

Host bastion
    HostName bastion.example.com
    User alex

Host db-internal
    HostName 10.0.1.20
    User alex
    ProxyJump bastion

ssh db-internal first connects to the bastion, then on to 10.0.1.20 through it. The one-off command-line equivalent is ssh -J bastion [email protected]. Keys stay on your machine; prefer ProxyJump to ForwardAgent, which the manual says "should be enabled with caution": a forwarded agent lets the remote side request signatures from your keys while you are connected.

Example: keep idle connections from dropping

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3

The client sends an encrypted check after 60 seconds of silence and disconnects after 3 unanswered checks, so a dead connection is detected after about 3 minutes instead of hanging. The default ServerAliveInterval is 0, meaning no checks are sent. Keep this block last.

Split a long config with Include

Include config.d/*

Host *
    ServerAliveInterval 60

Relative paths in Include are resolved from ~/.ssh in the user file. Because of first-match-wins, put the Include line near the top, so the included host blocks are read before your defaults.

If your config is ignored

  • Run ssh -G hostname and look at the values. If they're wrong, an earlier block won, or the Host pattern doesn't match what you typed (matching is case sensitive).
  • Run ssh -v hostname. Near the top it prints which file it read and which blocks applied, for example debug1: Reading configuration data /home/alex/.ssh/config and debug1: /home/alex/.ssh/config line 1: Applying options for web.
  • Check permissions (chmod 600 ~/.ssh/config) and that the name has no .txt extension.
  • On Windows, check which ssh runs with Get-Command ssh. Git for Windows ships its own ssh, which reads the same %userprofile%\.ssh\config but may be a different version.

Frequently asked questions

Does the SSH config file have a file extension?
No. The file is named config with no extension, inside the .ssh folder in your home directory. On Windows, make sure your editor did not save it as config.txt; turn on file name extensions in File Explorer to check.
Do I need to restart anything after editing ~/.ssh/config?
No. The ssh client reads the file every time it starts, so the next ssh, scp or git command uses the new settings. Only the server's sshd_config needs a service restart after changes.
Does Git use my SSH config?
Yes, when Git talks to a remote over SSH it runs the ssh client, which reads ~/.ssh/config. That is how a Host alias with its own IdentityFile lets you use different keys for different GitHub accounts, for example git@github-work:org/repo.git.
Is the Host name case sensitive?
Yes. Host patterns are matched case-sensitively against the name you type on the command line, as Microsoft's example config notes, so Host Web will not match ssh web.

Sources