sudo命令
sudo 介绍
sudo特性:
- sudo能够授权指定用户在指定主机上运行某些命令。如果未授权用户尝试使用 sudo,会提示联系管理员
- sudo提供了丰富的日志,详细地记录了每个用户干了什么。它能够将日志传到中心主机或者日志服务器
- sudo使用时间戳文件来执行类似的“检票”系统。当用户调用sudo并且输入它的密码时,用户获得了一张存活期为5分钟的票
-sudo的配置文件是sudoers文件,它允许系统管理员集中的管理用户的使用权限和使用的主机。它所存放的位置默认是在/etc/sudoers,属性必须为0440
sudo 组成
包:sudo
配置文件:/etc/sudo.conf
授权规则配置文件:
- /etc/sudoers #可以写在这个文件里
- /etc/sudoers.d #也可以以把授权规则放在这个文件夹里
安全编辑授权规则文件和语法检查工具
- /usr/sbin/visudo
范例:
#检查语法
visudo -c
#检查指定配置文件语法
visudo -f /etc/sudoers.d/test
授权编辑规则文件的工具:/usr/bin/sudoedit
执行授权命令:/usr/bin/sudo
时间戳文件:/var/db/sudo
日志文件:/var/log/secure
sudo 命令
sudo命令
ls -l /usr/bin/sudo
sudo –i –u wang 切换身份
sudo [-u user] COMMAND
-V 显示版本信息等配置信息
-u user 默认为root
-l,ll 列出用户在主机上可用的和被禁止的命令
-v 再延长密码有效期限5分钟,更新时间戳
-k 清除时间戳(1970-01-01),下次需要重新输密码
-K 与-k类似,还要删除时间戳文件
-b 在后台执行指令
-p 改变询问密码的提示符号
示例:-p "password on %h for user %p: "
sudo 授权规则配置
配置文件格式说明:/etc/sudoers, /etc/sudoers.d/
配置文件中支持使用通配符 glob
? 任意单一字符
* 匹配任意长度字符
[wxc] 匹配其中一个字符
[!wxc] 除了这三个字符的其它字符
\x 转义
[[alpha]] 字母
sudoers 授权规则格式
用户 登入主机=(代表用户) 命令
user host=(runas) command
范例:
root ALL=(ALL) ALL
格式说明:
- user: 运行命令者的身份
- host: 通过哪些主机
- (runas):以哪个用户的身份
- command: 运行哪些命令
sudoers的别名
User和runas:
username
#uid
%group_name
%#gid
user_alias|runas_alias
host:
ip或hostname
network(/netmask)
host_alias
command:
command name
directory
sudoedit #具有root所有权限危险
Cmnd_Alias
sudo别名有四种类型:
- User_Alias
- Runas_Alias
- Host_Alias
- Cmnd_Alias
别名格式: - A-Z*
别名定义:
Alias_Type NAME1 = item1,item2,item3 : NAME2 = item4, item5
范例:授权普通用户可以挂载光盘:
[root@centos8|20|~]#vim /etc/sudoers
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults env_keep += "HOME"
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
## 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
lin 10.0.0.8=(root) /usr/bin/mount /dev/sr0 /mnt ,/usr/bin/umount /dev/sr0
## 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
## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
## Same thing without a password
# %wheel ALL=(ALL) NOPASSWD: ALL
## Allows members of the users group to mount and unmount the
## cdrom as root
# %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom
## Allows members of the users group to shutdown this system
# %users localhost=/sbin/shutdown -h now
## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d
[root@centos8|18|~]#visudo -c
/etc/sudoers:解析正确
/etc/sudoers.d/test:解析正确
[lin@centos8|6|~]#sudo mount /dev/sr0 /mnt
我们信任您已经从系统管理员那里了解了日常注意事项。
总结起来无外乎这三点:
#1) 尊重别人的隐私。
#2) 输入前要先考虑(后果和风险)。
#3) 权力越大,责任越大。
[sudo] lin 的密码:
mount: /mnt: WARNING: device write-protected, mounted read-only.
[lin@centos8|7|~]#df
文件系统 1K-块 已用 可用 已用% 挂载点
devtmpfs 390316 0 390316 0% /dev
tmpfs 408092 0 408092 0% /dev/shm
tmpfs 408092 5828 402264 2% /run
tmpfs 408092 0 408092 0% /sys/fs/cgroup
/dev/mapper/cl-root 17811456 1776928 16034528 10% /
/dev/sda1 999320 140688 789820 16% /boot
tmpfs 81616 0 81616 0% /run/user/0
/dev/sr0 7377866 7377866 0 100% /mnt
#如果允许挂在任何设备可以写
lin 10.0.0.8=(root) /usr/bin/mount * ,/usr/bin/umount *
实战案例
注意:wheel组不能随便加人,这个组里的人都具有root权限
#案例一:
Student ALL=(ALL) ALL
%wheel ALL=(ALL) ALL
#案例2:
student ALL=(root) /sbin/pidof,/sbin/ifconfig
%wheel ALL=(ALL) NOPASSWD: ALL #不用输入口令
#案例3:别名命令
User_Alias NETADMIN= netuser1,netuser2 #用户别名
Cmnd_Alias NETCMD = /usr/sbin/ip #命令别名
NETADMIN ALL=(root) NETCMD #定义
#案例4:
User_Alias SYSADER=wang,mage,%admins
User_Alias DISKADER=tom
Host_Alias SERS=www.magedu.com,172.16.0.0/24
Runas_Alias OP=root
Cmnd_Alias SYDCMD=/bin/chown,/bin/chmod
Cmnd_Alias DSKCMD=/sbin/parted,/sbin/fdisk
SYSADER SERS= SYDCMD,DSKCMD
DISKADER ALL=