1

Is it possible to have multiple users using a root shell but with different prompts on the BASH shell for each session/user? We have some training environments with our users using su/sudo to get to a root shell for demonstration purposes.

Note: I'm aware of best practice regarding su/sudo etc. These are training environments and I specifically want then to run an interactive shell as root for learning purposes.

shocko
  • 49
  • For training purposes, you may want them to have individual user accounts (with their username in the prompt) from which they use sudo to prefix administrational commands without ever getting an interactive root prompt, in their own personal VM set up for the training session. For training purposes, that would definitely be best. For hacked up systems were things can be expected to work very differently from anywhere else, the root prompt can probably be made to contain the value of the SUDO_USER variable. I leave it up to other users here to write that up as an answer. – Kusalananda Oct 09 '22 at 12:50
  • 1
    All the training users do have a standard shell with their username in the prompt. I then get them to do something like sudo -s, sudo -i or su - to get a shell as root. I do specifically want them to run the interactive shell as root. I want this to demonstrate various aspects as to why we don't generally do this in production. – shocko Oct 09 '22 at 16:03

1 Answers1

0

You can start by playing with who. It can give you a real user name.

For example, adding into /root/.bashrc the code like

REALUSER=`who -m|cut -f1 -d' '`
export PS1="$REALUSER->\u \$ "
White Owl
  • 5,129
  • You don't have to export the PS1 variable, or any other variables that child processes never have to see. Also note the missing backtick, and that who -m will not work on systems such as Alpine Linux (using busybox who). – Kusalananda Oct 09 '22 at 12:54
  • Very good point! I also read the man page (which I should have done more thoroughly see sudo -E is an option that preserves the callers environment – shocko Oct 10 '22 at 08:46