Ssh config auto execute remote command

ssh

I connect to server through ssh under vpn. To make project changes I always do the same procedure after accessing server – log as project_user, because with my identity I get sh shell and I don't have write access to project files.

In my ~/.ssh/config file I tried to use LocalCommand parameter to automate the procedure:

LocalCommand sudo -iu project_user bash

and then

LocalCommand /bin/sh -c sudo -iu project_user bash

But none of this makes any change – after connecting I'm still in sh shell and I need to run above commands again and again.

Any ideas?


Update: sorry, but this LocalCommand parameter is meant for local and not remote command. Is there any ssh_config parameter that would let me execute remote command after connecting to server?
Or at least change the shell to bash so that I can execute my command through bash initialization scripts?

Best Answer

As the name indicates, LocalCommand runs a command on the local (client) machine, not on the remote (server) machine. The command that runs on the server is the argument(s) that you pass to ssh after the options and the host name.

By default, a terminal is not created when you pass a command, so pass the -t option to create one. Instead of ssh yourserver, run

ssh -t yourserver exec sudo -iu project_user
Related Question