linux sudo详解

sudo:控制用户对系统命令的使用权限,root允许的操作。通过sudo可以提高普通用户的操作权限,不过这个权限是需要进行配置才可使用。

常用的命令展示

配置sudo的2种方式

1. visodu  编辑

1

visudo: 编辑sudo配置文件(相当于 vi /etc/sudoers,但是不建议用,注意检查语法)

1

visudo      编辑/etc/sudoers命令  ->98行

1

visudo -c   检查语法命令,有报错可能导致系统起不来,所以编辑之前需要备份,编辑后最好检查一下用法

  ll   /etc/sudoers  这个是一个权限很小的文件(440)
2. vi /etc/sudoers(98gg 可以快速跳转)  但是visudo功能更强大

1

2

3

4

5

Syntax:    user    MACHINE=COMMANDS      # sudo 语法

root    ALL=(ALL)       ALL              #  (All)表示允许用户以哪个用户的权限做事情

omd     ALL=(ALL)       ALL              #  omd用户在任何机器上,可以只需任何用户的任何命令 == root用户

omd     ALL=(ALL)     NOPASSWD: ALL      #  免密而且omd用户在任何机器上,可以只需任何用户的任何命令

ftl     ALL=(ALL)   /bin/cp,/bin/touch   # 只允许ftl用户只需root用户的cp,touch命令

image

取消sudo必须需要tty才能执行的限制

 编辑 /etc/sudoers ,找到 Defaults    requiretty, 然后注释掉这行: 

image

: /etc/sudoers  这个是一个权限很小的文件(440)

image

sudo基本的操作 

1

2

3

4

5

6

7

8

9

10

sudo omd ifconfig    ==>omd用户只需root权限需要输入密码

[test@oldboy ~] $ sudo su - omd

    # 通过test用户切换到omd,所以此时输入的是test用户的密码

    test用户下:su - root -c "ifconfig"   需要输入密码常用的参数:

sudo常用参数:

     -l : 登录用户下面,执行sudo -l  显示当前用户有哪些权限

     -k :删除/var/db/sudo/下面对应的时间戳的信息,下次执行sudo需要输入当前用户的密码

                --> 系统默认也是5分钟失效

                --> 配置免密是另一种情况  NOPASSWD: ALL远程sudo(有条件限制的)

sudo的原创操作:    <br>     ssh –p lemFTL@HHH omd@10.105.163.162 sudo ls /root

sudo别名的应用

 1.主机别名

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<em id="__mceDel">    # Host_Alias     FILESERVERS = fs1, fs2       -->注意逗号,空格

    # Host_Alias     MAILSERVERS = smtp, smtp2      -->注意逗号,空格

2.用户别名

    # User_Alias ADMINS = jsmith, mikem,%omd  -->omd的用户组添加上去

3.命令别名

    # Cmnd_Alias NETWORKING = /sbin/route, /sbin/iwconfig, /sbin/mii-tool

4.角色别名   -->运行时可以切换的角色

    #Runas_Alias OP = root,omd

5.实例

     user            MACHINE                         COMMANDS

    ADMINS     FILESERVERS=(Runas_Alias)            ALL   

      包含2个用户的ADMINS

      可以再包含2个机器的FILESERVERS

      切换到包含2个角色别名Runas_Alias的服务器上,

      执行包含3个命令的Cmnd_Alias                  

另:不切换用户而直接执行命令:</em>

sudo原理

普通用户运行sudo -> 检查/var/db/sudo/下是否有时间戳文件(执行成功后会创建,且免密5分钟),并检查是否已经过期
                    未过期    -> 检查/etc/sudoers里面是否配置了运行sudo和执行相应命令的权限
                                ->有权限 ->执行命令并且返回结果 ->退出
                                ->无权限 ->退出
                      过期  -> 输入当前用户自己的口令 ->检查/etc/sudoers里面是否配置了运行sudo和执行相应命令的权限
                                ->有权限 ->执行命令并且返回结果 ->退出
                                ->无权限 ->退出

sudo配置的注意事项

1.命令别名下的成员必须是文件或者是目录的绝对路径
    2.别名是包含大写字母,数字,下划线,如果是字母就必须全部大写
    3.一个别名下有多个成员,成员与成员之间必须使用逗号分隔,且成员必须有效的存在
    4.别名成员受Host_Alias,User_Alias,Cmnd_Alias,Runas_Alias别名类型制约,定义什么类型的别名,就要有什么类型的成员匹配
    5.别名规则是每行算一个规则,如果一个别名规则一行容不下是,可以通过“\”来续行
    6.指定切换的用户要用()括起来,如果省略括号,默认是root   
    7.如果不需要输入密码可以输入NOPASSWD:ALL
    8.禁止的命令可以通过!来进行限制,根据测试,先执行后面的命令,
        所以禁止的命令必须放在允许执行的命令的后面
    9.用户组前面必须用%号   
    10.所有的授权ALL字符都必须是大写
    11.允许执行的命令都是有顺序的,命令的顺序是从后向前生效
        所以禁止的命令尽量向后放

sudo管理的2个文件

 /var/db/sudo/
    /etc/sudoers
    man sudo
    man sudoers

sudo实例--企业生产环境用户权限集中管理方案实例

【更多参考】sudo实例--企业生产环境用户权限集中管理方案实例  

visudo的参数

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

The options are as follows:

     -c          Enable check-only mode.  The existing sudoers file will be checked for syntax errors, owner

                 and mode.  A message will be printed to the standard output describing the status of

     -c          Enable check-only mode.  The existing sudoers file will be checked for syntax errors, owner

                 and mode.  A message will be printed to the standard output describing the status of

                 sudoers unless the -q option was specified.  If the check completes successfully, visudo

                 will exit with a value of 0.  If an error is encountered, visudo will exit with a value of

                 1.

     -f sudoers  Specify and alternate sudoers file location.  With this option visudo will edit (or check)

                 the sudoers file of your choice, instead of the default, /etc/sudoers.  The lock file used

                 is the specified sudoers file with “.tmp” appended to it.  In check-only mode only, the

                 argument to -f may be ‘-’, indicating that sudoers will be read from the standard input.

     -h          The -h (help) option causes visudo to print a short help message to the standard output and

                 exit.

     -q          Enable quiet mode.  In this mode details about syntax errors are not printed.  This option

                 is only useful when combined with the -c option.

     -s          Enable strict checking of the sudoers file.  If an alias is used before it is defined,

                 visudo will consider this a parse error.  Note that it is not possible to differentiate

                 between an alias and a host name or user name that consists solely of uppercase letters,

                 digits, and the underscore (‘_’) character.

     -V          The -V (version) option causes visudo to print its version number and exit.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值