超级用户的权利

root用户有在linux执行任何命令的权利,是超级管理员。在真实的环境中,一般不要以root的身份登录主机,因为root可以执行任何命令,可能带来误操作,就有可能带来意想不到的失误。所以一般管理员都是通过一个普通用户登录,然后在susudo去执行相关的命令。这样的好处是,防止误操作,管理员不需要太多的权限就可以执行相应的命令,同时有命令日志可以记录执行命令的情况。

 

下面说明sudo用户的相关知识:

 

sudo:普通用户root用户的身份执行本身不能执行的命令

 

sudo的配置文件:/etc/sudoers,在这个配置文件里规定了谁可以运行sudo,运行时是否需要密码,可以sudo运行哪些命令

 

编辑配置文件用:visudo命令visudo命令会检查是否有他人在修改这个文件,并在修改之后可以检查sudoer的语法。

 

配置文件说明:

别名;必须全部而且只能使用大写英文字母结合。别名的定义用于被引用,相当于一个变量

 

who(谁,执行命令者) which_host(在哪些主机上)=(runas)[作为谁执行命令] command(要执行的命令)

 

可以通过别名(将特定的用户或允许执行的命令放在一起)进行表示,有利于日后的操作与修改

who               User_Alias(用户别名)

which_host:        Host_Alias(主机别名)

runas:              Runas_Alias(runas别名)

command           Cmnd_Alias(命令别名,一般要指定完整的命令路径)                         

 

别名使用语法:

Alias_Type NAME = item1, item2, ...                ##定义别名类型

 

Alias_Type NAME = item1, item2, item3 :NAME = item4, item5            ##定义一个name

 

User_Alias:

           用户名:username

           组名:使用%号引导,%group

           已经定义了的User_Alias别名     

                  

Host_Alias

           主机名

           IP地址

           网络地址

           其他主机别名(已经定义的别名列表)

                  

runas_Alias:

          用户名:username

          组名:使用%号引导,%group

          已经定义了的runas_Alias别名

                  

Cmnd_Alias:

          命令

          已经定义了的命令别名

实例:                  

User_Alias ADMINS = jsmith, mikem
 
Host_Alias     FILESERVERS = fs1, fs2
Host_Alias     MAILSERVERS = smtp, smtp2               
                  
Cmnd_Alias SOFTWARE = /bin/rpm,/usr/bin/up2date, /usr/bin/yum
 
ADMINS   FILESERVERS=SOFTWARE#ADMINS用户可以在FILESERVERS主机上运行的命令在SOFTWARE中                   
 
## Command Aliases
## These are groups of related commands...
 
命令别名实例:
  
## Installation and management of software
#Cmnd_Alias SOFTWARE = /bin/rpm,/usr/bin/up2date, /usr/bin/yum
 
## Services
#Cmnd_Alias SERVICES = /sbin/service,/sbin/chkconfig
 
## Updating the locate database
#Cmnd_Alias LOCATE = /usr/bin/updatedb
 
## Storage
#Cmnd_Alias STORAGE = /sbin/fdisk,/sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount
 
## Delegating permissions
#Cmnd_Alias DELEGATING = /usr/sbin/visudo,/bin/chown, /bin/chmod, /bin/chgrp
 
## Processes
#Cmnd_Alias PROCESSES = /bin/nice,/bin/kill, /usr/bin/kill, /usr/bin/killall
 
## Drivers
#Cmnd_Alias DRIVERS = /sbin/modprobe               
                  
##root用户拥有所有权限             
## Allow root to run any commands anywhere
root   ALL=(ALL)       ALL
 
##允许wheel组中的所有成员执行所有命令的权限
## Allows people in group wheel to run allcommands
# %wheel        ALL=(ALL)       ALL
##允许wheel组中的所有成员执行所有命令时可以不输入密码,NOPASSWD:ALL(命令)
## Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL         
                  
## Allows members of the users group tomount and unmount the
## cdrom as root
# %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

 

##允许指定的用户进行关机

## Allows members of the users group toshutdown this system

# %users localhost=/sbin/shutdown -h now

 

 

实例:允许xcroom用户进行用户添加和修改

xcroom        ALL=(root)      /usr/sbin/useradd,/usr/sbin/usermod  

 

[[email protected] ~]$ sudo /usr/sbin/useradd tom
[sudo] password for wencheng:
[[email protected] ~]$ tail -1 /etc/passwd
tom:x:5002:5002::/home/tom:/bin/bash       
                  
[[email protected] ~]$ sudo /usr/sbin/usermod -scsh tom
[[email protected] ~]$ tail -1 /etc/passwd
tom:x:5002:5002::/home/tom:csh

sudo的相关选项:

sudo -l:列出当前用户可以使用的sudo类命令,sudo -l xcroom

         (root)/usr/sbin/useradd, (root) /usr/sbin/usermod

sudo -k:让认证信息失效,重新验证   

 

指定shell运行

[[email protected] ~]$ sudo /usr/sbin/usermod -sbash tom
[sudo] password for wencheng:

允许xcroom用户可以不输入密码运行/usr/sbin/useradd命令,运行/usr/bin/usermod需要密码才能运行                

xcroom  ALL=(root)      NOPASSWD: /usr/sbin/useradd, PASSWD:/usr/sbin/usermod 

 

command前使用TAG:标签   TAG: COMMAND    

                  

User_Alias      USERADMIN = xcroom,%xcroom

Cmnd_AliasUSERCMND=/usr/sbin/useradd,/usr/sbin/userdel,/usr/sbin/usermod,/usr/bin/passwd[A-Za-z]*,! /usr/bin/passwd root

 

USERADMIN       ALL=(root)      NOPASSWD: USERCMND 


最后附上Linux系统管理技术手册上的一张配置图:

SUDO的有关知识总结