6

I am logging to a remote server via ssh as user www-data. User www-data on the server has his default shell set to /bin/sh, and when I log in, I get dash as my shell. I can then type bash and get bash shell.

I would like to log in into bash directly, when I ssh in. But I don't want to change the default shell on the server. I want my change only to affect ssh session.

I have tried putting command="/bin/bash" infront of my public key in .ssh/authorized_keys, but this has another side effect: while bash works as default shell when logging in, scp stopped working. I can no longer scp files to or from the remore server.

How can I set bash as default shell for ssh session, without breaking other applications ?

3
  • 1
    ssh [email protected] /bin/bash didn't work?
    – DopeGhoti
    Jun 16, 2016 at 5:14
  • @DopeGhoti: I'm also curious about it but I suspect it'll have the same issue as with his command=.. syntax. Jun 16, 2016 at 5:18
  • 1
    I doubt it, as specifying it on the ssh commandline should not have any effect upon scp.
    – DopeGhoti
    Jun 16, 2016 at 5:23

1 Answer 1

6

I have a similar issue on one system I use (default shell is bash, I want ksh93, and chsh doesn't work).

My solution, adapted for your situation, is to exec the desired shell from ~/.profile, which Dash reads on startup. Bash doesn't touch ~/.profile unless it doesn't find ~/.bash_profile or ~/.bash_login (in that order, see the Bash manual).

# in ~/.profile:
if [ "$SHELL" != "/usr/bin/bash" -a -n "$SSH_TTY" -a -x /usr/bin/bash ]; then
    export SHELL="/usr/bin/bash"
    exec $SHELL -l
fi

SSH sets SSH_TTY in interactive SSH sessions, so we're checking to see whether that's set (non-empty string) before making sure Bash is available and executing it. I'm setting and exporting SHELL in case any other application looks at it, and to avoid Bash running into an infinite loop due to missing both ~/.bash_profile and ~/.bash_login and thus trying to execute ~/.profile again.

3
  • thanks. With one important modification, your suggested approach works. Executing $SHELL -l caused a fork bomb. Apparently, bash -l reads .profile as well. When omitting -l, everything works as expected. Jun 16, 2016 at 10:29
  • Probably because you lack both ~/.bash_profile and ~/.bash_login?
    – Kusalananda
    Jun 16, 2016 at 10:31
  • See my edit to my answer, which makes the if statement fail if we're already running Bash. (not sure this is really required since it should pick up the other init files instead).
    – Kusalananda
    Jun 16, 2016 at 10:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .