I am in a situation where several users are sharing the same user account on a remote machine. I have a “personal” directory where I wrote my own .zshrc file, and I would like to have a way to:
- Start a ssh session in the remote machine with directives from my ssh config file
(e.g.ControlMaster auto) - This session runs a Z shell
- It runs a
.zshrcin my “personal” directory (not on the shared user’s home directory)
It would be nice to have an alias or a simple way of starting such ssh sessions (that’s why I thought about using the OpenSSH’s config file), but I am open to any other ideas!
Using OpenSSH’s config file?
I read on the OpenSSH’s ssh_config man page that I can use the directive LocalCommand to specify a command to run locally after successfully connecting to the server. This made me think there may be a way to tell the config file what command to run remotely after connecting to the server, but there doesn’t seem to be any.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The most obvious way to run a command remotely is to specify it on the ssh command line. The ssh command is always interpreted by the remote user’s shell.
ssh <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b0904092b0e130a061b070e45080406">[email protected]</a> '. ~/.profile; command_that_needs_environment_variables' ssh -t <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="22404d4062475a434f524e470c414d4f">[email protected]</a> '. ~/.profile; exec zsh'
Shared accounts are generally a bad idea; if at all possible, get separate accounts for every user. If you’re stuck with a shared account, you can make an alias:
ssh -t <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d3e252c3f2829602c2e2e223823390d28352c203d2128632e2220">[email protected]</a> 'HOME=~/bob; . ~/.profile; exec zsh'
If you use public key authentication (again, recommended), you can define per-key commands in ~/.ssh/authorized_keys. See this answer for more explanations. Edit the line for your key in ~/.ssh/authorized_keys on the server (all on one line):
command="HOME=$HOME/bob;
if [ -n "$SSH_ORIGINAL_COMMAND" ]; then
eval "$SSH_ORIGINAL_COMMAND";
else exec "$SHELL"; fi" ssh-rsa AAAA…== <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dfbdb0bd9facb0b2baf1a8b7baadba">[email protected]</a>
Method 2
To run a command remotely after connecting the server, add in your .ssh/config file the following snippet
PermitLocalCommand yes
Host <server-ip-address>
LocalCommand *command*
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0