Chapter 1 · Lesson 19 of 19
Server basics

SSH config file shortcuts

The SSH config file stores per-host settings such as user, port and key, so a short alias replaces a long ssh command.

By the end you will

Give a server a short alias in ~/.ssh/config so ssh myserver replaces a long command.

Time
12 min
Level
Beginner
Check
4 questions
You need
  • A server you already log in to with an SSH key

With the SSH config file, a short entry in ~/.ssh/config turns a long command into ssh myserver. Once you manage two or three servers, commands like ssh -p 2222 -i ~/.ssh/work_ed25519 deploy@203.0.113.10 get tiresome and easy to mistype. The same details also have to match in scp and rsync. A short entry in your SSH config file turns all of that into ssh myserver.

How it works#

When you run ssh <name>, the client reads the SSH config file ~/.ssh/config on your own computer, then the system file /etc/ssh/ssh_config. It collects every Host block whose pattern matches the name you typed. For each option, the first value found wins. This is why specific entries go at the top and the catch-all Host * goes last.

The alias is only a label on your side. HostName holds the real address that is used for the connection. Everything you put in the entry (user, port, key) is used as if you had passed it on the command line, and a flag you type directly still overrides the file.

Because scp, rsync -e ssh and git over SSH all run ssh underneath, they use the same file. One entry works for all of them.

Useful options:

OptionMeaning
HostNameThe real IP address or domain
UserLogin name
PortSSH port, if not 22
IdentityFilePrivate key to use
IdentitiesOnly yesOffer only the named key, not every key in the agent
ProxyJumpReach the host through another SSH host
ServerAliveIntervalSend a keepalive every N seconds

Step 1: Create or open the file#

On your own computer, not the server:

bash
mkdir -p ~/.ssh
nano ~/.ssh/config

What you should see: an empty editor if the file is new, or your existing entries.

Step 2: Add a host entry#

output
Host myserver
    HostName 203.0.113.10
    User deploy
    Port 22
    IdentityFile ~/.ssh/id_ed25519
    IdentitiesOnly yes

What each line does:

  • Host: the alias you type.
  • HostName: the real IP address or domain.
  • User: the login name.
  • Port: the SSH port, only needed if it is not 22.
  • IdentityFile: the private key to use.
  • IdentitiesOnly: use only that key, which avoids trying many keys from your agent.

The indented lines belong to the Host above them. Indentation is for readability, but a blank line or new Host ends the block.

Step 3: Set permissions#

SSH refuses a config file that others can write to (see file permissions):

bash
chmod 600 ~/.ssh/config

What you should see: no output.

Step 4: Add shared defaults (optional)#

Settings under Host * apply to every host. Put it last, because the first value found for each option wins:

output
Host *
    ServerAliveInterval 60
    AddKeysToAgent yes

ServerAliveInterval helps keep idle sessions from being dropped. AddKeysToAgent yes adds a key to your agent the first time you unlock it.

Step 5: Reach a private server through a jump host (optional)#

If a server has no public address, connect through one that does:

output
Host private-db
    HostName 10.0.0.5
    User deploy
    ProxyJump myserver

Now ssh private-db logs in to myserver first and then opens the real connection from there. Your private key stays on your own computer.

Check it worked#

bash
ssh myserver

You should land in a shell on the server with no need to type the user, IP or key. To see exactly what SSH will use, run:

bash
ssh -G myserver | grep -E '^(hostname|user|port|identityfile) '

The output should show the values from your entry, each in lower case on its own line.

Edge cases and gotchas#

  • Order matters, first value wins. If a general block above your entry sets User, it beats the user in the entry below. Keep specific hosts first and Host * last.
  • Patterns match the alias, not the real name. Host web-* matches what you typed after ssh, not the HostName. A pattern like *.example.com matches only if you typed that full name.
  • Too many keys cause failures before yours is tried. An agent holding many keys can hit the server’s attempt limit. IdentitiesOnly yes fixes it.
  • Windows and WSL use different home folders. The file lives under the user profile of whichever environment runs ssh. An entry in WSL is invisible to the Windows client, and the reverse.
  • IPv6 HostName values need no brackets. Write HostName 2001:db8::10 here. Brackets are only for scp and rsync targets on the command line.
  • A changed host key still warns. The config does not hide host key changes. See What is SSH for what to do.
  • Do not store secrets in the file. It holds paths and names, which is fine. Never paste a private key or a password into it.

Troubleshooting#

SymptomLikely causeFix
Bad owner or permissions on /home/you/.ssh/configFile is writable by others, or owned by another userRun chmod 600 ~/.ssh/config and check the owner
Could not resolve hostname myserverTypo in Host, or you are editing on the wrong machineCheck the spelling and that the file is on the computer you connect from
Permission denied (publickey)Wrong IdentityFile, or the public key is not on the serverFix the path, see Log in with SSH keys
Too many authentication failuresThe agent offers many keys before the right oneAdd IdentitiesOnly yes
Settings seem ignoredAn earlier block already set that optionRun ssh -G myserver to see the effective value and reorder
/home/you/.ssh/config: line 4: Bad configuration optionTypo in an option nameFix the named line
ssh: connect to host ... port 22: Connection refused using the aliasHostName or Port in the entry is wrongRun ssh -G myserver and check both
The alias works in the terminal but not in a GUI toolThe tool does not read ~/.ssh/configUse the full host, user and key in the tool

Real-world scenario#

You manage a web server, a database host that has no public address, and a staging box. Typing the full command each time led to mistakes, once with the wrong key.

You write three entries in ~/.ssh/config: web, staging, and db with ProxyJump web. A Host * block at the bottom sets ServerAliveInterval 60. You run chmod 600 ~/.ssh/config.

Now ssh web, ssh db and scp backup.sql db:/tmp/ all work. When something breaks you run ssh -G db and read the user, port and key it will use, which finds the typo in seconds.

Quick reference#

OptionExample
HostHost myserver
HostNameHostName 203.0.113.10
UserUser deploy
PortPort 2222
IdentityFileIdentityFile ~/.ssh/id_ed25519
IdentitiesOnlyIdentitiesOnly yes
ProxyJumpProxyJump web
Checkssh -G myserver

Next#

You have finished the Server basics track. Continue with Install Docker on Ubuntu.

Check what you learned4 questions
1Which file holds the alias settings, and on which machine?
2Where should a Host * block go in the file?
3What does ssh -G myserver do?
4You reach a private server through a public one. Which option sends the connection through it?

Frequently asked questions

Does the config file work with scp and rsync?

Yes. They use SSH underneath, so scp file.txt myserver:/tmp/ and rsync -av ./dir/ myserver:/tmp/dir/ both use your aliases.

Can I use one alias for several servers?

Use a pattern such as Host web-* for shared settings, and define each server with its own Host block for the HostName.

Why do I get Too many authentication failures?

Your SSH agent offers every key it holds and the server gives up after a few. Add IdentitiesOnly yes to the host entry so SSH offers only the key named by IdentityFile.

Do I put this file on the server?

No. It is the client config, read on the computer you connect from. The server's settings live in /etc/ssh/sshd_config, which is a different file.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios