Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.5: Fix the sudo shell prompt UNIX
This hint assumes you are using the default bash shell. If you need to run a series of commands interactively as root (or any other user) using sudo, it's most convenient to start a new shell session with the effective user ID you want by typing sudo -s.

Prior to Leopard, you would then get a shell prompt similar to the usual one, but with the effective username in place of your username. For example, if you were logged into host foo.local as user bar, your regular shell prompt would be
[foo:~] bar$ 
...where the ~ represents your home directory (and it will change to show the current directory). If you started a root shell with sudo -s, the prompt would become...
[foo:~] root# 
...so you could see at a glance where you were and who you were. Under Leopard, though, the behavior of sudo has changed -- you now get a default prompt with a # to indicate you're running as root -- bash-3.2#. This is much less useful, because now you can't see what host you're logged into or what your working directory is. You have to type extra commands to get that information.

To get back the old-style prompt, you need to set the environment variable SUDO_PS1 in one of your shell initialization files. One way to do this is to start a shell (not a sudo shell) and enter...
cat >> .profile
export SUDO_PS1="\[\h:\w\] \u\\$ "
...then type Control-C to exit the cat process. Close the Terminal window or tab, open a new one, and start a sudo shell. You should now have the old prompt back. To revert the change, edit the file named .profile in your home directory to remove the line you added above.
    •    
  • Currently 2.63 / 5
  You rated: 4 / 5 (8 votes cast)
 
[18,348 views]  

10.5: Fix the sudo shell prompt | 20 comments | Create New Account
Click here to return to the '10.5: Fix the sudo shell prompt' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.5: Fix the sudo shell prompt
Authored by: club60.org on Mar 19, '08 08:11:48AM

Or you could issue "sudo su -" command.



