Force SSH to use a specific shell

Is there any way to force SSH to use a particular shell on the remote end, regardless of what the user's default shell is?

I've tried solutions akin to:

ssh host.domain.com /bin/bash -c 'complicated, multi-line command'

but unfortunately the default shell on the remote end is responsible for parsing the "complicated, multi-line command" part, and I'm having difficulty escaping it sufficiently to work both for Bash and C shell users.


Solution 1:

I don't believe this is possible, at least with openssh-based systems. If you have the ability, a better solution might be to sftp up a shell-script file, and then execute it with the method you posted. It would have the advantage of minimizing the amount of escaping needed, but would leave a file behind that would have to be removed (perhaps as the last step of the script).

Solution 2:

You can use the -t option to force allocation of a pseudo-tty for the program you want to start, as if you were executing the standard shell. Then pass the shell you want as a plain old argument.

With this technique you're able to not only use any shell that's installed but you can also open vim and other programs which require a TTY, from a single command. Which is cool if you're writing a shell script that logs you in somewhere and opens a file on vim, or htop or something.

Here's bash

me@my-machine $ ssh root@myhost -t bash
root@myhost:~# 

sh works too. As does anything else really.

me@my-machine $ ssh root@myhost -t sh
# 

Not sure whether this is a login shell but there's options to make bash act like a login shell, so your shell might have that too.