Ubuntu: Do you need sudo rights to shutdown on Ubuntu?



Question:

I am looking to migrate a Snoozer application I originally wrote in windows on AutoIt, which when it comes down to it is a Shutdown Snoozer.

I list faling asleep to a movie or show, but I wake up if my pc runs through the night. So I built an application which notifies you that there is a pending shutdown and you have 10 minutes to snooze this otherwise the PC shuts down. The application allows you to repeat this prompt for X times and on a Y interval.

My question is, in Ubuntu will I need sudo rights to shutdown. If so, how can I prevent the application from continually asking the user to authenticate as sudo in order to shutdown the system properly??


Solution:1

Yes, the user need to have sudo rights, but you can use NOPASSWD to skip password authentication anyway.

To allow a user to run specific commands, in this case, the shutdown command, edit the /etc/sudoers file, for example:

user ALL=/usr/bin/apt-get,/sbin/shutdown  

A more detailed version which suits your requirement better (user can shut down using sudo shutdown -h now and reboot with sudo reboot. Users wishing to power down a system can also use poweroff or halt without password):

user hostname =NOPASSWD: /sbin/shutdown -h now,/sbin/halt,/sbin/poweroff,/sbin/reboot  

You can use the following command to show what the user can run will tell us what commands the user can run with sudo:

sudo -U user â€"l  


Solution:2

A very long time ago, I wrote this for someone with the same question. I have no idea if it still works and can't test right now because I don't want to restart :-).

Here you go:

#!/usr/bin/env python    import dbus    def main():      answer = raw_input("Do you really want to shutdown your computer? (Y/n): ")      if answer in ('y', 'Y', ''):          bus = dbus.SystemBus()          bus_object = bus.get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/devices/computer")          bus_object.Shutdown(dbus_interface="org.freedesktop.Hal.Device.SystemPowerManagement")    if __name__ == "__main__":      main()  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »