Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remotely shutdown/restart a Linux machine without password

I am writing a pyQt client-server application which restarts/shutdowns PCs remotely.
The receivers are listening to the network for incomming messages, and the sender sends a restart/shutdown message to the selected receiver.

The following part of code is running on a receiver:

import os

self.currentOS = calling a function to determine the current OS

if self.currentOS == "Win":
    os.system("shutdown -r -f -t 1")
elif self.currentOS == "Lin":
    os.system("shutdown -r now")

I have 2 virtual machines acting as receivers, one on Windows and the other on Linux.

When i send a restart message to the Windows receiver, the machine restarts.
When i send a restart message to the Linux receiver, it asks for password

Incoming:EXEC_OP_RESTART
[sudo] password for jwalker: 

What do i have to change to overcome this?
Is shutdown -r now the only way, or can i do this another way (more directly)?

EDIT: In this question, something called dbus was used, and it was done without a password, i am searching about dbus, as an alternative.

like image 365
kafedakias Avatar asked Nov 11 '11 03:11

kafedakias


People also ask

How do I remotely restart a computer without logging in?

At the command prompt, type shutdown -r -m \\MachineName -t -01 then hit Enter on your keyboard. The remote computer should automatically shut down or restart depending on the switches you choose.

How can I remotely shutdown someones computer?

5. Shut down machines remotely from any computer on the network by clicking the Start button in the lower-left corner of your screen, selecting "All Programs," "Accessories" and then "Command Prompt." Type "shutdown /i" (without the quotes) and press "Enter" to open the remote shutdown dialog box.

How can you power down shut down a system from Linux terminal?

You can also use the systemctl command to shut down the system. For example, type systemctl halt or systemctl poweroff to achieve similar results to the shutdown command.


2 Answers

It takes root privileges to restart a Linux machine. Some desktop environments use a daemon to get around this.... but I suggest editing the sudoers file

https://help.ubuntu.com/community/Sudoers for a howto. Basically you'll want to allow the restart command - and only the restart command - to be run without a password.

Something like:

ALL ALL=(ALL) NOPASSWD: /usr/sbin/shutdown

will let any user on the machine restart it without using a password. You'll probably need to prefix the 'shutdown' in your system command with 'sudo', though it looks like it's being called automatically somehow. If this isn't secure enough, you can make a group, make your program run as that group, then allow that group to restart.

EDIT: Apparently this can be done with DBus (note: I haven't tested this):

dbus-send --system --print-reply --dest="org.freedesktop.Hal" /org/freedesktop/Hal/devices/computer org.freedesktop.Hal.Device.SystemPowerManagement.Restart int32:0

This works because dbus runs as root (or has root privs) and can therefore accept requests to restart from nonpriveleged processes and act on them. I still think the sudo way is cleaner, and so will anyone who maintains this code.

like image 194
Robert Avatar answered Oct 09 '22 14:10

Robert


Run the script with sudo python. For example, if your file were named remotesd.py, sudo python remotesd.py

like image 36
john Avatar answered Oct 09 '22 15:10

john