16

This is an expansion of my earlier question. I need to run a locally-stored command (i.e. mount /home) on a remote machine upon ssh login. At the moment, I am using:

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

which works fine, but I was wondering whether I could put this command into my .ssh/config so that I don't have to type it every time. I found a LocalCommand option, but I don't see any RemoteCommand option.

It is importand that the command-to-be-executed be stored on the local machine, as it will contain password to open an encrypted disk. That is the reason why I cannot put the command into .profile on the remote machine.

7

This question has been asked before on SuperUser. The first answer uses a remote file that doesn't fit your requirements, but the second answer that suggests to use a local alias could be the solution to your problem.

It is importand that the command-to-be-executed be stored on the local machine, as it will contain password to open an encrypted disk. That is the reason why I cannot put the command into .profile on the remote machine.

This is probably not safe. If I use an echo command as a ssh command, it shows up in ps on the remote server:

martin@martin ~ % ssh dev2 "echo hello; ps aux|grep echo"

hello
root     25358  0.0  0.0   5848  1296 ?        Ss   11:27   0:00 bash -c echo hello; ps aux|grep echo
root     25362  0.0  0.0   5012   828 ?        S    11:27   0:00 grep echo
  • thanks, but in the solution on SuperUser ~/.ssh/rc is stored again on the remote machine. Regarding your comment about ps showing my command line parameters (including the password), that does not bother me. It is the remote machine that I don't trust. My local machine is only used by myself, so nobody else has access to ps. – Martin Vegter Sep 21 '13 at 18:23
  • 6
    That ps output is from the remote machine. The point is: everything you supply to ssh as a remote command will show up on the remote server in ps. – Martin von Wittich Sep 21 '13 at 18:37
  • 1
    Yes, you are right. Thanks for pointing this out. – Martin Vegter Sep 21 '13 at 19:04
  • 1
    @MartinvonWittich - Each SE site is independent so it's completely acceptable for each site to have similar Q's and A's so long as they're on topic for that particular site. It's encouraged to copy content if a Q comes up and the A is on another SE site. Customize it as appropriate so it's on topic. – slm Sep 21 '13 at 22:54
7

This was added in OpenSSH 7.6:

add RemoteCommand option to specify a command in the ssh config file instead of giving it on the client's command line. This allows the configuration file to specify the command that will be executed on the remote host.

ref: https://www.openssh.com/txt/release-7.6

3

You can do this with the cooperation of the remote machine as follows.

On your local machine, stuff the command to run in the environment variable LC_SSH_INITIAL_COMMAND. SSH is usually configured to pass all environment variables of the form LC_*, because those are normally locale settings.

On the remote machine, run eval "$LC_SSH_INITIAL_COMMAND" via the per-key settings in ~/.ssh/authorized_keys or from ~/.ssh/rc (beware that if you have a ~/.ssh/rc file, you need to take care of running xauth manually for X11 forwarding).

With the authorized_keys method, you can similarly run a command when the SSH session exits (unless the shell process is killed abruptly; you should at least trap the HUP signal so that your command is executed even if the connection dies).

However this is not a good arrangement for doing something global like mounting a filesystem. You'll need to take care not to mount again if the filesystem is already mounted (for example due to a concurrently-running SSH command), and not to unmount on logout. My recommendation for this use case is still to do the mounting explicitly, then run remote commands without doing anything special, and unmount when you no longer want to access that filesystem.

2
RequestTTY yes
RemoteCommand screen -UDr
0

I have not tried it but an approach could be

  • Declare a Subsystem on the remote sshd_config that executes the command you want by reading some env vars
  • Use SendEnv on the local .ssh/config to pass such keys to the remote server

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.