Sudo - is there a command to check if I have sudo and/or how much time is left?

The -n option is available in newer versions of sudo, but as you stated that's not an option. There's no real way to do what you're looking for short of just trying sudo and seeing if it comes back with a prompt for a password. If your concern is you want a visual indication, why not start do sudo /bin/bash to start a root bash session? Note that this is insecure, but it's also somewhat insecure if someone realizes your prompt changes on sudo.


I know this is a really old question but here is I did in a script today. Assuming that being sudo means being able to run any command as sudo (or, at least, uptime)

CAN_I_RUN_SUDO=$(sudo -n uptime 2>&1|grep "load"|wc -l)
if [ ${CAN_I_RUN_SUDO} -gt 0 ]
then
    echo "I can run the sudo command"
else
    echo "I can't run the Sudo command"
fi

However, if your user has restrictions in the visudo, this check will not be enough, e.g., if your user can only use the make command. Try running some command that you know will work. You may add the filter grep -v Sorry, e.g.: $(sudo -n uptime 2>&1|grep "load" | grep -v Sorry|wc -l) for avoiding those false positives.


To simplify the answer given by @wags007

if sudo -n true
then
  sudo id
else
  echo "sorry, but did not want to bother you"
fi

However, if in your https://www.sudo.ws/man/1.8.15/sudoers.man.html configuration you have defaults mail_badpass there will be a mail sent for every test that results in false (would have prompted). To avoid such nuisance change that part of your sudoers file to

Defaults       mail_badpass
Defaults!      /bin/true !mail_badpass

As a result security alert mails are send for all commands except /bin/true. Well yes, somebody could now try to brute force a password by calling sudo true an unlimited number of times without any security alert mail being sent.

Note: Always use visudo instead of your favorite editor to edit the sudoers file. Failing to do so you risk being locked out.


The command below will show a colored indication that you have sudo granted, so you remember to do a sudo -k before going away from the machine. It is useful also on non colored terminals.

As we can have sudo active and inactive on different terminal sessions, I created this that you can put at the end of your ~/.bashrc

function FUNCpromptCommand () { 
    sudo -n uptime 2>/dev/null 1>/dev/null
  local bSudoOn=`if(($?==0));then echo true; else echo false; fi`

    history -a; # append to history at each command issued!!!
    local width=`tput cols`;
    local half=$((width/2))
    local dt="[EndAt:`date +"%Y/%m/%d-%H:%M:%S.%N"`]";
  if $bSudoOn; then dt="!!!SUDO!!!$dt"; fi
    local sizeDtHalf=$((${#dt}/2))
    #printf "%-${width}s" $dt |sed 's" "="g'; 
    echo
    output=`printf "%*s%*s" $((half+sizeDtHalf)) "$dt" $((half-sizeDtHalf)) "" |sed 's" "="g';`

    local colorLightRed="\e[1;31m"
  local colorNoColor="\e[0m"
    if $bSudoOn; then
        echo -e "${colorLightRed}${output}${colorNoColor}"
    else
        echo -e "${output}"
    fi
}
export PROMPT_COMMAND=FUNCpromptCommand

At terminal type bash to test it. It will also add a whole line each time you execute a command, that has the information of the time the last command ended, so you can go lunch and know when the last command ended :).

You can play with this code to fit your needs. There is the PS1 variable also (that is the actual small prompt single line), but I think it is better to not mess with it.

PS.: for OS-X, look for the comment below by @nwinkler.