Continuing the discussion from Can't remove Command Line Tools:
Since I mentioned it …
The behavior of the sudo command is configured via a text file, /etc/sudoers. Note that only the root account and users in the “wheel” group can read this file. But you can view it (from an account with sudo access, like an administrator), by typing “sudo cat /etc/sudoers” from a terminal window.
There’s a lot of stuff in there, but the most important lines, for the purpose of this thread, are:
root ALL = (ALL) ALL
%admin ALL = (ALL) ALL
Each line of this form authorizes a particular user or group of users to run a program or group of programs as root (or as another specified user).
The general form of these lines is:
<user> <host> = (<run-as>) <commands>
%<group> <host> = (<run-as>) <commands>
Where:
<user>is a user name. If it begins with#, then a user ID<group>is a group name. If it begins with#, then a group ID<host>is a host name. “ALL” means “all hosts”- This field exists because this config file could be on a network server, shared by many computers. So you can configure rules to apply to only some computers on the network.
<run-as>indicates which users and groups the user/group is allowed to run the commands as. “ALL” means “all users and groups”. (sudocan be used to run a command as any user or group in the system, not just root. The config file can be used to restrict this.)<commands>is a comma-separated list of CLI command the user/group is allowed to run viasudo. “ALL” means “all commands”.- You can also prefix commands with various keywords to further configure what can be done.
So, to explain the default entries:
root ALL = (ALL) ALL
This allows the root user (first term), running on any host (second term), to run any command (fourth term) as any user (third, parenthesized term).
%admin ALL = (ALL) ALL
This is similar, but allows any user in the admin group to run any command as any user.
So, if you want to add another specific user (e.g., a non-admin user) to the set of those allowed to run anything as anybody via sudo, you can add a line of the form:
username ALL = (ALL) ALL
And if you want to, for example, let a particular user run only diskutil, but no other commands as root, you could add a line like:
username ALL = (root) /usr/sbin/diskutil
And if you want to let him run this command without providing a password:
username ALL = (root) NOPASSWD: /usr/sbin/diskutil
And just for completeness, if you want to allow this user to run anything as anybody, not require a password for diskutil, but to require one for anything else:
username ALL = (ALL) PASSWD: ALL, NOPASSWD: /usr/sbin/diskutil
(“ALL” comes first because when a command matches multiple entries, the last match is the one that takes effect.)
The syntax supports far more than the above summary, but a full discussion could be an hours-long presentation. For all the ugly details type man sudoers or look here: sudoers(5) - Linux manual page
One final thing:
Although /etc/sudoers is a text file, you should not edit it with an ordinary text editor. This is because errors can cause sudo to stop working altogether. If you break it, you’ll need to gain root-level access through some other mechanism (most of which are disabled by default in macOS) in order to edit the file so you can recover from the mistake.
Instead, use a command visduo. This command invokes the vi text editor (or some other editor, configurable via environment variables), but it syntax-checks the file before letting you save it. So you can’t accidentally break anything, as long as you don’t ignore its warnings. See also: visudo(8) - Linux manual page