How to shutdown Linux remotely using web page

This is tutorial how to shutdown Linux remotely using web page. This way you can shutdown your computer from every device where you have web browser in use example with Android phone.

WARNING: Everyone who can access your computers IP address can shutdown your computer!

I’m using Ubuntu 16.04 and Python 2.7.12.

Enable shutdown command without password

First you need to enable shutdown without password

$ sudoedit /etc/sudoers.d/shutdown

Add this line to your file, but replace username text with your own username

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

Now you can run shutdown command without need to type password

$ sudo shutdown -P now

Shutdown script

Create python script

$ sudo mkdir -p /var/www/cgi-bin
$ sudoedit /var/www/cgi-bin/shutdown.py

Add these lines to your Python script file

#! /usr/bin/python
import subprocess

print "Content-Type: text/plain;charset=utf-8"
print

print "Shutdown"

cmdCommand = "sudo shutdown -P now"
process = subprocess.Popen(cmdCommand.split(), stdout=subprocess.PIPE)

Give execution permissions to the python file

$ sudo chmod a+x /var/www/cgi-bin/shutdown.py

You can test that your script works by running it

$ python /var/www/cgi-bin/shutdown.py

Computer should shutdown and next you can try shutdown your computer using web browser.

Shutdown computer using web page

Start server on port 8000

$ cd /var/www/
$ python -m CGIHTTPServer 8000

Server will start on 0.0.0.0 so every IP addresses that are configured in to your computer should work example localhost or 127.0.0.1. Open your web browser and go to an IP address x.x.x.x:8000/cgi-bin/shutdown.py (x.x.x.x is your IP address)

In browser should read “Shutdown” and computer should now shutdown.

Schedule server to start on boot

Next schedule Python server to start every time your computer boots up. Go to crontab

$ crontab -e

In the end of the file add this line

@reboot python -m CGIHTTPServer 8000

Now every time your computer starts you can use this address to shutdown your computer

x.x.x.x:8000/cgi-bin/shutdown.py

You can use this shutdown web page on every device that can access this web page.

You can example use your Android device and save bookmark to your home screen. Then you have shortcut button on your Android device when ever you want to shutdown your computer.