Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
With my current setup the only way to shut down my linux install is by going to a konsole and typing:
su
password
halt
I would like otherusers (besides me, the admin) to be able to shut down linux. How could I write a simple script that when clicked by them would execute:
su
password
halt
automatically?
OK, you don't actually want to do that for the simple reason that you would be making root's password visible to anyone looking at the script and that would give them the ability to log in as root and cause unending mayhem. The better way is to give users access to the halt command using sudo. Sudo is desinged to do exactly what you want to do, namely give normal users acces to specific commands with root privileges. The upside is that they don't need to know root's password and they can only execute the commands that they've explicitly been allowed to execute. You edit the /etc/sudoers file using the visudo command and would enter in a line like this:
%users computername=NOPASSWD: /sbin/halt
That would allow any user in the users group to turn off the computer by typing sudo halt at the command prompt. Or you could write a script
Code:
#!/bin/bash
sudo halt
Be sure to read through man sudo and man visudo for more details.
Not sure if this is exactly what you're looking for, but to allow all users to be able to shutdown or reboot you could:
chmod 4755 /sbin/shutdown
chmod 4755 /sbin/reboot
Users would be able to use /sbin/shutdown & /sbin/reboot commands.
Or as the previous user suggested you could add sudo entries using visudo as root.
I tried that but it still doesn't work. Hmmm....
Quote:
Sudo is desinged to do exactly what you want to do, namely give normal users acces to specific commands with root privileges. The upside is that they don't need to know root's password and they can only execute the commands that they've explicitly been allowed to execute. You edit the /etc/sudoers file using the visudo command and would enter in a line like this:
%users computername=NOPASSWD: /sbin/halt
That would allow any user in the users group to turn off the computer by typing sudo halt at the command prompt. Or you could write a script
I opened up sudo in visudo... but where do I specifically put the command?
I'm not at my linux box so I can't paste an example, but sudoers is just a text file. I think you can enter than line pretty much anywhere. Again, you really want to have a read through the man pages for sudo and visudo.
Alright... I'll try again to change the sudo command..
I tried the chmod 4755 /sbin/ commands for:
halt
poweroff
shutdown
reboot
as root and as a user. Later I went to shutdown as a user and it said that I needed to be superuser, so I figured that it hadn't worked. I then went to root and typed halt, AND IT SAID I HAD TO BE SUPER USER! To my knowledge there isn't a user more super than root! What can I do to reverse this so I can shutdown my computer!!!???
It sounds like the permissions are screwed up. You might post the output of ls -l and we can have a look. You could also try chmod 755 in the /sbin directory and see if that does the trick (do this as root).
If you want to try the sudo route (which is a much safer way to do this), here is my sudoers file as an example. This allows any user to shut down or reboot the macine and mount and unmount the cd drive. Note that the Slacktop name is the name of my laptop:
Code:
# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#
# Host alias specification
# User alias specification
# Cmnd alias specification
# Defaults specification
# 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
# %wheel ALL=(ALL) NOPASSWD: ALL
# Samples
#%users ALL=/sbin/mount /cdrom,/sbin/umount /cdrom
#%users ALL=/sbin/reboot NOPASSWD: ALL
%users Slacktop = NOPASSWD: /sbin/reboot,/sbin/halt,/bin/mount /dev/cdrom,/bin/umount /dev/cdrom
It sounds like the permissions are screwed up. You might post the output of ls -l and we can have a look. You could also try chmod 755 in the /sbin directory and see if that does the trick (do this as root).
Thanks... I'm currently running winblows.. let me restart real quick here and I'll post the results.
I'm guessing that sudo can only be edited in visudo, right?
I think I see the problem. Several of the commands are owned by andrew and in the users group. At very least you want to chown them to root users, if not root root or root bin and then use sudo.
Quote:
Im in visudo now but I cannot seem to add a line... and I think it just deleted a line.. how can I exit it without saving changes?
Visudo uses the vi editor, which in my opinion is one of the most horrible editors ever created. Anyway, you get out with :q. Also, you may want to bookmark a vi help page like this one.
This is my sudoers file, giving my user (slight) specific permission to execute the shutdown command.
Code:
# sudoers file.
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the sudoers man page for the details on how to write a sudoers file.
#
# Host alias specification
# User alias specification
User_Alias TRUSTED = slight
# Cmnd alias specification
Cmnd_Alias SHUTDOWN = /sbin/shutdown
# Defaults specification
# User privilege specification
root ALL=(ALL) ALL
TRUSTED ALL=SHUTDOWN
I then wrote this little script that uses Xdialog to make a little gtk+ window popup when the script is called:
Code:
#!/bin/sh
DIALOG=Xdialog
$DIALOG --title "SHUTDOWN" \
--radiolist "choose:" 0 0 3\
"-h" "Shutdown" off \
"-r" "Restart" on 2>/tmp/checklist.tmp.$$
retval=$?
choice=`cat /tmp/checklist.tmp.$$`
rm -f /tmp/checklist.tmp.$$
case $retval in
0)
sudo /sbin/shutdown $choice now ;;
1)
echo "Cancel pressed.";;
255)
echo "Box closed.";;
esac
The script produces this:
Hope this helps. Feel free to copy and paste if you wish.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.