How to Setup an SSH Key

An SSH key is a highly encrypted code that allows you to connect from one server to another, without having to send the password over the network. Because it is encoded, there is less of a chance that hackers will be able to snatch your password and gain unlawful access to your server.

To begin creating an SSH key you will, of course, need access to two locations running SSH. One can be your home computer, and the other should be your VPS or dedicated server. More probably, however, you will want to use this to connect two servers (i.e. a primary server and a backup server).

1. The first step is to connect to each machine using SSH. This will create a directory called ~/.ssh.


2. On server 1, enter the following:

ssh-keygen -t dsa

When it prompts you for a passphrase, enter one of your choice. It will then create the files id_dsa and in the /home/user/.ssh directory.

3. Copy the id_dsa and to the second server.

scp ~/.ssh/

4. Now tell your server which key to use:

ssh-agent sh -c 'ssh-add < /dev/null && bash'

This will prompt it to ask you for the passphrase you just created and open a new bash shell.

From now on, you should be able to type ssh from hostname1, and it will connect without asking for a password. This will allow you to perform tasks like automated backup without having to reveal your password in a script or send it remotely, since the passphrase will be stored on the local machine (hostname1) and not sent over the network.