If I as a non-root user (but in the sudoers file) do sudo -i
, I get root privileges:
laptop:~$ sudo -i
[sudo] password for bjmgeek:
laptop:~$ whoami
root
laptop:~$ id
uid=0(root) gid=0(root) groups=0(root)
laptop:~$
But, notice the prompt still shows a $
(which typically means a normal user), not a #
(which typically means root).
If, however, I do sudo su -
, I also become root, but get the # prompt:
laptop:~$ sudo su -
-bash-5.2# whoami
root
-bash-5.2# id
uid=0(root) gid=0(root) groups=0(root)
According to the man page, su -
starts the shell as a login shell. And, according to the man page, sudo -i
starts the shell as a login shell. So, what's the difference that one gets the prompt, with a #
, and one gets the prompt with a $
?
This is on a Debian system, but I don't think there's anything debian-specific about the su
or sudo
commands.
I wonder if it is specifically related to \$
which should show a # for root, and a $ for non-root users.
Update: $PS1
for sudo -i
is set to \h:\[\e[1;34m\]\w\[\e[0m\]$
instead of ending in a \$
, so that's the cause of the problem. Now I just need to comb through all my files to see what's not setting it correctly.
sudo -s
? What happens if you first clear your prompt withPS1='$ '
, and then runsudo -i
? – terdon Dec 14 '23 at 15:05sudo -i
sets$SUDO_USER
whereassudo su -
does not. Do you have anything in root's~/.profile
or~/.bash_profile
(etc.) that sets the prompt depending on$SUDO_USER
? – Chris Davies Dec 14 '23 at 15:41