SSH (Secure Shell) and SFTP (Secure FTP) support a very strong security model that can be used instead of the normal username and password authentication scheme we’ve all come to know and love. It uses public key cryptography to create a different, more secure approach to authenticating your identity and your right to access a server or other resource.
In a nutshell, you will generate a public and private key pair. The public key will be placed on the server by your system administrator, giving you access. You will keep the file containing the private key in a safe place. You’ll log in by providing that private key file to your SSH- or SFTP-supporting client.
It really is that simple.
Become a Patron of Ask Leo! and go ad-free!
SSH and public keys
The easiest approach to creating a public / private key pair is to download and install PuTTY and then use the PuTTYgen tool. Once you have a key pair, keep the private key secure and give the public key to the administrator of the server to which you want to connect. Using it is as simple as specifying the location of your private key to the SSH or SFTP tool you’re using.
The private key is just that: private. You may put a password on it, but you don’t have to. Without a password, all you need is the file in order to log in. Or, to put it more clearly, all anyone needs is that file to log in as you. Obviously, if you password-protect the file, you’ll need both the file and the password to unlock it. In that case, logging in is very similar to what you do today: specify a username and a password to unlock your key file.
I’m aware of the following tools that claim to work with this technique.
- SSH Clients
- SFTP Clients
Other tools may also work. The key terminology to look for is SSH, SFTP, and Public Key Authentication.
Personally, I make heavy use of PuTTY and MountainDuck.
Generating Your Keys
In general, it’s best to create your own key. That way, you control what happens to your private key, and no one else ever has to see it.
PuTTY is a free SSH client that includes a tool for generating keys called PuTTYgen. The tool can also be downloaded separately, but why bother? Just get the whole thing. PuTTY is my recommended SSH client.
When you run PuTTYgen, you’ll get a dialog with a “Generate” button on it.
It will ask you to move the mouse around to generate randomness. (Randomness is a key component of public key cryptography). Once that’s happened enough, a key will be displayed.
Next, you should do the following.
- Specify a passphrase. Technically, this is optional, but if you omit the passphrase, then anyone who can access your private key file can log in as you. You may or may not have enough security in place for this to be an issue. If you do specify a passphrase, you’ll need to enter it when you log in, similar to a normal login.
- Press the Save Public Key button to save the public key. I recommend saving as your name “.pub” (for public). For example, I would save “leo.pub”.1
- Press the Save Private Key button to save your private key. This saves the private key in PuTTY’s own format, a .ppk file. So, “name.ppk” might be appropriate.
- I also recommend hitting the Conversions menu and then Export Openssh key and saving that to “name.key”. This format will allow you to use your private key with other applications besides PuTTY.
Once you have your keys…
Send your public key to your system administrator via email (the key itself or the “.pub” file as an attachment). It will be put in all the right places2 to allow you to log in to all the account(s) you might need to.
Keep your private key in a safe place. You’ll need it each time you want to log in. If either key is lost, you will lose access until the key generation and installation process can be repeated. If they or the computer they’re on are stolen, tell your system admin immediately.
Using Your SSH Keys to Log In
Once your keys are generated and the public key installed on the server, you need to specify the private key to your SSH client in order to log in.
There are (at least) two approaches to using Public/Private keys with PuTTY. When you fire up PuTTY without any command line options, you get its standard configuration dialog. Here you enter the name of the server to which you want to connect.
On the left is a tree view of various options. Underneath Connection, expand SSH, expand Auth, and then click on Credentials. The dialog will then include a field labelled Private key file for authentication.
Specify the location of the .ppk file you generated with PuTTYgen. When you connect, if your private key is passphrase-protected, you’ll be asked for the passphrase.
The other approach, and the one I use, is to create shortcuts for the various servers I connect to regularly and specify the location of the private key on the command line. For example:
PUTTY.EXE -i c:\somepath\leo.ppk firstname.lastname@example.org
That, as a desktop shortcut or item on a Windows menu, connects to the named server using the specified account name “leo”, and uses the private key found in “c:\somepath\leo.ppk” to authenticate. Run that and there’s no additional step; the SSH window opens and signs you in. Here’s an example of connecting to the server currently hosting askleo.com, ec23.pugetsoundsoftware.com.
Using Your SFTP Keys
Secure FTP, or SFTP, is really just using SSH technology to provide FTP-like functionality. Since it’s using SSH, the keys you’ve generated and use for your SSH authentication work with many SFTP applications as well.
PSFTP is a command-line FTP program using SSH that’s distributed with PuTTY. More importantly, it supports public key SFTP by using the “.ppk” file that you created above for PuTTY. Connecting using a public key is simply a different set of comment line options:
psftp -l username -2 -i keys.ppk remotehost
- -l username specifies your username on the remote host. Following the example above, this might be “leo”.
- -2 indicates that PSFTP should use SSH protocol version 2.
- -i keys.ppk specifies the location of your private key as created with PuTTYgen. Again, from the example: “c:\somepath\leo.ppk”.
- remotehost is the name of the remote host you’re connecting to.
Once this technique is in place, running the command as shown above will connect without further prompting (except for any password placed on the private key).
Other FTP-like programs supporting SFTP/SSH have similar approaches to specifying the private key to be used.
Subscribe to Confident Computing! Less frustration and more confidence, solutions, answers, and tips in your inbox every week.
I'll see you there!
Footnotes & References
1: Unfortunately that’s also the file extension for Microsoft Publisher. If you have Publisher installed, note you’ll get Publisher icons when viewing the file in Windows File Explorer.