Turn on sshd from php

=About= This tip will show you how to make a php page that in conjunction with a shell script can turn sshd on remotely. I found myself only needing to use sshd once in a great while remotely, and I didn't want to set up a port knocking solution for something I rarely used. I include a php page you can put on your apache webspace, as well as the shell script you use to actually turn sshd on and off. I've made the defaults as easy to implement as possible.

If you load the php page in your browser, it writes a file to /tmp/start_ssh.tmp with the IP address of the person who loaded the page. The shell script checks if this file exists and then starts sshd for 5 minutes, then stops it again. This gives you a 5 minute window in which to connect to the server via ssh.

The Shell Script
For those who have iptables running on the system, an additional layer of security may be implemented as follows: iptables commands

or you can use

an iptables only version
=Putting it together=


 * Copy the php page into your Web space.
 * Copy the shell script into a file, like /usr/local/sbin/sshon.sh
 * Make the file executable only by root: chmod 700 /usr/local/sbin/sshon.sh
 * Add a cron entry for root:

Now whenever you run the php page from the browser, sshd should start for 5 minutes within 1 minute.