Chinaunix首页 | 论坛 | 博客
  • 博客访问: 481850
  • 博文数量: 82
  • 博客积分: 3003
  • 博客等级: 中校
  • 技术积分: 1285
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-11 15:27
文章分类

全部博文(82)

文章存档

2011年(1)

2010年(5)

2009年(63)

2008年(13)

我的朋友

分类: LINUX

2009-10-08 20:29:00

Sudo 控制普通用户使用命令的权限

原理和介绍:

可以使用su sudo来进行用户身份的切换,若su后面没有接使用者账号,则默认是root身份,su - 表示在切换身份的同时还改变环境参数。但是用su进行身份切换的时候必须得知道要切换的那个身份的密码,比如要切换成root用户,就必须知道root的密码,这样比较危险而且不方便,在这种情况下可以使用sudo进行身份切换,使用sudo进行身份切换时只需要输入自己的密码就可以了。

但在执行sudo时,前提是必须具有执行权限,该执行权限在/etc/sudoers中定义。预设情况下只有root才有sudo执行权限。

注:一般用visudo命令来编辑/etc/sudoers文件,若直接用vi 编辑,出现错误可能造成sudo无法使用,使用visudo会在储存时去检查内部语法错误,visudo必须由root执行。

实验:配置三块网卡eth0,eth0:1,eth0:2,只有 root 用户能关掉所有网卡,其他用户只能关掉 eth0:1 eth0:2,不能关掉eth0.

1,增加两个用户:userAuserB

[root@localhost ~]# useradd userA   增加userA

You have mail in /var/spool/mail/root

[root@localhost ~]# passwd userA   userA加密码

Changing password for user userA.

New UNIX password: 

Retype new UNIX password: 

passwd: all authentication tokens updated successfully.

[root@localhost ~]# useradd userB    增加userB

[root@localhost ~]# passwd userB     userB加密码

Changing password for user userB.

New UNIX password: 

Retype new UNIX password: 

passwd: all authentication tokens updated successfully.

####查看新增的用户信息####

[root@localhost ~]# tail -n2 /etc/passwd

userA:x:501:501::/home/userA:/bin/bash

userB:x:502:502::/home/userB:/bin/bash

2,配置网卡 eth0eth0:1eth0:2

[root@localhost ~]# ifconfig eth0 192.168.0.1

[root@localhost ~]# ifconfig eth0:1 192.168.0.2

[root@localhost ~]# ifconfig eth0:2 192.168.0.3

[root@localhost ~]# ifconfig

eth0      Link encap:Ethernet  HWaddr 00:0C:29:54:25:33  

          inet addr:192.168.0.1  Bcast:192.168.0.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe54:2533/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:0 errors:0 dropped:0 overruns:0 frame:0

          TX packets:57 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:0 (0.0 b)  TX bytes:12155 (11.8 KiB)

          Interrupt:67 Base address:0x2000 

eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:54:25:33  

          inet addr:192.168.0.2  Bcast:192.168.0.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          Interrupt:67 Base address:0x2000 

eth0:2    Link encap:Ethernet  HWaddr 00:0C:29:54:25:33  

          inet addr:192.168.0.3  Bcast:192.168.0.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          Interrupt:67 Base address:0x2000 

3,改/etc/sudoers 配置文件,使userAuserB具有sudo权限

[root@localhost ~]#whereis ifconfig

(it will tell the route /sbin/ifconfig)

[root@localhost ~]# visudo

User_Alias ADMINS = userA, userB

ADMINS ALL=NOPASSWD: /bin/su,/sbin/ifconfig, !/sbin/ifconfig eth0 down

注:这里采用定义别名的方式指定具有权限的一些用户,定义别名一定要大写。别名不同于组,定义组权限时前面要加上%。表示以root用户的身份执行ifconfig的命令,但不能执行ifconfig eth0 down,要是NOPASSWD前面加上(ALL)就可以转为所有身份,不仅仅是root了。

因为使用sudo只能进行一次指令,很麻烦,所以加上/bin/suADMIN的使用者就可以利用sudo su - 来切换成root身份了。

4,检查

[root@localhost ~]# sudo -u userA ifconfig eth0 down (,以userA的身份执行命令,权限不够,userB同)

SIOCSIFFLAGS: Permission denied

[root@localhost ~]# sudo -u userA ifconfig eth0:1 down userB同)SIOCSIFFLAGS: Permission denied

[root@localhost ~]# su - userA

[userA@localhost ~]$ sudo /sbin/ifconfig eth0 down

Sorry, user userA is not allowed to execute '/sbin/ifconfig eth0 down' as root on localhost.localdomain.

[userA@localhost ~]$ sudo /sbin/ifconfig eth0:1 down root的身份执行该命令是可以的

[userA@localhost ~]$ /sbin/ifconfig 

eth0      Link encap:Ethernet  HWaddr 00:0C:29:54:25:33  

          inet addr:192.168.0.1  Bcast:192.168.0.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe54:2533/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:0 errors:0 dropped:0 overruns:0 frame:0

          TX packets:91 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000 

          RX bytes:0 (0.0 b)  TX bytes:20966 (20.4 KiB)

          Interrupt:67 Base address:0x2000 

eth0:2    Link encap:Ethernet  HWaddr 00:0C:29:54:25:33  

          inet addr:192.168.0.3  Bcast:192.168.0.255  Mask:255.255.255.0

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          Interrupt:67 Base address:0x2000 

[userA@localhost ~]$ sudo su -    不用输入密码就可以直接切换成root用户

[root@localhost ~]# 

再次切换到root身份visudo将别名中的userB去掉,使userB不再具有sudo的执行权限以后进行测试如下:

[userB@localhost ~]$ sudo /sbin/ifconfig eth0:2 down

We trust you have received the usual lecture from the local System

Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.

    #2) Think before you type.

    #3) With great power comes great responsibility.

Password:    ##输入userB的密码##

userB is not in the sudoers file.  This incident will be reported.

阅读(2058) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~