A Non-Techie's Introduction to SSH and SCP
Once upon a time, my websites originated from a couple of well-known hosting providers. During those years, I regularly needed to log into the servers that hosted my web sites to change or upload a file. For the longest time, I did that using FTP, but stopped because the passwords are sent in plaintext. Wasn't that reason enough?
While I often used FileZilla for secure connections, it was overkill when I needed to change or transfer one file. Instead, I turned to the command line and used SSH and SCP. Both offered me a level of security that FTP didn't. Why? They create an encrypted connection with a server — no plaintext is allowed.
Let's take a quick look at SSH and SCP.
Note: This post isn't a comprehensive guide to SSH and SCP. It's a quick and dirty introduction for someone with few technical skills. What you read here will help get you going. You can get more information from a number of sources, including this one.
To use SSH, you'll need two things. First, an SSH client installed on your computer. With most Linux distros, that's standard kit. You can find out whether or not you have it installed by cracking open a terminal window and typing which ssh. If SSH is installed, that command returns /usr/bin/ssh. If it doesn't return anything, you can install an SSH client using your package manager.
Second, the servers you intend to connect to need to be running an SSH server. Not every one does. I chose the companies that hosted my web sites partly because they offered shell access using SSH.
Let's assume both conditions are met. How do you log into a server using SSH? Type this command:
Where username is your user name on the server, and servername is the name of the server. For example:
When you connect to a server for the first time, you'll be asked whether or not you want to add the server to something called a list of known hosts. This list is in a file, named known_hosts in the .ssh folder in your /home directory. This list contains information that validates the identity of the server. Type yes to add the server to the list of known hosts.
Then, you're prompted for your password. Once you type that, you're logging into the server. You can move around and do things using standard Linux commands. That's where a basic knowledge of the command line comes in.
Automating the Process
Typing ssh username@servername can be a chore, especially if you log into your servers a lot. Once again, though, the command line comes to the rescue. Instead of typing a long string of commands, you can create a command line alias to save you some keystrokes.
Open your .bashrc file and add something like this to the file:
alias yourAlias='ssh username@servername'
Obviously, yourAlias should be short. For one of my servers, for example, I used the alias goSN. That saves a lot of keystrokes. You'll have to type your password, though.
When I SSH into a server, I mostly edit files using the vi or vim text editors. Sometimes, though, I need to upload or download files. SSH doesn't have get and put commands like FTP. But there is SCP. SCP is short for secure copy and it enables you to upload and download files over an encrypted connection.
You'll need SCP installed on your computer. It should be. But to check, open a terminal window and type which scp. The command should return /usr/bin/scp.
Using SCP is a lot like using SSH. To transfer a file to a server, type this command:
scp filename email@example.com
So, if I wanted to upload a file named pricelist.php to a server named myServer.com, I'd type:
scp pricelist.php scott@myServer.com
What if you need to copy a file to a particular directory on the server? You'd use this command:
scp filename firstname.lastname@example.org:directory
When I upload a file named menu.inc to the directory includes on the my server, I'd type:
scp pricelist.php scott@myServer.com:includes
Uploading files is one thing. What happens if you need to download a file to your computer? Type:
scp filename email@example.com *directory on your computer*
scp scott@myServer.com:SCP_Tutorial.pdf /home/scott/Temp
That command downloads the file SCP_Tutorial.pdf to the folder /home/scott/Temp on my computer.
Whether you're uploading or downloading a file, you can specify a directory. For example, maybe the file SCP_Tutorial.pdf is in a folder named Documents on the remote server. To get it, I'd type:
scp scott@myServer.com:Documents/SCP_Tutorial.pdf /home/scott/Temp
Something I forgot to mention: you'll be asked for your password when you connect to the server.
There you go. The basics of using SSH and SCP. It's easier than it sounds, isn't it?