Image by Rob Speed on Flickr
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:
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 USERNAME@192.168.X.X:.
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/* USERNAME@192.168.X.X: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!
Trouble-shooting starting an ssh server on Bash-on-Windows
SSH Client for Android devices
Why home IP addresses start with 192.168
A guide to tmux
Getting Started with Bash-on-Windows
Also In The December 2018 Issue
Create colourful geometric patterns in SketchUp inspired by the Great Mosque of Cordoba.
Meet BOLT, Sphero’s newest robot equipped with ambient light sensors.
Learn how to use animated GIFs to create proper animation loops in Scratch.
Create an electronic obstacle course and test the steadiness of your hands as you navigate through.
Resources to help you master programming without breaking the budget.
What’s allowed in an e-mail address? Let’s break down the syntax.
Throwing your old tech in the trash is bad for the environment. Check out some safer, cleaner alternatives.
New, crafty mods that’ll push your Minecraft design skills to new levels.
Part philosophical paradox, part coding puzzle, quines are sure to tease and challenge your brain!
5 questions with the designer pushing innovation in 3D printed clothes.
Harness bash to become the ghost haunting your own computer.
Get lost in a mess of infinite lists with this kooky, lazy language.
All work and no play makes it harder for kids to learn, especially those with anxiety and learning disabilities.
Dive into the nuts & bolts of storing pictures and files on your computer.
A clever AI is creating twisted, surreal art. Math or magic?
Two doors hide a zonk, and one door hides a prize. Can probability & statistics help you pick the right one?
How can you tell if a credit card number is valid or invalid? Meet the Luhn algorithm, one of many checksums helping us keep the internet in order.
Learn about the Destination Dispatch algorithm and create your own robotic lift.
Grab your friends & disconnect from the digital world with these fun science & tech board games.
5 hands-on books & activities to hone your cipher skills.
Interesting stories about computer science, software programming, and technology for December 2018.
Links from the bottom of all the December 2018 articles, collected in one place for you to print, share, or bookmark.