how to be warned if terminal has sudo privileges and also show how much time is left?
[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...
fi;
}
function FUNCpromptCommand () {
FUNCsudoOn
}
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
.