【原创】Linux基础之sudo


sudo容许用户以其余用户的身份(好比root)执行命令,好比切换用户、执行命令、读写文件等;vim

配置

sudo配置在:/etc/sudoerside

## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## Examples are provided at the bottom of the file for collections
## of related commands, which can then be delegated out to particular
## users or groups.
##
## This file must be edited with the 'visudo' command.
...
## 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
...
## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.dui

从这个配置文件能够看到3点:ip

  • 配置的格式(user MACHINE=COMMANDS)以及示例(在末尾附近),好比root的权限;
  • /etc/sudoers.d/* 这个目录下边的文件也会被加载,这样就能够把不一样类别的权限配置分开到不一样的文件;
  • 使用visudo编辑,使用vi或vim编辑也能够,不过visudo在保存前会作检查,若是配置有问题会报错,直接保存错误的配置可能致使难以预料的后果,千万不要尝试;

Options are:
(e)dit sudoers file again
e(x)it without saving changes to sudoers file
(Q)uit and save changes to sudoers file (DANGER!)get

What now?it

经常使用场景

1 切换用户

username ALL=(root) NOPASSWD: /bin/su - targetuserio

容许username切换到targetusertable

%groupname ALL=(root) NOPASSWD: /bin/su - targetuser配置

容许groupname里边的用户切换到targetuserfile

2 执行命令

username ALL=(root) NOPASSWD: /usr/sbin/iptables

容许username执行iptables命令

相关文章
相关标签/搜索