1

I regularly have to log onto remote hosts via SSH and I like to use the bash option set -o vi and I normally have to cd to a specific directory. Since these hosts are ephemeral (they are created and destroyed regularly) I can't log onto them and save these in a .bash_profile/.bashrc. So I was looking into how I can do this when I connect via SSH so I don't have to type the same commands each time I connect to a remote server. I've tried several ways to achieve this but none of them seem to work. Can someone help me get this command right? In a nutshell this is what I want to do.

  1. cd to a specific directory
  2. set the bash option set -o vi

Here's how I've tried to do it:

ssh root@remotehost.com -t "cd /data; exec bash --login -c \"set -o vi\""

I feel that this actually works but that the SSH session terminates because once the command set -o vi is executed the session disconnects with a message like:

Shared connection to remotehost.com closed.

Is there a way to keep the session open after executing these commands or is there another way to achieve what I want?

4 Answers 4

4

Some options:

ssh -t root@remotehost.com 'cd /data && exec bash --login -o vi'

(that works with all POSIX-like shells including ksh/zsh/dash... as -o is a standard option of the POSIX sh utility and vi happens to be one of the standard options. That command line is also compatible with shells of all major shell families including Bourne, csh, rc, fish so should work regardless of the login shell of the root user over there)

Or:

ssh -t root@remotehost.com 'cd /data && exec env SHELLOPTS=vi bash --login'

Actually, you don't want to use that second one. That means the $SHELLOPTS variable will be in the environment and affect all bash invocations (and the non-interactive ones will inherit the ones from interactive shells causing all sorts of problems).

Also beware that bash has two sets of options, one you set with -o/$SHELLOPTS and one you set with -O/$BASHOPTS).

3
  • first option worked perfectly Nov 30, 2017 at 12:16
  • Since it works for various shells, you could even make it slightly more general with ... exec $SHELL --login ...
    – Jeff Schaller
    Nov 30, 2017 at 15:44
  • 1
    @JeffSchaller, --login is not standard; -o vi is only for POSIX-like shells (not csh, tcsh, rc, es, fish, Bourne...), not all even have a vi mode to start with. Here I'm under the impression the OP wants to start bash regardless of the login shell of the remote user. Nov 30, 2017 at 16:07
1

Make it two commands.

  1. Put the commands to be executed on the remote host into a file .bashrc.tmp.
  2. $ scp .bashrc.tmp root@remotehost.com:
  3. $ ssh <options> root@remotehost.com "bash --login --rcfile ${HOME}/.bashrc.tmp"

This way you don't even need to back up your original .bashrc.

1

To simplify jumping from a folder to another on your host, you can install apparix and set up bookmarks in the different places you need to jump, then use to folderX in apparix: https://micans.org/apparix/man/apparix.html

0

I'm using a remote system with a shared user ID, so I don't want to edit the startup files, since it would affect everyone else too. Your posting here showed me how to set the vi option that I wanted, though. Here's what I put in my ~/.ssh/config file

Host testsystem
User tester
RequestTTY yes
RemoteCommand exec /bin/bash --login -o vi

which is remarkably simple. Dunno about the "cd" part that you want.

1
  • Welcome to the site, and thank you for your contribution. Please note however that when you refer to a specific post to a question, always be sure to include a link as the display order depends on user preferences. Also, be sure to not post comments as full-fledged answers - they are treated differently on this site.
    – AdminBee
    Jun 30, 2021 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.