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

share|improve this question
add comment

3 Answers

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 mymachine.example.com 'mount /home ; /bin/bash'
share|improve this answer
add comment

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
share|improve this answer
add comment

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