Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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

share|improve this answer
    
This is what I eventually did, but using scp. A great idea. –  plinehan Jul 21 '10 at 3:09
add comment

Use a heredoc:

ssh host.domain.com /bin/bash << EOF
big ugly commands
lots of them
EOF
share|improve this answer
    
shouldn't you use "-s" for bash to read commands from stdin? –  Weboide Jul 20 '10 at 1:12
    
It isn't always required. –  Ignacio Vazquez-Abrams Jul 20 '10 at 2:10
    
+1 for a great answer, but I'm selfishly "accepting" sysadmin1138's answer because his solution matched mine. My script already had a heredoc embedded inside of it, and I had a mental block against putting a heredoc inside a heredoc. Mental block busted! –  plinehan Jul 21 '10 at 3:11
    
+1, you rock!!! thanks!!!!!! –  Dean Hiller Feb 26 '13 at 22:21
add comment

Use key-based logins, not password-based. Then you can add a (list of) "forced command(s)" to your public ssh key (in the "options" field in case of SSH1) which is installed on the server (in ~/.ssh/authorized_keys file for SSH1, ~/.ssh2/authorization for SSH2).

Make your forced command so that your desired shell is called...

More: You can associate at most one forced command to a given key. If you require multiple forced commands for different purposes, you have to setup different keys. (Of course you can put multiple things into one script, which you call via forced command. But be aware that forced commands are always run for a given account/key if the user logs in, regardless if he asked for something different to run. If you want to still honor the original command asked for, have a look into how to exploit the $SSH_ORIGINAL_COMMAND variable...)

Read up about the "forced commands" via Google or at O'Reilly.

share|improve this answer
    
Good stuff. That graphic on the O'Reilly page is very nice. In my particular case, however, I want to be able to force this for any users, not just users who have set up their keys correctly. I also don't have root on the server machines, so I can't edit files like /etc/sshrc. –  plinehan Aug 2 '10 at 18:30
    
Well, it is always the server (or better: the one who exerts control over the server) who calls the shots when you connect to its service.... The 'owner' of the server decides what can be done with it. -- You cannot force anything 'for any user(s)' if you don't have higher privileges than them. –  Kurt Pfeifle Aug 2 '10 at 20:25
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.