The next step in securing your server is to set up public key authentication for your new user. Setting this up will increase the security of your server by requiring a private SSH key to log in.
Generate a Key Pair
If you do not already have an SSH key pair, which consists of a public and private key, you need to generate one. If you already have a key that you want to use, skip to the Copy SSH Keys to Server step.
To generate a new key pair, enter the following command at the terminal of your local machine (ie. your computer):
ssh-keygen -t rsa
Assuming your local user is called "david", you will see output that looks like the following:
ssh-keygen output Generating public/private rsa key pair. Enter file in which to save the key (/Users/david/.ssh/id_rsa):
Hit return to accept this file name and path (or enter a new name).
Next, you will be prompted for a passphrase to secure the key with. You may either enter a passphrase or leave the passphrase blank. Note: If you leave the passphrase blank, you will be able to use the private key for authentication without entering a passphrase. If you enter a passphrase, you will need both the private key and the passphrase to log in. Securing your keys with passphrases is more secure, but both methods have their uses and are more secure than basic password authentication.
This generates a private key, id_rsa, and a public key, id_rsa.pub, in the .ssh directory of the localuser's home directory. Remember that the private key should not be shared with anyone who should not have access to your servers!
Here we describe how to copy over your public ssh keys to a server on Linux and MacOS.
Use ssh-copy-id
After generating an SSH key pair, you will want to copy your public key to your new server. We will cover two easy ways to do this.
If your local machine has the ssh-copy-id script installed, you can use it to install your public key to any user that you have login credentials for.
Run the ssh-copy-id script by specifying the user and IP address of the server that you want to install the key on, like this:
ssh-copy-id -i demo demo@SERVER_IP_ADDRESS
You only need to pass the "-i demo" parameter if you are generating a second ssh key. If you only have one you can leave this empty and simply issue:
ssh-copy-id username@SERVER_IP_ADDRESS
However in our case we are assuming you have just set up a server and generated and used an ssh key for the "root" user to gain initial access. The second "demo" key being specified is for the "demo" user you just created and had given sudo permissions. So you have two keys and need to specify which one to upload with the "i" parameter.
After providing your password at the prompt, your public key will be added to the remote user's .ssh/authorized_keys file. The corresponding private key can now be used to log into the server.
Now you should:
To improve security you should only allow ssh pass-wordless (key based) authentication, especially for root:
How to remove ssh keys from server
You can add, remove and edit SSH keys within the panel under settings in the security tab. Visiting https://cloud.digitalocean.com/settings/security should yield the same effect - digitalocean.com
To remove keys added to your local system with ssh-keygen:
ssh-keygen -R [your.ip.address.here]