specify shell for ssh session

14,215

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.

Share:
14,215

Related videos on Youtube

Tam Borine
Author by

Tam Borine

Updated on September 18, 2022

Comments

  • Tam Borine
    Tam Borine over 1 year

    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 ?

    • Admin
      Admin almost 8 years
      @DopeGhoti: I'm also curious about it but I suspect it'll have the same issue as with his command=.. syntax.
    • Admin
      Admin almost 8 years
      I doubt it, as specifying it on the ssh commandline should not have any effect upon scp.
  • Tam Borine
    Tam Borine almost 8 years
    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.
  • Kusalananda
    Kusalananda almost 8 years
    Probably because you lack both ~/.bash_profile and ~/.bash_login?
  • Kusalananda
    Kusalananda almost 8 years
    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).