Configuring default user and key for remote SSH login

Yet again I found myself in need of configuring ssh to use a specific username and identity key when connecting to a specific remote server, and yet again, I had forgotten how to. Either a case of Teflon brains, or not enough regular practice — hopefully the latter;P

So, as a mental note for later, this is how you configure ssh to automatically use specific (i.e., different than your local) user name and key when logging into a remove server, by stuffing this into ~/.ssh/config.:

Host shortname
HostName fully-qualified-name-or-ip
IdentityFile %d/path/to/file.pem
User remoteusername

Where shortname as a nick name of the remote server, e.g. rasmus and the fully-qualified-name-or-ip is rasmus.uib.no or 129.177.13.13. The PEM file is given by %d/path/to/file.pem, where %d expands to you your local home directory, e.g. /home/karltk. The remoteusername is your remote name, e.g. st04178.

Note: If you already have the ssh-agent running, and you’re okay with the key being available for all hosts, adding ssh-add ~/path/to/file.pem to your environment file, e.g. .zshenv, will also work.