Using an Alias for Handling ssh Sessions

tl;dr

If you want to ssh into your phone e.g. “ssh myphone”, put the following lines in ~/.ssh/config

  • Host myphone
  • Hostname [the ip addres of the phone]
  • Port [the port number needed for the session]

In my case I used:

  • Host myphone
  • Hostname 192.168.1.39
  • Port 2222

You can use your favorite text editor or you can easily echo the details into the config file with a cli one liner:

  • echo -e “Host myphone\nHostname 192.168.1.39\nPort 2222” >> ~/.ssh/config

Once this is done, you can now immediately ssh into a device with:

  • ssh myphone

Amazingly, linux even gives you access to the autocomplete feature (this time it’s populated from the Host head line in the config file). So, this will work also:

  • ssh m[tab]

Now the Details

Why use ssh with an alias?

In a previous tutorial I showed how to ssh into an Android device from a device running Linux. Once you have several devices that you are accessing by ssh, it can get a little taxing to remember all of the ip addresses associated with each device. One solution is to use a form of a Short Code officially known as an alias. In the world of telephony Short Codes are 3 to 5 numbers that translate to the full 10+ digits of a typical phone number – thus making them easier to use – think “911.”

By using an alias with ssh, we will be able to ssh into devices with mnemonic formulas such as

  • “ssh myphone”, or
  • “ssh pi3”, or
  • “ssh rooster-tablet”, or even
  • “ssh a”

It’s more difficult to remember and utilize the fully expanded ip address and port number which is more like:
- “ssh -p 2222 192.168.1.41.”

Using aliases to manage ssh targets will make everything cleaner, easier to read, less prone to mistakes, and easier to remember.

To implement such a system we will be dealing with the current local user’s ssh config file found at ~/.ssh/config

ssh session parameters and the ~/.ssh/config file

There are 3 ways to control the parameters of ssh sessions. Parameters are specified for the session in the following order:

  1. parameters specified on the command line interface (cli)
  2. parameters specified in the current local user’s ~/.ssh/config file and
  3. parameters specified in the global /etc/ssh/config file

The way this hierarchical order works is that once a parameter is set, it won’t be reassigned in the current session. So, if we specify a port number on the cli, as in “ssh -p 2222”, the port settings in the local user’s config file and the port settings in the global config file will be ignored. But if we don’t specify a port on the cli, the parameter will be assigned from the local user’s config file. If there is no port setting in the local user’s config file, the parameter will be assigned from the global config file.

We will modify the current local user’s config file to control the way the ssh session works. This method will allow us to make the cli terse when using ssh to access a device.

Step #1 – Backup of the local user’s ssh config file.

This enables us to always get back to where we started if something goes wrong. Append today’s date as the extension of the copied file to keep it unique:

  • cp ~/.ssh/config ~/.ssh/config.20210111

Step #2 – Understand ssh’s config file format

The current local user’s ssh config file can be used to declare parameters for multiple ssh targets. Each line of the config file is made of pairs of keywords and arguments assigned to those keywords. The keywords and arguments are separated by any number of blanks and tabs but must be on the same line. Lines that begin with “host” or “Host” (keywords are case insensitive) start a session’s parameters. That line can be considered to be the “session head” line. All lines following the “session head” are read and included as additional parameters of that session. When the interpreter encounters the next “session head” line, the declaration of parameters stops. It looks like this:

Host 	myphone         # the alias name of the ssh target
Hostname 192.168.1.39	# the real name of the ssh target
port 2222				# the port for the ssh target
Host 	tablet			# "Host" starts a new set of parameters
Hostname 192.168.1.66
port 2222

Step #3 – Edit the ~/.ssh/config file

Use your favorite text editor to add the new host designation line to the ~/.ssh/config file. Add the following lines:

Host	myphone		# the alias you choose to use for the target device
Hostname 	192.168.1.39	# the ip address of the target device
Port	2222		# the listening port number of the target device

Another easy way to place the parameters in your local ssh config file is to echo the parameters into it. Here is how to do it with 3 echo commands:

echo "Host myphone" >> ~/.ssh/config
echo "Hostname 192.168.1.39" >> ~/.ssh/config
echo "Port 2222" >> ~/.ssh/config

Or you can do it all with a single cli command:

echo -e "Host myphone\nHostname 192.168.1.39\nPort 2222" >> ~/.ssh/config

Special Notes Regarding the config file:

  1. The keyword part of the keyword-argument pair is case insensitive but the argument is case sensitive.
  2. Linux even gives you access to the autocomplete feature (this time it’s populated from the Host head line in the config file). So, this will work also:
    • ssh m[tab]
4 Likes

Ha! Stupid me, I just have a sticky note of IP addresses stuck to my computer. This is way nicer

2 Likes

Very well written. Thanks for posting this, I’m sure I’ll get plenty use of this.

1 Like

I forgot to add this gem:

Linux even gives you access to the autocomplete feature (this time populated from the Host head line in the config file). So, this will work also:

ssh m[tab]

I forgot to add this gem:

Linux even gives you access to the autocomplete feature (this time populated from the Host head line in the config file). So, this will work also:

ssh m[tab]