ssh-config Name your hosts and choose which key each one uses

The file ~/.ssh/config holds per-host settings for ssh. Give each router, NAS or service a short name, set its user, address and key once, and then connect with just ssh name. This is also where the macOS Keychain settings go.

Synopsis

~/.ssh/config

Common options

Option or formWhat it does
Host NAMEStarts a block. Settings below it apply when you run ssh NAME. Use Host * for settings that apply to everything
HostName ADDRESSThe real address or domain to connect to, such as 192.168.1.1
User NAMEThe login name on that machine
Port NUMBERThe port, if it is not the default 22. Many NAS devices use a different one
IdentityFile PATHThe private key to use for this host
IdentitiesOnly yesOffer only the key named here, not every key in the agent. Stops "too many authentication failures"
AddKeysToAgent yesLoad a key into the agent the first time it is used
UseKeychain yesRead and save the passphrase in the macOS Keychain. This line is macOS only

Examples

Keychain settings for every host
Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519
A router you can then reach with ssh router
Host router
  HostName 192.168.1.1
  User admin
  IdentityFile ~/.ssh/id_ed25519
  IdentitiesOnly yes
A NAS on a custom port with its own key
Host nas
  HostName nas.local
  User bob
  Port 2222
  IdentityFile ~/.ssh/id_rsa_nas
  IdentitiesOnly yes
GitHub with a dedicated key
Host github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_github
  IdentitiesOnly yes
Create the file with safe permissions
touch ~/.ssh/config && chmod 600 ~/.ssh/config

Notes

See also

Last reviewed 2026-10-12.