When working in Linux, being able to use shortcuts in the terminal can make life easier. I’ve discussed using bash aliases in the past, but there are certain use-cases where I would highly recommend using SSH config instead.
I want to give a very special thank you to Robert Lützner, who inspired this topic based on a discussion we had on Mastodon.
Table of contents
Open Table of contents
Assumptions
I’m writing with with the assumption that you understand terms like ssh
, sftp
, scp
, and remote server
. If you don’t, this may not be for you… yet. And worry not as I’ll write up something about those terms sometime in the future… probably.
This is geared to individuals who want to try to use best practices for connecting to remote servers using ssh connections.
Bash Ahoy
To put it bluntly, bash aliases are fantastic for setting up frequently used local terminal commands. Commands like sudo apt update && sudo apt upgrade
and tar -xvf
can be truncated into any alias you have set up. (I use the command yiss
to run update and upgrade on my system.)
Yes, this means you can even include connections using ssh; alias pumpkin="ssh [email protected]
. This will assume that you have your ssh key for that connection setup already.
Realistically, if you’re someone who is connecting to a single remote system using one ssh key, I don’t see a problem with using a bash alias. For example, I have a friend who only connects via ssh to their Minecraft server. That’s it.
I would be, however, remiss to not also include using OpenSSH’s .ssh/config file for handling aliases for ssh connections as I know some friends, and others, where bash aliases just won’t cut it.
When to SSH Config
If you have multiple ssh keys, connect to multiple remote servers, have many remote user accounts, use non-standard ports, have custom commands-line options, connect with scp
or sftp
or rsync
, or have a combination of the above, then you should be using the .ssh/config
file.
Why? It’s designed to handle all of the above and more. It’s also a lot easier to manage than a bunch of bash aliases.
The General Details
To start, SSH config is just a plain text file that stores the settings for your ssh connections in a specific format. There are two locations where where you can find the file:
/etc/ssh/ssh_config
- This is the system-wide configuration file. It’s used by all users on the local system.~/.ssh/config
- This is the user-specific configuration file. It’s used by the user who owns the file and usually requires you to create it.
The format for the file is made up of a series of Host
blocks, each of which contains a set of configuration options. Here’s an example of what a Host
block looks like:
# Connection info for bob
Host pumpkin
Hostname 123.456.78.90
User bob
Port 42069
IdentityFile ~/.ssh/pumpkin.key
With the above, I could then use ssh pumpkin
or sftp pumpkin
to connect to the remote server. This would be the equivalent of running ssh -i ~/.ssh/pumpkin.key -p 42069 [email protected]
.
Now, say we had another user on the same server, but we use the same key. We could add another Host
block to the file:
Host pumpkin
Hostname 123.456.78.90
User alice
Port 42069
Now I can run ssh -o "User=alice" pumpkin
to connect to the server as Alice.
If you find your new “alias” isn’t working in your terminal, close and restart all terminals and try again.
Learn more
There are a lot more options that you can use such as regex and forwarding. I recommend looking at the man
but I also suggest giving a read of the articles from Linuxize and nixCraft.