Home > Blockchain >  How to invoke an interactive bash shell as another user with initial commands?
How to invoke an interactive bash shell as another user with initial commands?

Time:01-28

I currently have a RemoteCommand, RemoteCommand sudo su - admin-user set up in my ssh config that allows me to connect to a system and immediately switch users to an admin user. This is because I must only make changes to the system as that admin-user, the admin-user does not have a password, and I am not allowed to add my public key to the system to connect without specifying a password. This works perfectly; I'm able to login as "login-user", and it immediately switches me to the admin-user.

However, I want to do the same, and create a temporary function that would allow me to run a common command in a more shorthand manner. This is because I am not allowed to change the admin-user's bashrc.

My thought in setting this up was to do the following in my ssh config:

HOST SYSTEM
    Hostname 12.34.56.78
    User login-user
    RemoteCommand sudo -Hu admin-user /usr/bin/bash --init-file <(echo ". ~/bashrc; function testy() { ls ; }") -li
    ...

In this case, I'm just testing with a function that runs ls. Just using a named pipe to source the normal bashrc and add a func to the new shell via an init file/rc file.

This creates an interactive bash shell as the admin-user as expected, but upon trying to run the function testy in this shell, I get bash: testy: command not found. Doing the same without switching users in the same step works, but not if I add the flags to run the shell as admin-user. I can't figure out how to get this working. Any help using this approach or another is greatly appreciated!

CodePudding user response:

Likely a named pipe sharing issue. You can use another wrapper shell:

HOST SYSTEM
    Hostname 12.34.56.78
    User login-user
    RemoteCommand sudo -Hiu admin-user /usr/bin/bash -c 'exec /usr/bin/bash --init-file <(echo ". ~/bashrc; function testy() { ls ; }")'
    ...

. ~/.bashrc also likely can be unnecessary but that's besides the main point.

  • Related