(Originally posted on Stack Overflow. They suggested I try here instead. Here's the original post: http://stackoverflow.com/questions/3858208/sudo-is-there-a-command-to-check-if-i-have-sudo-and-or-how-much-time-is-left)

See title. I want a command that lets me query sudo. Ideally it would return success if I still have sudo and false if sudo has expired. Getting the time left might also be useful (although if I was concerned I could just do sudo -v to revalidate.) Oh and it shouldn't have to ask for a password.

The closest thing I've found is "sudo -n true", but the -n option is only present on my Centos 5 machine at work. -n fails if it has to ask for a password. Is there any other way to get this functionality? Assume I don't actually have root on all the machines I work with, so I can't install new versions of sudo to my liking.

For what it's worth I'm doing this so I can get my prompt to indicate sudo status. I like knowing which terminals are actively sudo-able. I also have a prompt that changes colors when I'm root, but I don't use root very often so that's of limited use.

link|improve this question
feedback

3 Answers

up vote 1 down vote accepted

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 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.

link|improve this answer
+1 for the security ramifications of a visual indicator! – Dennis Williamson Oct 4 '10 at 19:50
That part hadn't occurred to me. What I'm hoping the indicator does is remind me to run sudo -K, instead of forgetting I left sudo active and leaving some potentially dangerous terminals open. Not that I often forget to lock my screen, but I like the extra insurance. – valadil Oct 4 '10 at 20:11
At the moment I'm leaning toward checking sudo -V, and if it's sufficiently new enough to have -n, checking -n to get the notification. Seems like it shouldn't break anything anywhere. – valadil Oct 4 '10 at 20:12
@valadil: It occurs to me that a subtle indicator wouldn't introduce too much security risk. Turn on underlining for the username in the prompt, for example. – Dennis Williamson Oct 4 '10 at 21:05
@Dennis: Exactly. I wasn't going to change my prompt into "OMG_YOU_HAVE_SUDO_NOW!_user@host" or something like that. I'd probably just change the color a little. I don't expect anybody to know what that means unless they sit down and get intimate with my .bashrc. – valadil Oct 4 '10 at 23:42
feedback

According to the sudo manual, the sudo session is determined according to the time stamp file (/usr/lib/sudo/<username>), so you may be able to figure out how much time is left by checking the date/time of the time stamp file. However, in my system, the time stamp file is in fact a directory, and there are three files with cryptic content in them (and also some weird time stamps, but /usr/lib/sudo/<username> seemed to have a timestamp that coincided with the time I gave sudo my password. I think /usr/lib/sudo/<username>/0 has the time stamp of the most recent sudo execution.

link|improve this answer
On my system, the timestamp files are in a directory which can't be read without using sudo, which prompts for a password and thus wouldn't work for the OP's needs. – Dennis Williamson Oct 4 '10 at 21:04
Good point. I didn't check ownership of these files. You are right, it would be useless. – Dysaster Oct 4 '10 at 21:07
feedback

How about the man page

man sudo

List your available commands:

sudo -l

sudo itself has no time or date limits... see:

man sudo
man sudoers
link|improve this answer
sudo -l gives me a password prompt. I need it to tell me I have no active sudo session. – valadil Oct 4 '10 at 19:36
sudo status times out, but you're correct if you mean that it doesn't have a schedule feature. – Dennis Williamson Oct 4 '10 at 19:53
Yes - it times out - but no schedule. – Andreas Rehm Oct 7 '10 at 22:04
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.