Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2122533
  • 博文数量: 317
  • 博客积分: 5670
  • 博客等级: 大校
  • 技术积分: 3677
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-10 17:51
文章分类

全部博文(317)

文章存档

2016年(2)

2015年(44)

2014年(68)

2013年(42)

2012年(23)

2011年(51)

2010年(67)

2009年(17)

2008年(3)

分类: LINUX

2014-08-06 11:29:22

版权声明:原创作品,谢绝转载!否则将追究法律责任。
原文BLOG:http://iminmin.blog.51cto.com/689308/455992

sudo” Unix/Linux平台上的一个非常有用的工具,允许为非根用户赋予一些合理的权利,让他们执行一些只有根用户或特许用户才能完成的任务,从而减少根用户的登陆次数和管理时间同时也提高了系统安全性。

  • sudo的目的:为非根用户授予根用户的权限;
  • 配置文件:/etc/sudoers
  • visudo命令编辑修改/etc/sudoers配置文件

1、一般用户赋权设置:

[root@localhost ~]# visudo
……前面省略
69 ## Syntax:
70 ##
71 ##      user    MACHINE=COMMANDS
72 ##
73 ## The COMMANDS section may have other options added to it.
74 ##
75 ## Allow root to run any commands anywhere
76 root    ALL=(ALL)       ALL
77 test   ALL=(root) /usr/sbin/useradd         //新增加用户行
……后面省略
 

        说明:
        第一个字段:root为能使用sudo命令的用户;
        第二个字段:第一个ALL为允许使用sudo的主机,第二个括号里的ALL为使用sudo后以什么身份(目的用户身份)来执行命令;
        第三个字:ALL为以sudo命令允许执行的命令;
  上列解释: test   ALL=(root) /usr/sbin/useradd
表示允许test用户从任何主机登录,以root的身份执行/usr/sbin/useradd命令。

        用户执行命令效果:

[root@server ~]# su - redhat
[redhat@server ~]$ sudo /usr/sbin/useradd test
//
命令需要输入完整的路径
口令:                                     //这里输入用户redhat自己的密码
[redhat@server ~]$ cat /etc/passwd |tail -5
xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
gdm:x:42:42::/var/gdm:/sbin/nologin
sabayon:x:86:86:Sabayon user:/home/sabayon:/sbin/nologin
redhat:x:500:500::/home/redhat:/bin/bash
test:x:501:501::/home/test:/bin/bash            //新增加的用户
 
2、sudo配置深入:
         1)多个用户的设置(非同一群组用户):
        对于不同需求的用户:可以按照上面的方法依次增加多行,每行对应一个用户。
        对于相同需求的多个用户
        User_Alias UUU=user1,user2……             定义用户别名;
[root@localhost ~]# visudo
……前面省略
16 ## User Aliases
17 ## These aren't often necessary, as you can use regular groups
18 ## (ie, from files, LDAP, NIS, etc) in this file - just use %groupname
19 ## rather than USERALIAS
20 # User_Alias ADMINS = jsmith, mikem      //这个就是一个实例行,按照这个写自己的
21 User_Alisa sudouser=user1,user2,user3,user4 //第一一个别名suduouser
……后面省略
69 ## Syntax:
70 ##
71 ##      user    MACHINE=COMMANDS
72 ##
73 ## The COMMANDS section may have other options added to it.
74 ##
75 ## Allow root to run any commands anywhere
76 root    ALL=(ALL)       ALL
77
sudouser   ALL=(root)    /usr/sbin/useradd  ////命令行书写格式,用户列用别名
……后面省略
 
        对于多个命令的设置:
        Cmnd_Alias CCC=command1,command2……          定义命令别名;
[root@localhost ~]# visudo
……前面省略
23 ## Command Aliases
24 ## These are groups of related commands...
25
26 ## Networking
27 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                                                     //多个命令定义一个命令别名;
……后面省略
69 ## Syntax:
70 ##
71 ##      user    MACHINE=COMMANDS
72 ##
73 ## The COMMANDS section may have other options added to it.
74 ##
75 ## Allow root to run any commands anywhere
76 root    ALL=(ALL)       ALL
77
sudouser   ALL=(root)    NETWORKING  //命令行书写格式,命令列用别名
……后面省略

        对于多主机的设置和多登陆角色的设置:
        修改 Host_Alias HHH=host1,host2……   定义主机别名;
        修改后对应的命令行主机列位置也是用别名
        Runas_Alias   RRR=role1,role2……                定义runas别名,指定的是“目的用户”,即sudo 允许转换至的用户;
        建立相关别明后,相关命令行相应的列也是用我们定义的别名。
         2)多个用户的设置(同一群组用户): 

[root@localhost ~]# visudo
……前面省略
81
82## Allows people in group wheel to run all commands
83# %wheel        ALL=(ALL)       ALL        //用户列%+群组名
……后面省略
阅读(1249) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~