Have you ever wanted the power to control another computer remotely? I don’t mean hacking, I’m talking about cool command line tools like secure shell (ssh for the remainder of the article). With ssh, secure copy (scp), and other tools we’ll be able to control other computers, like Raspberry Pis, from the command line.
What you’ll need for this article is either two machines running Linux, OSX, or Bash-on-Windows, or a machine running one of those systems and a smartphone with a ssh client. I’ve used JuiceSSH on my Android phone and Termius on an iPad to do this. You’ll need the machine you want to remotely control on the same home network as the computer you’re on. If you want to use your phone running an ssh client for this you’ll need to be connected to your wifi network and not the cellular network.
To summarize the next few steps we’re going to need to:
- Get an ssh server running on the machine we want to control so that the computer is ready to be controlled over ssh
- Find out the local address of the machine we want to control
- Start ssh from our controlling machine and log in!
The first step is to install the ssh server on the device you want to connect to. On the Raspberry Pi this is really simple and can be done by opening the Raspberry Pi configuration software, which is raspi-config on the command line. You’ll find the ability to turn on the ssh server under “Interfacing options” as seen in the screenshot below:
On an OSX machine you need to enable Remote Login to start the Mac’s ssh server. For Linux, it depends on what kind of Linux you’re running. Most people are running either Ubuntu or something derived from it, like Mint. For all of these some form of the following instructions should work.
If you’re running something very different like Arch, or Gentoo, you’ll need to search for the specific instructions. It shouldn’t be too complicated either way, though! Finally, if you’re running Bash-on-Windows by default you’re running Ubuntu so those instructions should apply but there’s some possible weirdness. In the supplementary links in this article you’ll find a forum post where people discuss some problems that can happen and how to fix them.
The second thing you need to do with the machine you want to control, now that you’ve got a server running on it, is find its local network IP address. Open up a terminal on the target and type ifconfig at the command line. You should see something like:
I’ve drawn a circle around the part that you need to write down! You should look for the address returned by ifconfig that starts with 192.168.
This is the address of the computer on your home network. You couldn’t get to it from the internet this way. It’s just the address your home router has given the computer. Below I linked to an article I found explaining why all machines on home networks should have addresses that start with 192.168.
Finally, open a terminal on the device you want to ssh from and type:
ssh [email protected]
Where you should replace USERNAME with the username you’d use on the computer you’re going to control and replace the X.X with the rest of the IP address you wrote down in the previous step. If it’s working correctly you should be asked for your password to the computer. Type your password and you’re in! If you’re using a mobile app to connect instead, it will have its own way to enter all the same information.
If you’re following along and all is well, you should be logged in to the second machine! Try typing ls and hitting Enter. You should see the files and directories that exist on the other machine.
From here, you can do basically anything! You can run programs, move files, or even update and restart the machine remotely. I do this all the time for my Raspberry Pi at home: I just ssh from my cell phone and run all the updates it needs then remotely restart it.
If you’re running programs on the machine you’re controlling and you close the connection, the program may stop prematurely. There’s a really good program to help deal with that, though! It’s called tmux. You can install it on any Linux flavor by following the normal way software is installed, and on Mac you can follow this guide.
The basic instructions for tmux are that if you have a program you want to run after you disconnect you start by typing tmux. You should see something that looks like a slightly different terminal window. From here, you can run the program you want to keep using after you disconnect. Then type Ctrl-b and d. You’ll be back into the ordinary terminal window and you can type exit and Enter to quit out, knowing your code will keep running in the background. I use this to remotely start web-servers that have to be manually started after a reboot.
The final useful tool is secure copy, or scp. It’s like a mashup of ssh and the cp command, which copies files! You can use it to copy files between entirely different computers. I use it a lot to back up important files to the one computer that’s connected to a big external hard drive. Sure I could just move the hard drive, but being a programmer means never passing up an opportunity to write code instead of doing things the easy way! You can use scp to take a file and move it to the home directory of the target computer.
scp fileYouWantToCopy [email protected]:.
Just like the normal cp you can use the asterisk to say “copy all files in this directory” and use -r to also copy over all directories and subdirectories.
scp -r importantDirectory/* [email protected]:backupDirectory/.
It might look something like that with the suitable dummy names replaced.
So go ahead and give it a try! There’s a lot of fun and useful things you can do once you start running code remotely!
Learn More
Trouble-shooting starting an ssh server on Bash-on-Windows
https://superuser.com/questions/1111591/how-can-i-ssh-into-bash-on-ubuntu-on-windows-10/
SSH Client for Android devices
https://play.google.com/store/apps/details?id=com.sonelli.juicessh
Why home IP addresses start with 192.168
https://trendblog.net/ever-wondered-use-192-168-x-x-ip-addresses-home/
A guide to tmux
https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/
Getting Started with Bash-on-Windows
https://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/