且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux让普通用户也能执行shutdown

更新时间:2022-09-11 16:57:08

以Fedora5为例,其它诸如Ubuntu之类也类似。

        当前用户名为:test,为一普通用户。在输入关机命令会出现must be superuser,需要超级用户来执行此命令。操作如下:

[test@localhost ~]$ halt
halt: must be superuser.

经测试本人发现,可以使用umask命令来设置,如下:

[test@localhost ~]$ su -
Password:
[root@localhost ~]# which halt
/sbin/halt
[root@localhost ~]# ll /sbin/halt
 -rwxr-xr-x 1 root root 12584 Feb 14  2006 /sbin/halt
[root@localhost ~]# 
chmod 4755 /sbin/halt

[root@localhost ~]# ll /sbin/halt
 -rwsr-xr-x 1 root root 12584 Feb 14  2006 /sbin/halt

[root@localhost ~]# exit

[test@localhost ~]$ halt


Broadcast message from root (pts/1) (Thu May 19 12:25:35 2011):

The system is going down for system halt NOW!

比较一下就会发现,当普通用户执行halt命令时,系统反馈说“需要超级用户来执行”,意思是可以找到我,但是你没有执行我的权限。

而shutdown 命令就不同了,普通用户执行它时,系统反馈为:“没有找到相关命令”,意思是你找不到我,当然更没有执行我的权限。

[test@localhost ~]$ shutdown
-bash: shutdown: command not found
针对这个问题,又该如何解决呢?呵呵。

[test@localhost ~]$ su -
Password:
[root@localhost ~]# chmod 0755 /sbin/shutdown
[root@localhost ~]# which shutdown
/sbin/shutdown
[root@localhost ~]# ll /sbin/shutdown
-rwxr-xr-x 1 root root 21872 Feb 14  2006 /sbin/shutdown
[root@localhost ~]# chmod 4755 /sbin/shutdown
[root@localhost ~]# ll /sbin/shutdown
-rwsr-xr-x 1 root root 21872 Feb 14  2006 /sbin/shutdown

修改完成后尝试执行如下:

[test@localhost ~]$ shutdown -h now
-bash: shutdown: command not found

还是不行!?

 通过命令echo $PATH 发现里面有没有/sbin这个路径,所以如果执行shutdown命令可以直接输入绝对路径。如下:

[test@localhost ~]$ /sbin/shutdown -h now

Broadcast message from root (pts/1) (Thu May 19 12:56:09 2011):

The system is going down for system halt NOW!

【完】



本文转自tiancong 51CTO博客,原文链接:http://blog.51cto.com/tiancong/655866