To simply put it, sudo (superuser do) is the facility to run commands as another user on a system without knowing that user's password. sudo allows flexibilities like, allowing a limited set of commands ( as opposed to all command allowed for the real user as whom you are sudo-ing ) for sudo users. It also allows sudo privilege at group level. These features in combination makes sudo a powerful utility that can control and manage privileges in a granular way.
For a non root user to run a command that requires root privilege, prepend the command "sudo" to the command that needs to be ran as sudo. It will prompt for the non root user's password, and once authenticated the command will be ran with root privilege. If you want to run the command as another user, you should prefix "sudo -u <username>" to the command. For this to work you should have proper permissions configured in the sudo configuration file /etc/sudoers.
Before going into the configuration of /etc/sudoers, let us see a few examples of using sudo
1. User safeer want to run command "useradd" to add a user, this needs root privilege and "safeer" is obviously not "root"
safeer@buttercook:~$ useradd saniba
useradd: cannot lock /etc/passwd; try again later
Permission is denied since safeer is not having root privilege, now run the same command with sudo
safeer@buttercook:~$ sudo useradd saniba
[sudo] password for safeer:
safeer@buttercook:~$ id -u saniba
1005
As you can see, the user was successfully added. Now let us run a script in user "saniba"'s home directory which has executable permission to be ran as saniba only.
saniba@buttercook:~$ /home/saniba/test.sh
Testing Permission : /home/saniba/test.sh
saniba@buttercook:~$ ls -l /home/saniba/test.sh
-rwxrw-r-- 1 saniba saniba 48 Mar 2 01:44 /home/saniba/test.sh
I am trying to run the command as user "safeer" but fails.
safeer@buttercook:~$ /home/saniba/test.sh
bash: /home/saniba/test.sh: Permission denied
Now using sudo as saniba and running the command.
safeer@buttercook:~$ sudo -u saniba /home/saniba/test.sh
[sudo] password for safeer:
Testing Permission : /home/saniba/test.sh
Let us examine how sudo can be configured. All the configurations are inside /etc/sudoers file, this file is often referred to as sudo policy file. To start editing the sudoers file, you can either run you favorit editor, say vim like this: sudo vi /etc/sudoers OR use the command "sudo visudo" which will open the sudoers file in the default editor.
The sudoers file contains following types of entries : Aliases ( Variables ), Defaults ( Override for default options ) and User Specifications ( who runs what )
For a quick configuration, User specifications are more important. The general format of user specification is like this
"who where = (as_whom) what"
who - The user / group for which sudo permission is being defined. If "who" is group an "%" should be prefix to it.
where - The host on which the sudo is being run. This setting was given in the assumption that the same sudo file will be shared over multiple hosts. This is not the case in most scenarios. So a keyword "ALL" will suffice for most of the cases.
as_whom - Under which user's/group's permission the sudo should be run.
what - what commands the "who" is allowed to run.
"as_whom" has the following syntax "(user1,user2,user3...:(group1,group2..)). The whole list is enclosed in a parentheses and starts with a comma separated list of one or more users followed by a column and a comma separated list of groups enclosed in parenthesis. A minimum of one user in the form (user1) or group in the form ":(group1)" is required, also the wild card "ALL" can be used.
what - Command has the following syntax : "[tag1,tag2..]: command1,command2..". Commands can have zero or more tags preceding it, which influences how the command is run. For eg: the tag NOPASSWD causes sudo to not prompt for password when the subsequent command is run. The command itself can be a comma separated list of one or more commands. The commands can take the shell wildcards like */?/[..] etc for the command name and for its arguments.
We will see a few examples:
root ALL = (ALL) ALL
Allows root to execute any command on any host as any user ( on any host ).
%admin ALL = (ALL) ALL
Allow members of group wheel to run any command as any user ( on any host ).
safeer ALL = ALL
User safeer is allowed to run all commands as himself but not as any other user ( The absense of "as_whom" means, user can run "sudo COMMAND" but not "sudo -u USER COMMAND" )
%vmusers ALL = (:vboxusers) /usr/bin/VirtualBox
All members of vmusers group can run the VirtualBox application as vboxusers group.
sunil ALL = NOPASSWD: /bin/mount,/bin/imount
Allow user sunil to mount and unmount devices without verifying his password ( the password prompt that shows up when running sudo command will be gone )
Let us look at aliases now, there are four categories of aliases.
General Syntax is :
ALIAS_TYPE ALIAS_NAME = COMMA_SEPERATED_LIST_OF_ONE_OR_MORE_ALIAS_MEMBERS
Alias type is one of
User_Alias, Runas_Alias, Host_Alias and Cmnd_Alias. Alias name should be a combination of uppercase letters, numbers and underscore starting with an uppercase letter.
User_Alias - For grouping set of users/groups. Each item in the list should be either a username, userid ( preppended by #),groupname (preppended by %),groupid(preppended by %#) etc..
Eg:
User_Alias SUPERUSERS = root,safeer,#1027,%admins
Host_Alias : For grouping hosts - list items can be hostnames,ips,network id/mask,netgroups
Eg:
Host_Alias MANAGEMENT_HOSTS = admin1,192.168.7.4,10.15.14.0/255.255.255.0
Runas_Alias : For grouping the "as_whom" groups/users. The list item syntax is same as User_Alias
Eg:
Runas_Alias RUNAS01 = dba,%operators
Cmnd_Alias : Grouping commands : The list item can be a single command, command with arguments, commands with wild card or entire directories.
Eg:
Cmnd_Alias MOUNT = /bin/mount,/bin/umount
For a single alias type, you can have multiple definitions in one line separated by a column.
Eg:
User_Alias DBA = safeer,saniba : OPERATOR = hareesh,bijo
Aliases can be used in User Specification in the place of their respective type, ie: User_alias => who, Host_Alias => Where , Runas_Alias => as_whom, Cmnd_Alias => what.
Eg:
ADMINS DBSERVER = ( DBA ) NOPASSWD: DBMCMDS
Defaults: Defaults override sudo's defaul setting in runtime. The overrides can be applied systemwide, or for specific set of hosts,users,commands and runas (command being run as specific user). Syntax of Defaults is:
Default_Type Comma_Seprated_Parameter_List
Default_Type can be one of the following depending on how the overrrides are applied
Defaults - Systemwide
Default@HOSTLIST - Apply to given host list
Defaults:USERLIST - Apply to given list of users
Defaults!COMMANDLIST - Apply to given list of commands
Defaults>RUNASLIST - Apply to given list of runas users/groups
Parameter - There is a long list of parameters of different types (boolean,integer,string,list) which are explained in sudoers man page. Booleans can be turned on by just giving the boolean name or turned off by preppending a "!" to the boolean name. For other types, the parameter takes the form of a key value pair in of of the following three ways: name = value ( set value - for strings ), name += value ( append value - for list ), name -=value ( remove value - from list ). We will see couple of examples.
Defaults insults
-- Apply system wide, by default if u type wrong password when promted by sudo the error message is "Sorry, try again". When u turn on the insults bolean, every time the user enters wrong passord, sudo will give a mildly insulting error message.
Defaults:DBA always_set_home
IF users defined in DBA User_Alias are sudoing as other users, set the HOME env variable to the target users home directory
Defaults!MOUNTCMDS logfile=/var/log/sudo-mount.log
If commands defined in MOUNTCMDS Cmnd_Alias are run, log them to /var/log/sudo-mount.log
That will be enough for a good sudo policy configuration. Couple of more points to note before we close this tutorial.
For the first time a user runs sudo on a system, just before prompting the password, a message will be shown to the user. This message is called lecture and can be turned on/off using the "lecture" Defaults settings.
When a user runs a sudo command, the user is asked for the password, and if given correct password the command is run. After this for any sudo command that is ran within a certain time period, password is not promted. This setting is controlled by the Default setting "timestamp_timeout" and its default value is 15 minutes
You can include extra configuration files by adding one or more lines in the form "include </file/full/path>|</directory/full/path/". These configuration files should follow the same syntax as sudoers.
"man sudoers" is a beautiful documentation, consult it for more details.
Also checkout the official
sudo website