Open Source Musings The thoughts, ideas, and opinions of an open source guy

A quick introduction to ssh and scp

A padlocked document

Sometimes … well, actually more than sometimes, I need to log into the servers that host my web sites to change or upload a file. I stopped using FTP a while back, if only because the passwords are sent in plaintext. Isn’t that reason enough.

While I often use FileZilla for secure connections, it’s often overkill when I want to change or transfer one file. When I want to do that, I turn to the command line and use SSH and SCP. Both offer me a level of security that FTP doesn’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 that will help get you going. You can get more information from a number of sources, including this one.

Using SSH

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 will return /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 host my web sites partly because they offered shell access using SSH. Obviously, you’ll also need an account on those servers.

Let’s assume both conditions are met. How do you log into a server using SSH? Type the following command:

ssh username@servername

Where username is your user name on the server, and servername is the name of the server. For example:

ssh [email protected]

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 found in a file, named knownhosts_ 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’ll be prompted for your password. Once you type that, you’re logging into the server. You can move around and do things using the standard Linux commands. That’s where a basic knowledge of the command line comes in. If you’re interested, I’ll be publishing an ebook covering the basics of the command line (and a bit more) in the coming months.

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 use the alias goSN. That saves a lot of keystrokes. You’ll have to type your password, though.

Using SCP

When I SSH into a server, I mostly edit files using vi or vim. 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.

Obviously, 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. If you want to transfer a file to a server, type the following command:

scp filename username@servername.com

So, if I want to upload a file named pricelist.php to a server named myServer.com, I’d type:

scp pricelist.php [email protected]

What if you want to copy a file to a particular directory on the server? You’d use this command:

scp filename username@servername.com:directory

When I want to upload a file named menu.inc to the directory includes on the my server, I’d type:

scp pricelist.php [email protected]:includes

Uploading files is one thing. What happens if you want to download a file to your computer? Just type:

scp filename username@servername.com directory on your computer

For example:

scp [email protected]:SCP_Tutorial.pdf /home/scott/Temp

That command downloads the file SCPTutorial.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 SCPTutorial.pdf_ is in a folder named Documents on the remote server. To get it, I just type:

scp [email protected]: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.

Thoughts? Let's start a conversation on Twitter.

Did you enjoy this post or find it useful? Then please consider supporting this blog with a micropayment via Liberapay. Thanks!