Guide
SSH config file: examples for ~/.ssh/config
The SSH config file, ~/.ssh/config, saves each server's details under a short name. With the entry below, ssh web connects to the right address and port, as the right user, with the right key. scp, sftp, rsync and other tools that run ssh read the same file.
Below: where the file is on Linux, macOS and Windows, how ssh reads it, and examples you can copy for ports, keys, jump hosts, port forwards and keep-alives.
- Linux, macOS and Windows
- Copy-ready examples
- Check it with ssh -G
Updated · OpenSSH on Linux, macOS and Windows 10 and 11
Where the SSH config file is
| System | Your own settings | For every user |
|---|---|---|
| Linux | ~/.ssh/config | /etc/ssh/ssh_config |
| macOS | ~/.ssh/config | /etc/ssh/ssh_config |
| Windows 10 and 11 | C:\Users\you\.ssh\config | C:\ProgramData\ssh\ssh_config |
The file is plain text with no extension, and it doesn't exist until you create it. Don't confuse it with /etc/ssh/sshd_config (with a d), which is the server's settings.
Step-by-step: create your SSH config file
Step 1: Create the file
# Linux and macOS mkdir -p ~/.ssh && chmod 700 ~/.ssh touch ~/.ssh/config && chmod 600 ~/.ssh/config nano ~/.ssh/config # Windows PowerShell New-Item -ItemType Directory -Force $env:USERPROFILE\.ssh | Out-Null if (-not (Test-Path $env:USERPROFILE\.ssh\config)) { New-Item -ItemType File $env:USERPROFILE\.ssh\config | Out-Null } notepad $env:USERPROFILE\.ssh\configOn Windows, make sure the file is called
config, notconfig.txt: ssh ignores the second. Creating it first, as above, stops Notepad adding the extension.Step 2: Add a Host entry for a server
Host web HostName 203.0.113.10 User deploy Port 2222 IdentityFile ~/.ssh/id_ed25519 IdentitiesOnly yesHostis the name you type,HostNamethe real address, then the user, the port and the key. The indenting is only for reading, and lines starting with#are comments. Save the file.Step 3: Check what ssh will use
ssh -G webprints every setting ssh would use forweb, without connecting. Look for thehostname,user,portandidentityfilelines.Step 4: Connect by its short name
ssh webconnects.sftp webandscp file.txt web:/tmp/use the same settings (the SFTP guide has the file transfer commands). The server itself has to run SSH, of course: here's how to enable SSH on Ubuntu.
SSH config file examples
Several servers with shared settings
Host web-01 web-02
User deploy
IdentityFile ~/.ssh/id_ed25519
Host web-01
HostName 203.0.113.11
Host web-02
HostName 203.0.113.12Every block whose Host matches applies, so web-01 gets its user and key from the first block and its address from the second.
A whole group with a wildcard
Host *.lab.example.com
User admin
IdentityFile ~/.ssh/lab_ed25519A server behind a jump host (ProxyJump)
Host bastion
HostName bastion.example.com
User admin
Host db
HostName 10.0.0.5
User admin
ProxyJump bastionssh db signs in to the bastion first and reaches 10.0.0.5 from there, the same as ssh -J bastion admin@10.0.0.5.
A port forward that opens with the connection
Host db-tunnel
HostName db.example.com
User admin
LocalForward 5433 localhost:5432While ssh db-tunnel is connected, port 5433 on your computer reaches the database on the server. See what an SSH tunnel is for the other kinds of forward.
Defaults for every server (put this last)
Host *
ServerAliveInterval 60
ServerAliveCountMax 3
AddKeysToAgent yesKeep-alives stop a quiet session from being dropped by a router or firewall. AddKeysToAgent hands your key to ssh-agent after you first type its passphrase; the SSH keys guide explains the agent.
An old device that only offers ssh-rsa
Host old-switch
HostName 192.168.1.2
User admin
HostKeyAlgorithms +ssh-rsa
PubkeyAcceptedAlgorithms +ssh-rsaRecent OpenSSH versions refuse the old SHA-1 ssh-rsa signatures by default, so connecting to old switches, routers or NAS boxes can fail with no matching host key type found. Their offer: ssh-rsa. These two lines allow it for that one device only (the second is only needed if you sign in to it with an RSA key). Versions of ssh older than 8.5 don't know PubkeyAcceptedAlgorithms and stop with "Bad configuration option"; they still accept ssh-rsa, so leave both lines out there. Updating the device is the better fix when one exists; the fix for no matching host key type found covers the related key exchange and cipher errors too.
How ssh reads the file
- The first value wins. ssh goes through the file from the top, and for each setting keeps the first value it finds in a matching block. So put specific hosts first and
Host *at the end. - Settings on the command line come first.
ssh -p 22 webuses port 22 whatever the file says. - Check the result with ssh -G. It shows the settings after all of these rules, so it settles any doubt.
- Split it up with Include.
Include ~/.ssh/config.d/*near the top reads more files, handy for one file per project. - Keep it private. ssh refuses a config file other users can write to ("Bad owner or permissions"):
chmod 600 ~/.ssh/config.
Common SSH config keywords
| Keyword | What it does | Example |
|---|---|---|
| Host | Starts an entry: the short name(s) you type. Wildcards like * and ? work. | Host web |
| HostName | The real address or DNS name to connect to. | HostName 203.0.113.10 |
| User | The user name to sign in as. | User deploy |
| Port | The server's SSH port, when it isn't 22. | Port 2222 |
| IdentityFile | The private key to use. | IdentityFile ~/.ssh/id_ed25519 |
| IdentitiesOnly | Offer only the IdentityFile key, not every key in the agent. | IdentitiesOnly yes |
| ProxyJump | Connect through another SSH server (a jump host). | ProxyJump bastion |
| LocalForward | A local port forward, like ssh -L. | LocalForward 5433 localhost:5432 |
| DynamicForward | A SOCKS proxy on your computer, like ssh -D. | DynamicForward 1080 |
| ServerAliveInterval | Send a keep-alive every so many seconds, so idle sessions don't drop. | ServerAliveInterval 60 |
| StrictHostKeyChecking | accept-new trusts new servers' keys but still refuses a changed key. | StrictHostKeyChecking accept-new |
| ForwardAgent | Lets the server use your ssh-agent. Only for servers you trust. | ForwardAgent yes |
man ssh_config on Linux or macOS lists every keyword. Most people only ever need the first five, plus Port when a server doesn't use 22.
Bring your SSH config into RemoteAccessApp
If you'd like your servers in a list with tabs and a file manager, the RemoteAccessApp SSH client can import the config file you already have:
Step 1: Open Import
In the Console, open Connections, then Import on the Hosts tab.
Step 2: Read your SSH config
Import from this PC… (on Linux, Import from this computer…) reads
~/.ssh/config. To use another file, choose your SSH config under Import from file, or Choose a file….Step 3: Check the preview and import
Each named
Hostbecomes a saved SSH host with its address, user, port and key file. ProxyJump becomes a jump host and LocalForward, RemoteForward and DynamicForward become port forwards. Wildcard entries and Match blocks are left out. You see the list before anything is added, and passwords never come in.
Good to know
Questions, answered
Related
- Passwordless SSH with keysMake an SSH key, copy it to the server with ssh-copy-id (or one PowerShell line on Windows), get the permissions right and sign in without a password.Read the guide
- SSH port 22SSH and SFTP use port 22. Connect on another port with ssh -p, change the port on a Linux or Windows server, and check it's open.Read the guide
- What is an SSH tunnel?Local, remote and dynamic SSH port forwarding with ready-to-use commands, when to use each, agent forwarding, and how to keep tunnels safe.Read the guide
- Import saved sessionsBring your saved SSH, RDP, VNC and serial sessions in from session files, a CSV or your SSH config, with a preview and no duplicates.Read the guide
- SSH to a Linux serverConnect to a Linux server from Windows or another Linux machine, check its key, and copy files.Read the guide
- SSH & SFTP clientTerminal tabs, an SFTP and FTP file manager, key sign-in and one command to every server.Learn more
