ssh configuration file environment variable?

9,243

This is a bit misusing of the whole concept and twisting the tools to your specific use case. You should rather use the tools as they should be. The configuration files are by default in ~/.ssh/config.

  • Why not to create a symlink from there to your ~/workspace/etc/ssh_config on all three computers to make it work out of the box without all that work around?

  • Since OpenSSH 7.3 you can use Include directive to achieve the same without symlinks (so you can have also different configuration on some hosts if you need).

  • The same trick can be done with system-wide configuration in /etc/ssh/ssh_config.

As you have already have ... add a reference to my custom bashrc ... you can put together also modification of the .ssh/config together in some script in your workspace.

Share:
9,243

Related videos on Youtube

Lucio Crusca
Author by

Lucio Crusca

Updated on September 18, 2022

Comments

  • Lucio Crusca
    Lucio Crusca almost 2 years

    I need to login into several remote ssh servers during my admin work. I work with three different computers client side, all of them are Debian GNU/Linux systems. I keep a workspace directory where I put everything I need to do my job, and that directory contains, among other things, a bashrc file and a ssh_config file.

    I manually sync that directory contents with a script that uses rsync from one client computer to the others as I move and start using a different client.

    The local user on each of the three clients needs minimal configuration, so that if I need to create a new local user or reinstall a client, I have only to rsync the workspace and to add a reference to my custom workspace/bashrc in the real user's $HOME/.bashrc. My custom workspace/bashrc creates several aliases and I get my usual environment regardless of the client I'm using.

    My custom workspace/bashrc creates aliases to connect to remote servers this way:

    alias s1='ssh -F ~/workspace/etc/ssh_config server1.example.com'
    

    and I log into server1 by just typing s1 in a terminal of any client, because my custom ssh_config sets the needed public key authentication options, the correct port server1 is listening on and the correct user.

    Now this works like a charm, as long as the ssh command is what I need. Unfortunately, as soon as I need another command that in turn uses ssh, things become more chaotic.

    E.g. if I need to rsync anything to one of those servers, I'm forced to write the whole ssh command:

    rsync -Pavz --delete -e "ssh -F $HOME/workspace/etc/ssh_config" ...
    

    Similar things happen with other commands such as scp, ssh-copy-id and others.

    I'd like to be able to write

    rsync -Pavz --delete -e "ssh" ...
    

    instead, and have ssh take it's configuration file name from a environment variable, so that I can set that environment variable in my bashrc and every ssh invocation automagically works.

    Is there such environment variable or is there a different solution to the problem?

  • Lucio Crusca
    Lucio Crusca about 7 years
    The first proposed solution (symlink to ssh_config) does not fit my needs, because I need my custom bashrc for other things too, and the local .ssh/config on each computer does have other configurations in place. However the second idea (Include) with the last hint (modify .ssh/config in my bashrc) is the perfect solution for me. I've already implemented that and it works perfectly, thanks.