View previous topic :: View next topic |
Author |
Message |
pandaxiongmao Guru
Joined: 29 Sep 2003 Posts: 478 Location: USA
|
Posted: Fri May 25, 2007 12:04 am Post subject: Granting Permission to Hibernate [SOLVED] |
|
|
I want to use Code: | echo disk > /sys/power/state | in order to put my PC to hibernate mode, but how do I do it as non-root? I tried to modify /etc/sudoers, but it didn't work.
Code: | %wheel ALL=NOPASSWD: /sys/power/state
%wheel ALL=NOPASSWD: echo disk > /sys/power/state
%wheel ALL=NOPASSWD: /usr/bin/echo disk > /sys/power/state |
All 3 of them refused to grant permission to non-root.
BTW, I tried suspend2, and it failed miserably, so I'm not going to redo it again.
Last edited by pandaxiongmao on Mon Jun 04, 2007 12:55 pm; edited 1 time in total |
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 23064
|
Posted: Fri May 25, 2007 1:07 am Post subject: |
|
|
Can you paste the failed command and its output? I suspect you were attempting to sudo an echo, with the output of the sudo redirected by the non-root shell to /sys/power/state. That would obviously fail, as the entire point of the sudo is that the non-root shell lacks permission to do that redirection.
As a cheesy hack, save the below code block as /usr/local/bin/suspend-to-disk.sh and tell sudo to let anyone run it.
Code: |
#!/bin/sh
echo disk > /sys/power/state
|
This should work since it moves the redirection into the spawned process, making it unambiguous when you issue your command that you are sudo'ing a command with nothing special, and unknown to sudo or your shell, that command happens to perform some redirected output. |
|
Back to top |
|
|
pandaxiongmao Guru
Joined: 29 Sep 2003 Posts: 478 Location: USA
|
Posted: Fri May 25, 2007 1:33 am Post subject: |
|
|
It failed because of the lack of permission, but thanks for letting me know that I can turn the command into bash script.
Next, how do I tell sudo to allow me to execute the script?
I added
Code: | %wheel ALL=NOPASSWD: /usr/local/bin/hibernate.sh |
into /etc/sudoers, is that the correct way?
Next, I ran
Code: | sudo . /usr/local/bin/sh |
But the sudo couldn't find the period "." command, so I tried to change the ownership of hibernate.sh to my regular account, but I got denied again whenever I tried to run it.
Does this below command exist?
|
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 23064
|
Posted: Fri May 25, 2007 2:55 am Post subject: |
|
|
sudo /usr/local/bin/hibernate.sh should work to run it, once you configure sudo to allow it. I do not use sudo, so I cannot advise whether you have configured it right. |
|
Back to top |
|
|
pandaxiongmao Guru
Joined: 29 Sep 2003 Posts: 478 Location: USA
|
Posted: Fri May 25, 2007 5:27 am Post subject: |
|
|
It didn't work, sudo still complained that it couldn't find /usr/local/bin/hibernate.sh.
Thanks for the reply, btw. |
|
Back to top |
|
|
Hu Administrator
Joined: 06 Mar 2007 Posts: 23064
|
Posted: Fri May 25, 2007 10:31 pm Post subject: |
|
|
What did you name the file that holds the shell script I posted? I thought from your /etc/sudoers file that it was /usr/local/bin/hibernate.sh. |
|
Back to top |
|
|
pandaxiongmao Guru
Joined: 29 Sep 2003 Posts: 478 Location: USA
|
Posted: Sat May 26, 2007 4:59 am Post subject: |
|
|
It is.
Sudo complains
Code: | sudo: /usr/local/bin/hibernate.sh: command not found |
I tried several different ways to execute the command (look at the example I provided underneath,) but they didn't work.
Code: | sudo sudo /usr/local/bin/hibernate.sh
sudo . /usr/local/bin/hibernate.sh |
|
|
Back to top |
|
|
Monkeh Veteran
Joined: 06 Aug 2005 Posts: 1656 Location: England
|
Posted: Sat May 26, 2007 5:14 am Post subject: |
|
|
You.. don't want to try and source it. Is the file executable? |
|
Back to top |
|
|
pandaxiongmao Guru
Joined: 29 Sep 2003 Posts: 478 Location: USA
|
Posted: Mon May 28, 2007 1:20 pm Post subject: |
|
|
Yes, the script works flawlessly if I execute it as root. In fact, I just attempted it before I posted this message. _________________ CPU: Intel Core 2 Duo
GPU: nVidia GeForce 9800 GT
MB: Asus P5N-E SLI |
|
Back to top |
|
|
pandaxiongmao Guru
Joined: 29 Sep 2003 Posts: 478 Location: USA
|
Posted: Mon Jun 04, 2007 12:55 pm Post subject: |
|
|
Okay, I found the solution.
1. Create a file called hibernate at /usr/bin
Code: | su -
cd /usr/bin
vim hibernate |
2. Fill in the file with these lines
Code: | #!/bin/bash
echo mem > /sys/power/state |
3. Make it executable by typing
Code: | chmod 755 hibernate |
4. Add the new file to the wheel group or any other group by using
Code: | chgrp wheel hibernate |
5. Finally, add this line to /etc/sudoers
Code: | %wheel ALL=(ALL) NOPASSWD: /usr/bin/hibernate |
6. In order to run the file, you must type
7. Voila _________________ CPU: Intel Core 2 Duo
GPU: nVidia GeForce 9800 GT
MB: Asus P5N-E SLI |
|
Back to top |
|
|
|