Can i used these aliases in some sort of config file?

I have many of these kinds of aliases, the part before = is the host in ssh config

alias sshConfigHost='ssh -t hostname docker exec -it containerName bash'

Could I add these to some sort of config file? The hostname is defined in ssh config so it’s being used twice in a way, could I have a place holder like %<whatever> that it would take the hostname from ssh config.

If not then could I at least have the container name in my ssh config, then pull it into the cmd using a placeholder?

Asked By: Nickotine


I assume you want to use your alias without any trailing "arguments" (i.e. in a shell you want to call sole sshConfigHost, not something like sshConfigHost -c "shell code here").

If so, you can put everything in your ~/.ssh/config:

Host friendlyName
   RequestTTY yes
   Hostname hostname
   RemoteCommand docker exec -it containerName bash

(In case there is Host * or so, keep in mind that for each parameter, the first obtained value will be used.)

Now you just do:

ssh friendlyName

and ssh will act as if you did:

ssh -t hostname 'docker exec -it containerName bash'

Define as many Host … sections as you wish. Some of them may use the same Hostname and different RemoteCommand, or whatever. The point is you can build a rich config, so in a shell it’s enough to ssh oneword and the config will fill in everything for you, depending on oneword.

The config file is deliberately so powerful, it can specify every option. See man 5 ssh_config. I felt intimidated by this concept (why bother with some "obscure" config while I can specify options and such in the command line like with any other tool?) until I discovered it’s neat and convenient. It is the Right Way to use ssh. In general if there is anything you would need to repeat again and again when calling ssh, consider putting it in the config file.

And then there is no need for accompanying aliases in the shell. The whole config is in your ~/.ssh/config.

Answered By: Kamil Maciorowski
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.