How To Modify the Sudoers File
You will be presented with the/etc/sudoers
file in your selected text editor.
/etc/sudoers
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
root ALL=(ALL:ALL) ALL
%admin ALL=(ALL) ALL
%sudo ALL=(ALL:ALL) ALL
#includedir /etc/sudoers.d
Let's take a look at what these lines do.
Code:
%admin ALL=(ALL)NOPASSWD:/usr/bin/apt-get
- %admin - All users of the admin group
- ALL= - from any Host/IP
- (ALL) - can run as any user
- NOPASSWD - with no password required
- :/usr/bin/apt-get - the list of comma, separated, applications.
User Privilege Lines
The fourth line, , which dictates theroot
user's sudo
privileges, is different from the preceding lines. Let's take a look at what the different fields mean:root ALL=(ALL:ALL) ALL
The first field indicates the username that the rule will apply to (root
).demo ALL=(ALL:ALL) ALL
The first "ALL" indicates that this rule applies to all hosts.demo ALL=(ALL:ALL) ALL
This "ALL" indicates that theroot
user can run commands as all users.demo ALL=(ALL:ALL) ALL
This "ALL" indicates that theroot
user can run commands as all groups.demo ALL=(ALL:ALL) ALL
The last "ALL" indicates these rules apply to all commands.
root
user can run any command using sudo
, as long as they provide their password.Group Privilege Lines
- Names beginning with a "%" indicate group names.
How To Set Up Custom Rules
Now that we have gotten familiar with the general syntax of the file, let's create some new rules.How To Create Aliases
Thesudoers
file can be organized more easily by grouping things with various kinds of "aliases".For instance, we can create three different groups of users, with overlapping membership:
/etc/sudoers
. . .
User_Alias GROUPONE = abby, brent, carl
User_Alias GROUPTWO = brent, doris, eric,
User_Alias GROUPTHREE = doris, felicia, grant
. . .
Group names must start with a capital letter. We can then allow members of GROUPTWO
to update the apt
database by creating a rule like this:
/etc/sudoers
. . .
GROUPTWO ALL = /usr/bin/apt-get update
. . .
If we do not specify a user/group to run as, as above, sudo
defaults to the root
user.We can allow members of
GROUPTHREE
to shutdown and reboot the machine by creating a "command alias" and using that in a rule for GROUPTHREE
:
/etc/sudoers
. . .
Cmnd_Alias POWER = /sbin/shutdown, /sbin/halt, /sbin/reboot, /sbin/restart
GROUPTHREE ALL = POWER
. . .
We create a command alias called POWER
that contains commands to power off and reboot the machine. We then allow the members of GROUPTHREE
to execute these commands.We can also create "Run as" aliases, which can replace the portion of the rule that specifies the user to execute the command as:
/etc/sudoers
. . .
Runas_Alias WEB = www-data, apache
GROUPONE ALL = (WEB) ALL
. . .
This will allow anyone who is a member of GROUPONE
to execute commands as the www-data
user or the apache
user.Just keep in mind that later rules will override earlier rules when there is a conflict between the two.
GROUPONE ALL = NOPASSWD: /usr/bin/updatedb
. . .
NOPASSWD
is a "tag" that means no password will be requested. It has a companion command called> To find out just exactly what sudo permissions you have on your computer, you would run the following
# sudo -l
Code:
%admin jaunty=(ALL)NOPASSWD:/usr/bin/apt-get
The second way is by using IP addresses.
Note: This requires for you to be connected to a network! Else you will be denied access
Code:
%admin 192.168.1.0/255.255.255.0=(ALL)NOPASSWD:/usr/bin/apt-get
255.255.255.0 is the subnet of your local network.
So, in this instance, you are restricting the use of sudo only to users with an IP address of 192.168.1.1 through to 192.168.1.254
Restricting User Switching
To run commands as another user (other than root), you would run the following:
sudo -u username command
Code:
%admin jaunty=(root)NOPASSWD:/usr/bin/apt-get
Note: if a previous permission is set so the user can run the command as any user.
More specifically this line that is the default in Ubuntu:
Code:
%admin ALL=(ALL) ALL
A more secure method:
Code:
%admin ALL=(root) ALL
Restricting Application Usage
As well, as limiting the applications a user can run using sudo, you can limit the arguments of those applications that the user can use also, for apps that do more than one job.
To limit apt-get usage to just 'update' and 'upgrade', we can have something like this:
Code:
%admin jaunty=(root)NOPASSWD:/usr/bin/apt-get update,/usr/bin/apt-get upgrade
Alternately, we can also use the glob match '*'.
Code:
%admin jaunty=(root)NOPASSWD:/usr/bin/apt-get up*
Code:
%admin jaunty=(root)NOPASSWD:/*/sbin/*
Restricting use of sudo to Single Users
As well as specifying groups, we can hand out sudo permissions on a 'per-user' basis too. For example, to only allow the user 'iain' (me) to run 'apt-get update' and 'apt-get upgrade', we use the following:
Code:
iain jaunty=(root)NOPASSWD:/usr/bin/apt-get update,/usr/bin/apt-get upgrade
Code:
%iain jaunty=(root)NOPASSWD:/usr/bin/apt-get update,/usr/bin/apt-get upgrade
There is a hidden 5th that I forgot to mention. In some cases, you want to restrict the application once it has been given root powers.
ie: vim, less and some other similar applications can allow you to 'shell out' of the application and run commands using the ! operator, as an example:
Code:
:!aptitude
For this, we have the NOEXEC option, with will prevent the command from shelling out.
Code:
%admin ALL=(root)NOEXEC:/usr/bin/vim
Host Alias Specification
Host aliases are declared as so, to use my hostname as an example:
Code:
Host_Alias HOST = jaunty
Code:
Host_Alias LAN = 192.168.1.0/255.255.255.0
Code:
User_Alias FUSE_USERS = andy,ellz,matt,jamie
The runas alias, are aliases for the users you can sudo as, via the 'sudo -u' command. Again, I won't go into this, but it works like so:
Code:
Runas_Alias USERS = root,andy,ellz,matt,jamie
Code:
%admin ALL=(USERS) ALL
Command Alias Specification
And lastly, the command alias, as you may have guessed, are aliases for the command names. To skip any brief talk about them, lets first fufil what my original scenario intended.
Code:
Cmnd_Alias CRYPT = /usr/bin/truecrypt Cmnd_Alias USBDEV = /usr/bin/unetbootin,/usr/bin/gnome-format Cmnd_Alias APT = /usr/bin/apt-get update,/usr/bin/apt-get upgrade Cmnd_Alias UPDATES = /usr/bin/update-manager Cmnd_Alias FUSE = /usr/bin/Gmount-iso Cmnd_Alias MYPROGS = CRYPT,USBDEV,APT,UPDATES,FUSE
Gelling it together
With the above in place, we can now write out lines such as this:
Code:
iain HOME=(root)NOPASSWD:MYPROGS
The Result
Put that all together, and we have something that looks like this:
Code:
Defaults env_reset,tty_tickets # Host alias specification Host_Alias HOST = jaunty Host_Alias LAN = 192.168.1.0/255.255.255.0 Host_Alias HOME = HOST,LAN # User alias specification # Cmnd alias specification Cmnd_Alias CRYPT = /usr/bin/truecrypt Cmnd_Alias USBDEV = /usr/bin/unetbootin,/usr/bin/gnome-format Cmnd_Alias APT = /usr/bin/apt-get update,/usr/bin/apt-get upgrade Cmnd_Alias UPDATES = /usr/bin/update-manager Cmnd_Alias FUSE = /usr/bin/Gmount-iso Cmnd_Alias MYPROGS = CRYPT,USBDEV,APT,UPDATES,FUSE # User privilege specification root ALL=(ALL) ALL # Members of the admin group may gain root privileges %admin HOME=(root) ALL %admin HOME=(root) NOEXEC:/usr/bin/vim iain HOME=(root) NOPASSWD:MYPROGS
No comments:
Post a Comment