分页: 2 / 4

Re: 如何关机?

发表于 : 2010-06-17 9:29
长头发的和尚
Z想干嘛

Re: 如何关机?

发表于 : 2010-06-17 12:24
aerofox
罗非鱼 写了:pt@pt-laptop:~$ sudo apt-get install gnome-power-manager
[sudo] password for pt:
Reading package lists... Done
Building dependency tree
Reading state information... Done
gnome-power-manager is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.
pt@pt-laptop:~$ man gnome-power-cmd
No manual entry for gnome-power-cmd
有问题找 man 的想法不错,但是这个命令恰好是没有 manpage 的,不能因为没有 man 就断定不存在这个命令吧?为什么不直接试一下命令呢?

Re: 如何关机?

发表于 : 2010-06-17 12:44
aerofox
pocoyo 写了:没这个命令.
抱歉,刚才到 Ubuntu 10.04 中找一下,gnome-power-manager 软件包中确实不包含 gnome-power-cmd,Debian 5.0 中是包含的。
用 sudo 的方法吧,试过了吗?一定可以的。

Re: 如何关机?

发表于 : 2010-06-17 12:46
aerofox
Debian 中的 gnome-power-cmd 命令是个脚本文件,内容如下,估计放到 Ubuntu 中也可以用:

代码: 全选

#!/bin/sh
# Copyright (C) 2007 Richard Hughes <[email protected]>
#
# Licensed under the GNU General Public License Version 2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

#$1 = method name
execute_dbus_method ()
{
	dbus-send --session --dest=org.freedesktop.PowerManagement		\
		  --type=method_call --print-reply --reply-timeout=2000	\
		  /org/freedesktop/PowerManagement 			\
		  org.freedesktop.PowerManagement.$1
	if [ $? -eq 0 ]; then
		echo "Failed"
	fi 
}

if [ "$1" = "suspend" ]; then
	echo "Suspending"
	execute_dbus_method "Suspend"
elif [ "$1" = "hibernate" ]; then
	echo "Hibernating"
	execute_dbus_method "Hibernate"
elif [ "$1" = "reboot" ]; then
	echo "Rebooting"
	execute_dbus_method "Reboot"
elif [ "$1" = "shutdown" ]; then
	echo "Shutting down"
	execute_dbus_method "Shutdown"
elif [ "$1" = "" ]; then
	echo "command required: suspend, shutdown, hibernate or reboot"
else
	echo "command '$1' not recognised, only suspend, shutdown, hibernate or reboot are valid"
	exit 1
fi

Re: 如何关机?

发表于 : 2010-06-18 21:57
Bill Lee
我说的很清楚了

代码: 全选

# !/bin/bash
do
        if [ -e "~/shutdownLock" ]; then
                sleep 10
        else
                shutdown -h now
        fi
done
保存为 TestShutDown.sh
然后

代码: 全选

chmod u+x TestShutDown.sh
sudo chown root\: TestShutDown.sh
sudo ./TestShutDown.sh
再执行 sudo -k 清除 sudo 密码缓存
只有 TestShutDown.sh 会以 root 执行。而你原来的脚本并没有 root 权限。另外修改 TestShutDown.sh 也需要 root 权限,这样就可以防止 root 权限被盗用。

Re: 如何关机?

发表于 : 2010-06-19 1:36
erol
本来想说给shell脚本用suid的,做了一下实验发现我那ubuntu10.04上的bash 4.1.5不吃这一套...残念
用别方式的加上suid就应该行了吧
我很白,错了不要笑我...

Re: 如何关机?

发表于 : 2010-06-23 15:31
mgqw
用超级用户创建shell脚本,然后修改此脚本的SUID,应该就可以了吧

Re: 如何关机?

发表于 : 2010-06-23 18:07
only_one
sudo chown root filename
sudo chmod u+s filename

刚刚学习shell 不知行不行 :em06

Re: 如何关机?

发表于 : 2010-06-24 21:11
onelynx
直接拔电源就是了

Re: 如何关机?

发表于 : 2010-06-27 7:58
罗非鱼
各位谈呢这么多都没有到点子上,请问,linux(或者说ubuntu10.04)中,普通用户(没有root密码),可否用命令行关闭计算机??

Re: 如何关机?

发表于 : 2010-06-27 8:55
aerofox
上面给出了几种方法,你偏不用,还想要什么办法呢?

Re: 如何关机?

发表于 : 2010-06-28 8:53
hongszh
aerofox 写了:知道 sudo su,为什么不直接 sudo shutdown 呢?

代码: 全选

visudo
然后加入一行:

代码: 全选

%sudo ALL=NOPASSWD: /sbin/shutdown
然后脚本中就可以用

代码: 全选

sudo /sbin/shutdown -h now
如果用 gnome 环境,也可以使用命令:

代码: 全选

gnome-power-cmd shutdown
我是这么操作的,类似上面,先加上NOPASSWD,这样sudo就不需要输入密码了:

代码: 全选

%admin ALL=(ALL)NOPASSWD:NOPASSWD: ALL
然后就可以使用init 0了:

代码: 全选

sudo init 0

Re: 如何关机?

发表于 : 2010-06-28 21:03
lilydjwg
罗非鱼 写了:各位谈呢这么多都没有到点子上,请问,linux(或者说ubuntu10.04)中,普通用户(没有root密码),可否用命令行关闭计算机??
aerofox 的就是了,使用 dbus 发个关机信号,没其它用户登录的话就立即给你关机了。

Re: 如何关机?

发表于 : 2010-06-29 19:04
罗非鱼
这帮回帖的,一个比一个不务实,我的前提条件是,没有sudo 的密码,没有root密码,普通用户根本不能使用那些命令!

Re: 如何关机?

发表于 : 2010-06-29 22:06
lilydjwg
罗非鱼 写了:这帮回帖的,一个比一个不务实,我的前提条件是,没有sudo 的密码,没有root密码,普通用户根本不能使用那些命令!
你才不务实呢!普通用户可以发dbus消息!以下是我的关机函数:

代码: 全选

shutdown () { #{{{2
  echo -n 你确定要关机吗?
  read i
  if [ $i = "y" -o $i = "是" ]
  then
    dbus-send --system --print-reply --dest=org.freedesktop.Hal /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Shutdown
  fi
}