Sudo

    简介

sudo是linux系统管理指令,是允许系统管理员让普通用户执行一些或者全部的root命令的一个工具,如halt,reboot,su等等。这样不仅减少了root用户的登录 和管理时间,同样也提高了安全性。sudo不是对shell的一个代替,它是面向每个命令的。

它的特性主要有这样几点:

§ sudo能够限制用户只在某台主机上运行某些命令。
§ sudo提供了丰富的日志,详细地记录了每个用户干了什么。它能够将日志传到中心主机或者日志服务器。
§ sudo使用时间戳文件来执行类似的“检票”系统。当用户调用sudo并且输入它的密码时,用户获得了一张存活期为5分             钟的票(这个值可以在编译的时候改变)。
§ sudo的配置文件是sudoers文件,它允许系统管理员集中的管理用户的使用权限和使用的主机。它所存放的位置默认          是在/etc/sudoers,属性必须为0440。

 

 

在sudo于1980年前后被写出之前,一般用户管理系统的方式是利用su切换为超级用户。但是使用su的缺点之一在于必须要先告知超级用户的密码。
        sudo使一般用户不需要知道超级用户的密码即可获得权限。首先超级用户将普通用户的名字、可以执行的特定命令、按照哪种用户或用户组的身份执行等信息,登记在特殊的文件中(通常是/etc/sudoers),即完成对该用户的授权(此时该用户称为“sudoer”);在一般用户需要取得特殊权限时,其可在命令前加上“sudo”,此时sudo将会询问该用户自己的密码(以确认终端机前的是该用户本人),回答后系统即会将该命令的进程以超级用户的权限运行。之后的一段时间内(默认为5分钟,可在/etc/sudoers自定义),使用sudo不需要再次输入密码。
        由于不需要超级用户的密码,部分Unix系统甚至利用sudo使一般用户取代超级用户作为管理帐号,例如Ubuntu、Mac OS X

 

编辑配置文件命令:visudo
        ※注意:编辑sudo的配置文件/etc/sudoers是一般不要直接使用vi(vi /etc/sudoers)去编辑,因为sudoers配置有一定的语法,直接用vi编辑保存系统不会检查语法,如有错也保存了可能导致无法使用sudo工具,最好使用visudo命令去配置。虽然visudo也是调用vi去编辑,但是保存时会进行语法检查,有错会有提示
       要使用sudo,用户 必须提供一个指定用户名和密码。
       注意:sudo需要的不是目标用户的密码,而是执行sudo的用户的密码

        如果不在sudoers中的用户通过sudo执 行命令,sudo会向管理员报告这一事件。用户可以通过sudo -v来查看自己是否是在sudoers 之中。如果是,它还可以更新你的“入场券”上的时间;如果不是,它会提示你,但不会通知管理员。

 

默认配置文件位置:/etc/sudoers

 

首先将主机分类,目的是为了更好地管理
## Host Aliases      机器别名

## Groups of machines. You may prefer to use hostnames (perhaps using

## wildcards for entire domains) or IP addresses instead.

# Host_Alias     FILESERVERS = fs1, fs2

# Host_Alias     MAILSERVERS = smtp, smtp2

 

 

用户定义,将用户分为ADMINS…… 几类

## User Aliases   用户别名

## These aren't often necessary, as you can use regular groups

## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname

## rather than USERALIAS

# User_Alias ADMINS = jsmith, mikem

 

 

 

## Command Aliases     命令别名

## These are groups of related commands...     以下是替换命令 

 

## Networking    网络类命令别名

# Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool

 

## Installation and management of software   安装管理软件命令别名

# Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum

 

## Services    服务类命令别名

# Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig, /usr/bin/systemctl start, /usr/bin/systemctl stop, /usr/bin/systemctl reload, /usr/bin/systemctl restart, /usr/bin/systemctl status, /usr/bin/systemctl enable, /usr/bin/systemctl disable

 

## 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

 

 

 

 

# 这里是针对不同的用户采用不同地策略

# Defaults specification

 

#

# Disable "ssh hostname sudo <cmd>", because it will show the password in clear.

#         You have to run "ssh -t hostname sudo <cmd>".

#

Defaults    requiretty

禁用“ssh主机名sudo < cmd >”,因为它将显示清晰的密码。
        你必须运行ssh - t主机名sudo < cmd >”。

 

 

#

# Refuse to run if unable to disable echo on the tty. This setting should also be

# changed in order to be able to use sudo without a tty. See requiretty above.

#

Defaults   !visiblepw

 

 

 

 

具体配置:

 

哪些用户可以  在哪些机器   运行哪些软件

## Next comes the main part: which users can run what software on

## which machines (the sudoers file can be shared between multiple

## systems).

## Syntax:

##

##      user    MACHINE=COMMANDS

##

## The COMMANDS section may have other options added to it.

##

## Allow root to run any commands anywhere

root    ALL=(ALL)       ALL

 

用户 主机=(目标用户) 命令 

 

 

举例:

允许foobar 在任何主机 使用mike用户  执行kill命令

#foobar all=(mike)/bin/kill

 

允许sys组 任何主机 运行网络和软件命令

## Allows members of the 'sys' group to run networking, software,

## service management apps and more.

# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

 

允许wheel组 任何主机 运行所有命令

## Allows people in group wheel to run all commands

%wheel  ALL=(ALL)       ALL

 

允许wheel组 在任何主机 运行所有命令  不需要密码

## Same thing without a password
# %wheel        ALL=(ALL)       NOPASSWD: ALL

 

允许users组 在任何主机 运行挂载类命令

## Allows members of the users group to mount and unmount the

## cdrom as root

# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

 

允许users组 在本地 运行关机命令

## Allows members of the users group to shutdown this system

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

 

posted @ 2017-10-10 22:02  乌托邦眺望  阅读(218)  评论(0编辑  收藏  举报