Why does sudo -n on Mac OS X always return 0?

0

It looks like sudo -n on Mac OS X (10.10.3) always returns 0 as a return code.

So something like this will always show 0, regardless of whether the user has authenticated with his password first:

sudo -n true &> /dev/null ; echo $?

In the case that the user does not have a valid sudo session, I would expect to see 1 as an exit code, but on OS X, it always returns 0.

I'm asking since I've seen this pattern suggested to check for sudo access, e.g. here: https://superuser.com/a/587876/119764

What's the official (???) behavior of sudo -n? Does it differ by version or OS, or am I missing something?

nwinkler

Posted 2015-04-17T15:25:44.587

Reputation: 245

2My first reaction is "Of course behaviour may differ by OS." The GNU folks and the BSD folks are under no obligation to agree with each other. The "official" answer will have to be: check the man pages. – glenn jackman – 2015-04-17T15:59:34.997

Answers

1

I have the same issue. Referring to the man page,

Otherwise, sudo quits with an exit value of 1 if there is a configuration/permission problem or if sudo cannot execute the given command.  In the latter case the error string is printed to stderr.  If sudo cannot stat(2) one or more entries in the user's PATH an error is printed on stderr.  (If the directory does not exist or if it is not really a directory, the entry is ignored and no error is printed.)  This should not happen under normal circumstances.  The most common reason for stat(2) to return "permission denied" is if you are running an automounter and one of the directories in your PATH is on a machine that is currently unreachable.

So it seems to be a bug in OS X.

Basuke

Posted 2015-04-17T15:25:44.587

Reputation: 26

@G-Man @Basuke Its a bug in sudo not OSX ;) see my answer below – Marco M. von Hagen – 2015-06-11T21:46:34.867

1I am using port because of things like that. So I could install sudo -V 1.8.13 on OSX 10.6.8 and use sudo -n true without problems. – Marco M. von Hagen – 2015-06-13T09:53:00.973

Apple finally fixed this bug on 10.10.5 update with bundling sudo of version "1.7.10p9". – Basuke – 2015-08-14T19:46:45.010

2

After reading sudo manpage again and again I checked Bugzilla here. This behaviour of sudo is a bug which appeared in 1.7.10 after merging from a 1.8 branch.

If you want to use sudo -n command &>/dev/null ; echo $? to check your 'privileges' make sure, you have sudo version 1.7.9b1 and earlier.

Marco M. von Hagen

Posted 2015-04-17T15:25:44.587

Reputation: 495