27
12
(Originally posted on Stack Overflow. They suggested I try here instead. Here's the original post: https://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.
1+1 for the security ramifications of a visual indicator! – Paused until further notice. – 2010-10-04T19:50:43.667
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 – 2010-10-04T20:11:38.500
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 – 2010-10-04T20:12:13.833
@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. – Paused until further notice. – 2010-10-04T21:05:32.030
@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 – 2010-10-04T23:42:23.443