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.

  • 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? Commented Sep 14, 2011 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) Commented Sep 14, 2011 at 23:28
  • I think this deserves an update of the OP, so I just updated it. Commented Sep 14, 2011 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. Commented Sep 14, 2011 at 23:43
  • I posted a related question: ssh, start a specific shell (ash), and source your environment on the remote machine Commented Nov 11, 2021 at 23:33

2 Answers 2


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 [email protected] '. ~/.profile; command_that_needs_environment_variables'
ssh -t [email protected] '. ~/.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 [email protected] '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):

     if [ -n \"$SSH_ORIGINAL_COMMAND\" ]; then
       eval \"$SSH_ORIGINAL_COMMAND\";
     else exec \"$SHELL\"; fi" ssh-rsa AAAA…== [email protected]
  • 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? Commented Sep 14, 2011 at 23:47
  • Hmm, when I run ssh -t [email protected] '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. Commented Sep 15, 2011 at 0:04
  • 2
    @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. Commented Sep 15, 2011 at 0:09
  • Thanks again for your help. One follow up question here. Any way to run more commands, after the exec zsh command? Commented Oct 6, 2015 at 0:05
  • 1
    @AmelioVazquez-Reina Not conveniently unless you have some control over one of the dot files. You can set the environment variable ZDOTDIR to make zsh look for dot files in a different directory. If you do have some control over the dot files then you can do something like add eval $LC_STARTUP_CODE to $ZDOTDIR/.zshrc and pass the code to execute in the environment variable LC_STARTUP_CODE. Commented Oct 6, 2015 at 10:07

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*
  • 2
    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. Commented Sep 14, 2011 at 22:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .