XXX is not in the sudoers file 解决方案:赋予linux普通用户挂载光驱的权限

   默认普通用户不能挂载光驱,提示只有root可以

    [root@localhost~]# su - htc

    [htc@localhost ~]$ mount  /dev/cdrom  /mnt/

    mount: only root can do that

    假设机器上有几个普通用户,想让他们用光驱,但是又不能告知root密码,

    这就要求赋予他们最少的权限但是又可以用光驱,可以用sudo来实现。

    以下是测试实例

    1,root当然可以挂载

    [root@localhost~]# mount  /dev/cdrom  /mnt/

    mount: block device /dev/cdrom iswrite-protected, mounting read-only

    2,普通用户htc不行啦

    [root@localhost~]# su - htc

    [htc@localhost ~]$ mount  /dev/cdrom  /mnt

    mount: only root can do that

    3,尝试用sudo来执行,提示sudoers里边没有htc,惨,还被人记录了

    [htc@localhost~]$ sudo mount  /dev/cdrom  /mnt

    Password:

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

    4,下边就用visudo来修改权限了(/etc/sudoers文件有提示要用visudo修改)

    [root@localhost ~]# visudo

    搜索关键字cdrom找到如下的行

    ## Allows members of the users group tomount and unmount the

    ## cdrom as root

    # %users ALL=/sbin/mount /mnt/cdrom,/sbin/umount /mnt/cdrom

   

   %users表示users组, ALL表示hostlist(主机列表) 等号后边的表示可以执行的命令

    上边这一行的意思是users组中的成员,可以执行/sbin/mount /mnt/cdrom 和/sbin/umount /mnt/cdrom这两个命令

   Fedoracore 6实际中的mount命令是在/bin 下的,而不是/sbin

    [root@localhost ~]# which mount

    /bin/mount

    [root@localhost ~]# which umount

    /bin/umount

    现在要赋予htc用户可以挂载和卸载光驱的权限,添加如下行,多个命令间用逗号隔开,保存退出

htc ALL=/bin/mount  /dev/cdrom /mnt, /bin/umount  /dev/cdrom

注意上面这句htcALL=… 中的htc是用户名,不是htc这个用户的组名

可以通过#groups htc 来查找htc的组名 ,假设htc在htcs组中,那么上面的那句话可以改成:

%htcs  ALL=/bin/mount  /dev/cdrom  /mnt, /bin/umount  /dev/cdrom

同时要注意上面这两句中ALL =后面的/ bin/mount  /dev/cdrom/mnt之间是有空格的,同理 /bin/umount/dev/cdrom之间也是空一格

    5,测试,成功挂载

    [root@localhost ~]# su - htc

    [htc@localhost ~]$ sudo mount /dev/cdrom/mnt (此时会提示输入htc自己的密码而不是root的密码)

    mount: block device /dev/cdrom iswrite-protected, mounting read-only

    [htc@localhost ~]$ mount

    /dev/sda3 on / type ext3 (rw)

    proc on /proc type proc (rw)

    sysfs on /sys type sysfs (rw)

    devpts on /dev/pts type devpts(rw,gid=5,mode=620)

    /dev/sda1 on /boot type ext3 (rw)

    tmpfs on /dev/shm type tmpfs (rw)

    none on /proc/sys/fs/binfmt_misc typebinfmt_misc (rw)

    /dev/hdc on /mnt type iso9660 (ro)

   

    下边是成功卸载

    [htc@localhost ~]$ sudo umount /dev/cdrom

    [htc@localhost ~]$ mount

    /dev/sda3 on / type ext3 (rw)

    proc on /proc type proc (rw)

    sysfs on /sys type sysfs (rw)

    devpts on /dev/pts type devpts(rw,gid=5,mode=620)

    /dev/sda1 on /boot type ext3 (rw)

    tmpfs on /dev/shm type tmpfs (rw)

    none on /proc/sys/fs/binfmt_misc typebinfmt_misc (rw)

    6,怎么又不行了

    [htc@localhost ~]$ sudo mount  /dev/cdrom  /mnt/

    Sorry, user htc isnot allowed to execute '/bin/mount /dev/cdrom /mnt/' as root on localhost.

    看出以上的错误在哪里了吗?

    错误在于/mnt的后边加了一个slash/”,测试好久才发现这个问题

    下边就是成功的

    [htc@localhost ~]$ sudo mount  /dev/cdrom   /mnt

    mount: block device /dev/cdrom is write-protected,mounting read-only

    以下是需要注意的地方

    7,/etc/sudoers中没写的命令肯定不能执行,虽然/dev/cdrom就是/dev/hdc但是仍然不能执行

    [htc@localhost ~]$ ll /dev/cdrom

    lrwxrwxrwx 1 root root 3 May 2 19:48/dev/cdrom -> hdc

    [htc@localhost ~]$ sudo mount /dev/hdc /mnt

    Sorry, user htc is not allowed to execute'/bin/mount /dev/hdc /mnt' as root on localhost.

    [htc@localhost ~]$ sudo mount  /dev/cdrom  /mnt

    mount: block device /dev/cdrom iswrite-protected, mounting read-only

    下边也是同理,虽然在/dev/cdrom挂载之后就是/mnt,但是执行umount /mnt会报错

    [htc@localhost ~]$ sudo mount  /dev/cdrom  /mnt

    mount: block device /dev/cdrom iswrite-protected, mounting read-only

    [htc@localhost ~]$ sudo umount /mnt

    Sorry, user htc is not allowed to execute'/bin/umount /mnt' as root on localhost.

    [htc@localhost ~]$ sudo umount  /dev/cdrom

    [htc@localhost ~]$


关于XXX is not in the sudoers file . 的详细解释如下

在ubuntu中由于禁用了root用户,默认情况下会把安装系统时建立的用户添加到sudoers中。但在redhat和centos中并没有把任何root用户之外的用户默认的添加到sudoers之中。这样我们在执行sudo 命令时就会出现xxx is not in the sudoers file. This incident will be reported.这样的错误输出。现在为了安全起见比较提倡使用普通用户做日常操作,而在需要超级用户的时候使用sudo 来做,这样,我们就有必要把一些用户添加到sudoers之中。
其实把用户添加到sudoers之中很简单。就是用上面的第4步 visudo 来修改即可。

参考:赋予普通用户挂载光驱的权限

      is not in the sudoers file 的解决办法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值