Linux commands
Browse topics

Choosing SSH hosts and identities

Version
unversioned
Review
Draft

Summary#

A Host block gives an SSH destination a short name and keeps its login user and key together. The login key proves your identity to the server. The server's host key lets the client verify which server it reached.

Syntax#

Add a block to ~/.ssh/config for a host you use regularly. Set HostName to the server address, User to the remote login and IdentityFile to the private key. IdentitiesOnly yes keeps SSH from trying extra identities offered by the agent.

sh
ssh preview
ssh -i "$key" -o IdentitiesOnly=yes deploy@preview.example.test
ssh -G preview
ssh -vvv preview

ssh -G prints the effective settings. ssh -vvv connects and logs the authentication steps to standard error. For public-key login, find Offering public key to see which key the client tried, then Server accepts key and the final authentication result. After Permission denied (publickey), check the remote username and whether that account authorizes the corresponding public key.

Example#

This example runs offline. -F selects the temporary config and -G displays its effective settings. The generated key and its temporary path vary and are removed when the example exits.

sh
(
  set -eu
  work=$(mktemp -d /tmp/devlookup-ssh.XXXXXXXX)
  trap 'rm -rf -- "$work"' EXIT

  ssh-keygen -q -t ed25519 -N '' -f "$work/deploy"
  cat > "$work/config" <<EOF
Host preview
    HostName preview.example.test
    User deploy
    IdentityFile "$work/deploy"
    IdentitiesOnly yes
    StrictHostKeyChecking yes
EOF

  ssh -G -T -F "$work/config" preview > "$work/effective"
  awk '$1 ~ /^(hostname|user|identitiesonly|stricthostkeychecking)$/' "$work/effective"
  grep -Fx "identityfile $work/deploy" "$work/effective" >/dev/null
  printf '%s\n' 'identityfile: generated deploy key'
)

Expected output#

Text
user deploy
hostname preview.example.test
identitiesonly yes
stricthostkeychecking true
identityfile: generated deploy key

Common mistakes#

Permission denied (publickey). means that the server accepted none of the offered keys. Run ssh -v to see which identities were tried, and check that the server authorizes the public key for that user. Host key verification failed. means that SSH could not accept the server's host key, for example because strict checking found no trusted key or the key changed.

Put specific Host blocks before Host * defaults because SSH takes the first value for most settings. Multiple IdentityFile directives can add keys to the list even with IdentitiesOnly yes.

Keep host key checking enabled. StrictHostKeyChecking yes requires an already trusted host key and rejects a changed one. For a first interactive connection, StrictHostKeyChecking ask prompts before adding a key. Compare the displayed fingerprint with one you obtained through a trusted channel before accepting it. Confirm a reported key change through that channel before updating known_hosts.

Version applicability#

OpenSSH 6.5 added Ed25519 keys. The complete example also uses ssh -G, introduced in OpenSSH 6.8. The displayed settings and diagnostic wording were checked with OpenSSH 10.2p1.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0