[ Reply to This | # ]
10.5: Fix the sudo shell prompt
Authored by: allanmarcus on Mar 19, '08 09:14:44AM

sudo su - would only work if you have root enabled, which is not recommended. If you have root enabled you don't need the sudo command, just su - works.



[ Reply to This | # ]
10.5: Fix the sudo shell prompt
Authored by: Anonymous on Mar 19, '08 02:44:44PM

Eh? sudo su - works even if you don't enable the root account...

[dbr@schism:~]$ sudo su -
Password:[my users password]
[root@schism:~]$



[ Reply to This | # ]
10.5: Fix the sudo shell prompt
Authored by: leamanc on Mar 20, '08 05:46:24AM
You all beat me to it. sudo su - is the established way to do this. It works without root being enabled. All sudo -s does is open another shell, whereas sudo su - executes a full login, including the setting of any envirionment variables.

[ Reply to This | # ]
10.5: Fix the sudo shell prompt
Authored by: epyon9283 on Mar 19, '08 09:06:36AM

"sudo -i" will do the same thing



[ Reply to This | # ]
10.5: Fix the sudo shell prompt
Authored by: allanmarcus on Mar 19, '08 09:18:01AM

excellent tip! I always used -s, but -i seems to be "better"!



[ Reply to This | # ]
Not the same
Authored by: lincd0 on Mar 19, '08 10:01:11AM

sudo -i is not the same as sudo -s. The first simulates a root login. You get the root home directory and PATH, and lose your own environment. The second preserves most of your customized shell environment, while changing your effective UID to root. Much better for my purposes.



[ Reply to This | # ]
10.5: Fix the sudo shell prompt
Authored by: Scaramanga on Mar 19, '08 11:50:57AM

Thank you! This has been bugging me for so long!



[ Reply to This | # ]
10.5: Fix the sudo shell prompt
Authored by: mjb on Mar 19, '08 01:41:46PM
Unix convention in shell prompts is that the trailing character '$' is used for non-root users, and '#' for root (essentially).

[ Reply to This | # ]
10.5: Fix the sudo shell prompt
Authored by: corienti on Mar 19, '08 05:58:29PM

Yes, it certainly has. Very long standing custom.

However in these modern times, I always use a colour prompt, and have it so the username comes up in red if root, and another colour if not root.
I find the red is far more visible and noticeable than the #.



[ Reply to This | # ]
10.5: Fix the sudo shell prompt
Authored by: corienti on Mar 19, '08 06:02:30PM

For what it's worth, this is what I use. Customise colours to preference.
(note colours range from 31 to 36, and 01 and 00 indicate bold/nonbold.


if [ "${USER}" = "root" ]; then
USER_COL="01;31"; PATH_COL="01;33"
else
USER_COL="01;33"; PATH_COL="01;32"
fi
HOST_COL="01;37"
export PS1="\n\[\e[${USER_COL}m\]\u @ \[\e[${HOST_COL}m\]\h > \[\e[${PATH_COL}m\]\w/\[\e[0;38m\]\n-> "
unset HOST_COL PATH_COL USER_COL



[ Reply to This | # ]
10.5: Fix the sudo shell prompt
Authored by: mjb on Mar 19, '08 07:37:02PM

My apologies, while you are correct, I was referring to the $ in the example SUDO_PS1 given. :)



[ Reply to This | # ]
The prompt shown does that
Authored by: lincd0 on Mar 20, '08 08:16:40AM

Try it.



[ Reply to This | # ]
The prompt shown does that
Authored by: mjb on Mar 20, '08 02:53:09PM

Don't have to, just realised I forgot about \$. I'll shut up now. :)



[ Reply to This | # ]
10.5: Fix the sudo shell prompt
Authored by: saint42 on Mar 19, '08 04:22:50PM

Personally, I use this setting in .profile:

PS1="\u@\h:\w> "

This shows me who am I, what machine I am on, and what path:

ex:
saint@iMac:~>
saint@iMac:/System/Library>
root@iMac:/goinfre>



[ Reply to This | # ]
Not needed if you are already defining PS1 (+ example)
Authored by: ClassicUser on Mar 19, '08 06:10:19PM
To clarify: If you are already defining a custom PS1 entry in such an rc file, it will be picked up by "sudo -s", as well - so this specific "SUDO_PS1" entry would not be needed.

FYI, I use a custom entry within my .profile to display the path within brackets - truncating to just the final directory, if the path grows too long (see "MAXPWDLEN"):

# set prompt (add brackets to standard prompt)
## default: PS1="\h:\w \u\$ "
## previous: export PS1="[\h:\w] \u\\$ "
MAXPWDLEN=21
export PS1='[\h:\
$( MYPWD=${PWD/#$HOME/~} && [ ${#MYPWD} -gt $MAXPWDLEN ] && \
printf "%s" "…${MYPWD/${MYPWD%*/*}/}" || \
printf "%s" "${MYPWD}")] \
\u\$ '

This results in, for example:

[mymac:/Users/classic] classic$

…as well as responding to input as:
[mymac:/Users/classic] classic$ sudo -s
[mymac:/Users/classic] root# cd ~/Library/Application\ Support/
[mymac:…/Application Support] root# cd /System/Library/Frameworks/Message.framework/Versions/B/Resources/English.lproj/
[mymac:…/English.lproj] root# exit
[mymac:/Users/classic] classic$

(remember, ellipses are entered via "Option-semicolon"; if these don't work for you, just substitute 3 periods, or the like)

[ Reply to This | # ]

Not needed if you are already defining PS1 (+ example)
Authored by: joshewah on Mar 21, '08 09:06:01AM

I use the following prompt in .profile and using sudo -s does not keep it:
export PS1="\[\033[01;37m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\] "

I bet you could run visudo and change something in the sudo config file to fix this globally.



[ Reply to This | # ]
Not needed if you are already defining PS1 (+ example)
Authored by: ClassicUser on Apr 19, '08 11:44:03AM
Ah, but .profile is only run for direct login shells - of which performing a sudo -s doesn't qualify, as it is merely starts a sub-shell within that same session.

If you put that same command in your .bashrc file instead, you should be better off. Also, note that several folks simply source the one file from the other - I have a grand total of one line in my .profile:

source $HOME/.bashrc

…to handle just such a provision, and ensure the same environment is established upon first login, as well as when starting sub-shells.

[ Reply to This | # ]

Not needed if you are already defining PS1 (+ example)
Authored by: maxlyth on May 01, '08 07:48:00PM
Yoy are missing a backslash in the first printf line. It should read:
printf "%s" "…${MYPWD/${MYPWD%*\/*}/}" || \

[ Reply to This | # ]
10.5: Fix the sudo shell prompt
Authored by: Zeitkind on Mar 23, '08 03:03:29PM
My prefered one:

cat ~/.bashrc

alias dir='ls'
alias ll='ls -al'

export CLICOLOR=1
export LSCOLORS=ExCxhxdxbxegedabaghbag

declare -x PS1='\[\033[1;33m\]\u\[\033[1;37m\]@\[\033[1;32m\]\h\[\033[1;37m\]:\[\033[1;31m\]\w \[\033[1;36m\]\$ \[\033[0m\]'
But everyone has its own special .bashrc.. ;)

[ Reply to This | # ]