Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:

  1. Start a ssh session in the remote machine with directives from my ssh config file (e.g. ControlMaster auto)
  2. This session runs a Z shell
  3. It runs a .zshrc in 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.

share|improve this question
    
I don't think you can. But I'm puzzled as to why you want this. There's already a command on the ssh command line. Why can't you run ssh mycommand? And if you want to run some setup command before every command that comes over ssh, why not configure the server side? –  Gilles Sep 14 '11 at 23:22
    
Thanks @Guilles. I'm in a situation where multiple users are sharing the same remote account, so I would like to quickly set up a few things as I log in remotely. More specifically, I'd like to start a Z shell and ask it to run a .zshrc in a specific directory (i.e. a "personal" home directory). I tried ssh -t host_name 'zsh & source /path/to/my_zshrc' but it didn't work (I got FPATH variable not defined, and I think it is because zsh finishes before it runs my_zshrc, let alone this didn't give me a Z shell) –  user815423426 Sep 14 '11 at 23:28
    
I think this deserves an update of the OP, so I just updated it. –  user815423426 Sep 14 '11 at 23:39
    
Having gone looking for a similar answer of mine, it occurs to me that this question is pretty close to what you're trying to do. –  Gilles Sep 14 '11 at 23:43
add comment

3 Answers

up vote 6 down vote accepted

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 bob@example.com '. ~/.profile; command_that_needs_environment_variables'
ssh -t bob@example.com '. ~/.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 shared-account@example.com '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…== bob@some.where
share|improve this answer
    
Fantastic @Guilles. Your contributions to this site are making it a tremendously useful resource for all of us. Thanks so much, really. By the way, I didn't know about the exec command. How is exec zsh different from calling zsh directly? Why is it important in this particular case? –  user815423426 Sep 14 '11 at 23:47
    
Hmm, when I run ssh -t shared-account@example.com 'HOME=~/bob; exec zsh' I get HOME=~/bob: command not found. I think I am on tcsh. I get the same problem if I try HOME=/home/bob. I tried on bash and zsh. Any clues what may be causing this? Finally, would the syntax above leave me with the ssh session open? (what I want). Thanks again. –  user815423426 Sep 15 '11 at 0:04
1  
@intrpc exec replaces the original shell with zsh; without zsh, the original shell would remain in memory until zsh exits and then exit. The main difference is saving a bit of memory, it's not very important. If your login shell is (t)csh, use setenv HOME ~/bob; exec zsh. Finally, since zsh is started with no argument, you get a shell session, like you'd get if you just ran ssh and had zsh as your login shell. –  Gilles Sep 15 '11 at 0:09
add comment

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*
share|improve this answer
1  
Thanks @user626129, but as I mentioned in the original question the LocalCommand directive is to run a command on the local shell , not on the remote shell. –  user815423426 Sep 14 '11 at 22:56
add comment

Goto your ssh_config file. For the global one use /etc/ssh/ssh_config & add this

PermitLocalCommand yes 
LocalCommand touch out

This will create a out file in your home directory everytime you ssh to a server.

share|improve this answer
    
Thanks @shadyabhi, but as I mentioned in the original question the LocalCommand directive is to run a command on the local shell, not on the remote shell. –  user815423426 Sep 14 '11 at 22:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.