Thursday, January 20, 2011

Creating Reboot and Shutdown applets in FreeBSD 8.1

I've already mentioned that, in the versions of FreeBSD that I've played with, there are no buttons for reboot or shutdown.
There is a Logoff option in Systems>>Administration which brings you to the greeter page where there are Reboot/Shutdown options available, but this but, on my computer at least, is non-functional.
Therefore, I've always had to open a terminal as root, and type either "shutdown -r now" to reboot or set the switch to -p to power off.
So, I needed to make an applet for the gnome panel that did the job for me. However, the commands to shutdown or reboot must be run as root. If I just make a simple script, I'm going to be asked for a password which is inelegant to say the least.
In any event, here's the script (for reboot)

#! /bin/sh
sudo shutdown -r now

To get it to run from an applet without asking for a apssword. the first thing I needed to do was to install sudo from /usr/ports/security/sudo.
Next, I needed to open /usr/local/etc/sudoers and edit it to include myself as a "sudoer" or, in other words, to have sudo privileges.
Now, this is supposed to be done with visudo (run as root in a terminal) but visudo uses the vi editor which, I will admit, I have for many years had the most profound difficulty in using.
However, visudo does have one great advantage in that syntax errors will be corrected before saving so you don't run the risk of messing up this vital file.
Nevertheless, I used nano to edit /usr/local/etc/sudoers. The following lines in the sudoers file are relevant to what we need now:

# User privilege specification
root ALL=(ALL) ALL

# Uncomment to allow people in group wheel to run all commands
# %wheel ALL=(ALL) ALL

# Same thing without a password

To allow myself to use sudo I must enter the following line
paul ALL=(ALL) ALL

However, remember that I also need to run the shutdown script as root without having the script ask me for a password.
I can easily achieve this by uncommenting the line

which allows all root commands issued by members of the wheel group to be run without a password and I'm in this group.
However, I really didn't want to withdraw the need for a password from all of my root commands. For the moment, I'd be happy if the NOPASSWD option was confined only to this script.
Now there is a way to do this by using a line of this type (see the NOPASSWD and PASSWD section in the Sudoers manual.
ray rushmore = NOPASSWD: /bin/kill, /bin/ls, /usr/bin/lprm

However, I did try many examples of this type in FreeBSD 8.1 but they resolutely refused to work. I'll try again when I get FreeBSD running again.
However, for the moment, I have to be satisfied with the uncommented # %wheel ALL=(ALL) NOPASSWD: ALL" line.
Now, if I run this from a desktop icon, I'm first asked if I want to Display the script or Run it in a terminal -- not exactly what I was looking for.
However, I can easily drag the desktop icons to the Gnome panels where they'll stay and will run without first asking any questions. Now that's more like it.
For the icons, you can get the appropriate shutdown or reboot icons in .png format from Iconspedia.

No comments:

Post a Comment