Sign up ×
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 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 'mount /home ; /bin/bash'

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

ssh 'mount /home'

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.)

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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 'mount /home ; /bin/bash'
share|improve this answer

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.

share|improve this answer

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 'mount -o password=swordfish …; exec sleep 999999999'
ssh ls
share|improve this answer

Your Answer


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.