Bash – specify shell for ssh session

bashscpshellssh

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 ?

Best Answer

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.

Related Question