I am looking for a way to execute a local command when logging into a remote machine via SSH. (I want to stay logged in after the command has finished.) In other words, I want to specify the command on my command line. It should be executed on the remote machine and then I should get the shell, as if I had logged in normally.

For instance, let's say, I want to mount /home on the remote machine before I get the shell. I would do something like this (which does not work well)

ssh mymachine.example.com 'mount /home ; /bin/bash'

In effect, I am looking for a way how to condense the following two commands into one:

ssh mymachine.example.com 'mount /home'
ssh mymachine.example.com

Does anybody have any idea how to do it?

IMPORTANT: I don't want to store the command-to-be-executed on the remote machine. It must be stored locally (perhaps it contains my password to de-crypt the encrypted home.)


You are likely looking for the -t option for ssh which forces pseudo-terminal allocation - without that you are just directly connecting your terminal to standard input/output/error of the remotely running programs. Hence your command should look like this:

ssh -t mymachine.example.com 'mount /home ; /bin/bash'

I recommend an alternate approach: use a master connection. First start a master connection that performs the mounting and then sleeps forever, then use slave connections that require no authentication and so are very fast.

ssh -M mymachine.example.com 'mount -o password=swordfish …; exec sleep 999999999'
ssh mymachine.example.com ls

You could use the ssh's rc files.

File ~/.ssh/rc is executed before de user's home becomes avalaible, so you can put all your stuff there.

The caveat is that these files reside on the server.

man sshd could be of help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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