Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Granting Permission to Hibernate [SOLVED]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
pandaxiongmao
Guru
Guru


Joined: 29 Sep 2003
Posts: 478
Location: USA

PostPosted: Fri May 25, 2007 12:04 am    Post subject: Granting Permission to Hibernate [SOLVED] Reply with quote

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
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23064

PostPosted: Fri May 25, 2007 1:07 am    Post subject: Reply with quote

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
View user's profile Send private message
pandaxiongmao
Guru
Guru


Joined: 29 Sep 2003
Posts: 478
Location: USA

PostPosted: Fri May 25, 2007 1:33 am    Post subject: Reply with quote

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?
Code:
sudo .
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23064

PostPosted: Fri May 25, 2007 2:55 am    Post subject: Reply with quote

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
View user's profile Send private message
pandaxiongmao
Guru
Guru


Joined: 29 Sep 2003
Posts: 478
Location: USA

PostPosted: Fri May 25, 2007 5:27 am    Post subject: Reply with quote

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
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 23064

PostPosted: Fri May 25, 2007 10:31 pm    Post subject: Reply with quote

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
View user's profile Send private message
pandaxiongmao
Guru
Guru


Joined: 29 Sep 2003
Posts: 478
Location: USA

PostPosted: Sat May 26, 2007 4:59 am    Post subject: Reply with quote

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
View user's profile Send private message
Monkeh
Veteran
Veteran


Joined: 06 Aug 2005
Posts: 1656
Location: England

PostPosted: Sat May 26, 2007 5:14 am    Post subject: Reply with quote

You.. don't want to try and source it. Is the file executable?
Back to top
View user's profile Send private message
pandaxiongmao
Guru
Guru


Joined: 29 Sep 2003
Posts: 478
Location: USA

PostPosted: Mon May 28, 2007 1:20 pm    Post subject: Reply with quote

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
View user's profile Send private message
pandaxiongmao
Guru
Guru


Joined: 29 Sep 2003
Posts: 478
Location: USA

PostPosted: Mon Jun 04, 2007 12:55 pm    Post subject: Reply with quote

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
Code:
sudo hibernate

7. Voila
_________________
CPU: Intel Core 2 Duo
GPU: nVidia GeForce 9800 GT
MB: Asus P5N-E SLI
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum