$_ CLI In Depth
Learning progress 0%
lesson networking sockets ssh

SSH Keys & ~/.ssh/config

3 min read Module 16: Networking Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:25

Keys instead of passwords

An SSH key pair has two halves. The private key stays on your computer and must never be shared. The public key is a single line that you place in the authorized keys file of a server. When you connect, the client proves that it holds the private key without sending it, and the server accepts the connection. Passwords can be guessed and reused. A private key is much harder to steal in a useful form, and it can be protected with a passphrase.

By convention the keys live in the .ssh directory of your home directory. Common names are id_ed25519 for the private key and id_ed25519.pub for the public one. The directory itself must be private, and the private key file must not be readable by other users.

Permissions that ssh checks

The ssh client refuses to use a private key that other users can read, and it warns when the .ssh directory is too open. The usual modes are 700 for the directory and 600 for private files and the config file. Setting them is a standard step.

BASH3 lines
mkdir -p .ssh
chmod 700 .ssh
ls -ld .ssh

The listing shows the directory with the owner-only permissions. In the lab the home directory is /workspace, so .ssh here is /workspace/.ssh, which is the same layout you would use on your own machine.

The config file

The file .ssh/config lets you define a host alias with its real name, user, port, and key. A block begins with a Host line, and the indented lines below it apply to that alias. The first value found for each option wins, which is why general defaults belong at the end, in a block with Host *.

TEXT6 lines
Host devbox
  HostName 203.0.113.10
  User deploy
  Port 22
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes

With this block, the command ssh devbox connects to 203.0.113.10 as the user deploy, using only the named key. The address shown is a documentation address reserved for examples, so the block is safe to read and copy.

Tip

Set IdentitiesOnly yes for each host that has its own key. Without it, the client offers every key in your agent, and a server with a limit on failed attempts can disconnect you before it reaches the right one.

Defaults for every host

A block with Host * applies to every alias that did not match an earlier block. It is the right place for settings you want everywhere, such as keepalive intervals. Because the first match wins for each option, a specific block placed after Host * does not override it, so keep the specific blocks first.

Copying files is a separate step

The next unit covers the programs that copy files over SSH. The config file helps them too, because the alias works in every command that takes a host.

Hands-on: write the alias file

The lab asks you to create a config file with a host alias, set its permissions, and record the aliases you defined. The check reads the file, so the exact text matters. Practice the layout above, with HostName and IdentityFile lines indented under the Host line.

Terminal lab · 2 steps · real Linux in your browser

Write an ssh config file

Create the .ssh directory and a config file with a host alias, then list the aliases.

01
Write a host alias
Create the directory .ssh in /workspace with owner-only permissions, then write the file .ssh/config with a host block named devbox. Set the file mode to 600: ``bash mkdir -p .ssh chmod 700 .ssh cat > .ssh/config <<'EOF' Host devbox HostName 203.0.113.10 User deploy IdentityFile ~/.ssh/id_ed25519 IdentitiesOnly yes EOF chmod 600 .ssh/config ``
Show hint for step 1
The Host line starts the block, and the indented lines below it are its options. The file mode must be 600.
02
List the host aliases
Add a second host block named staging to .ssh/config, with HostName 203.0.113.11. Then write the host aliases, one per line, to hosts.txt in /workspace. Use grep and awk to read the Host lines: ``bash grep '^Host ' .ssh/config | awk '{print $2}' > hosts.txt ``
Show hint for step 2
Keep the Host lines at the start of each line, with no indentation, so grep can find them.
Check yourself Question 1 of 4
Concept Check Private key permissions recall

Which file mode should a private key file have so that only its owner can read it?

Recap & Key Takeaways

SSH uses a key pair: the private key stays on your machine, and the public key is placed in the server's authorized keys.
The ssh client expects private keys and the .ssh directory to be private, with modes such as 600 for files and 700 for the directory.
In .ssh/config, a Host block gives an alias its HostName, User, Port, and IdentityFile, and the first matching value for each option wins.
The lab image has no ssh client, so this unit practices writing and checking the config file only.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue