How to see in the terminal that you have sudo privileges? (so knowing I have these privileges, I can just type sudo -k) It would be better to not add too much information into the terminal also, just some kind of simple and small alert!


Also, how to see how much time I still have with those privileges on that specific terminal? (so I can just let it end instead of typing sudo -k). I think that remaining time could show up each time I press Enter key.

EDIT: I researched more and found that /var/lib/sudo/$USER/$nPts (where $nPts is current terminal pts as of ps -p $$) has its timestamp updated everytime a sudo command is issued, and it is only accessible if you use a sudo command, so the remaining time will always be the configured one...

PS.: Alternatively to that, I tried alias sudo='sudo -k' but I dislike having to type the password for every command... And also, this alias sudos='sudo -k' (or alias ssudo='sudo -k', good if you typed a long line, just press Home and type 's') works if you only want to issue one command by typing sudos. But I still miss to know when I have normal sudo privileges to know I need to act...

  • 2
    Why was this down-voted? Although this isn't the clearest post, the title question still seems like a valid question to me (and a pretty useful question, at that) – DaimyoKirby May 10 '13 at 2:10
  • could you say what could be clarified? my unedited initial post was 3 times bigger and I cleared it a lot to make it more concise :) – Aquarius Power May 10 '13 at 2:46
  • I think it was just late - I've re-read it, and it makes more sense. – DaimyoKirby May 10 '13 at 12:09

[I still would like to know if there is a better answear.]

I found a way that works, and the prompt will be kept on a single line:

at the end of your ~/.bashrc add this:

function FUNCsudoOn() { 
    if sudo -n uptime 2>/dev/null 1>/dev/null; then 
        echo -ne "\E[0m\E[93m\E[41m\E[1m\E[5m SUDO \E[0m"; 
        #echo #without newline, the terminal seems to bugout with lines that are too big... discomment this if you find any problems...
function FUNCpromptCommand () { 
export PROMPT_COMMAND=FUNCpromptCommand
#export PS1="\`FUNCsudoOn\`$PS1" #this also works, use instead of PROMPT_COMMAND

EDIT: I found that sudo -n uptime will update the sudo timeout, so everytime you hit the Enter key, that sudo time will be updated... That makes knowing the remaining time useless, as it will always be the configured one, defaulting to 15min...

and to find the best colors formatting for you taste you can use ScriptEchoColor with --escapedchars option like:

echoc --escapedchars "@{nRlyo} SUDO " #that outputs below...
echo -e "\E[0m\E[93m\E[41m\E[1m\E[5m SUDO \E[0m"

to just stop the blinking remove \E[5m like in \E[0m\E[93m\E[41m\E[1m SUDO \E[0m


Your terminal doesn't "have sudo privileges", unless you do something silly like sudo bash DON'T DO THIS!

All you have to do to "have normal sudo privileges" is to refrain from typing sudo in front of the command.

/usr/bin/id is a good way to see what sudo does. For example:

id -a        # returns my UID info
sudo id -a   # returns root's info
id -a        # me again
sudo id -a   # root again

The sudo man page says "The sudoers policy caches credentials for 15 minutes, unless overridden in sudoers(5)". What this means is that, by default, if you issue a sudo command (and enter your password),then wait 14.9 minutes before issuing a second sudo command, you won't have to enter your password again. If you wait 15.1 minutes, you will have to re-enter the password. sudo -k simply expires the 15 minute timer immediately. The only use I can see for sudo -k is if you're about to yield your unlocked terminal to someone you don't trust with UID 0.

  • You said "refrain from typing sudo in front of the command". But if I want to check my firewall rules with iptables -L it wont work unless I type sudo iptables -L. My point is, how can we actually avoid using sudo? – Aquarius Power May 10 '13 at 5:55
  • Also, about the remaining time, I want to know how much time is left each time I press Enter key on terminal. About sudo -k, I am worried about crackers trying to remotely access my machine (I dont really know if they can do it anyway...), or in case I leave my machine open without locking it up for a few moments... well, the real point is that: I want to do it this way! I dont care having to type sudo -k and my password again later as many times I need to, I feel good when I do it this way! :) – Aquarius Power May 10 '13 at 6:02
  • "Avoid using sudo"? Sudo was the solution to distributing the root password, making binaries setuid, or using /bin/su. Are you sure you want to discard it? "Remotely access my machine..." and, I suppose, take over /proc/YourPID/fd/{0,1,2}. No. If your machine is remotely accessed, the Bad Guy has easier ways to UID 0. You should increase your Linux-foo prior to this level of concern. – waltinator May 12 '13 at 1:39
  • Sorry for the rant, it sounds like you should alias k=/usr/bin/sudo -k, replace the sudo command with an alias or bash function that wraps the call to /usr/bin/sudo with /usr/bin/touch $HOME/tmp/$$/sudotime, and finally, add something to print ${{ time(now) - time($HOME/tmp/$$/sudotime) )) in your Bash prompt. Not trivial, but if it will make you feel good, go for it. – waltinator May 12 '13 at 1:53
  • that is interesting, I would also log when the attempt happened, or even better, shutdown any network communication, cool! but even if I hide sudo with an alias to any random word, they could still type /usr/bin/sudo instead of sudo and it would still work as sudo does :(. Anyway I like the idea.. I wonder if I change the real /usr/bin/sudo it will break something other than all my scripts that use it.. even if I do so, they could, by reading the scripts, guess what sudo became.. – Aquarius Power May 12 '13 at 2:44

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.