Preamble
I work in an environment with a lot of servers and developers. The servers are all generally set up for ease of use, so we all log into a common account. These are internal-only servers, so security is not a large concern. Most devs, myself included, add their public key to ~/.ssh/authorized_keys
.
I would like to have my usual environment customisations available after logging in.
What I do currently manually
studog@doghouse:~# ssh example.local
common@example:~# . ./studog_profile
common@example:~#
I'd like to have this occur automatically instead.
Extra credit: I'd like to always push my current .studog_profile
and so I always have my current customisations available.
Ideally:
studog@doghouse:~# ssh example.local
common@example:~#
and .studog_profile
has already been sourced and my customisations are available.
Constraints
- My
.studog_profile
contains environment variable setup including PATH changes, aliases and function declarations. - I can push uniquely-named files to servers (and modify
.ssh/authorized_keys
) but I can't change common configurations like~/.bashrc
or/etc/ssh/sshd_config
. - I often ssh from my doghouse, but often ssh between servers. A solution needs to work in both situations. I don't expect to be able to push my custom profile between servers though.
Things that don't work
ssh -t example.local '. ./.studog_profile; bash -l'
. This does run my profile, but then the environment changes are lost when the login shell runs.- Altering
.bashrc
on the servers. See the constraint above about altering common configurations. Nevertheless, I could probably get away with it, but, ssh doesn't provide a way to see what the 'start of the ssh pipe' is.SSH_CLIENT
andSSH_CONNECTION
contain the IP address of the originating server for the current hop, but there is nothing containing the identity of the machine on which I'm typing.
Questions I've read that don't work for me
- Run ssh and immediately execute command - Stack Overflow
How to run command after ssh connection? - Super User
These usebash -l
which loses changes. - Can I ssh somewhere, run some commands, and then leave myself a prompt? - Stack Overflow
Top answer shows how to inject environment variables but not aliases or functions. Also my profile is quite large, and this looks unwieldy. - How can I run a script immediately after connecting via SSH? - Unix & Linux Stack Exchange
Alters common configuration, which I might get away with but only works for the 'from the doghouse' not 'between servers' situations.
Things I haven't tried
- SSH, run a command on login, and then Stay Logged In? - Server Fault
An expect script is often mentioned. I believe that would work. It just seems... hacky? inelegant? the wrong solution? Something rubs me the wrong way.
Expectation
ssh has some method for sending a post-login command to the remote server, likely a keyword in ~/.ssh/config
. I haven't been able to find anything. I find this strange because accepting keyboard input and sending it to the remote server is nearly the same as reading a command from the config file and sending it to the remote server.
I have to think this is missing because it's a security hole, but I can't see what that hole might be.
I expect I could modify OpenSSH source and build what I need, but that's time consuming. Also, if it's a security hole my pull request is likely to be ignored.
I expect that I have covered all the bases. I'm posting in the hopes I've missed something.