Connect via SSH key to your Linux Server

You have a Linux server and want to connect to it? The common practice is to use a ssh key pair for that. Basically you store a public and private key locally on your machine, upload the public key to your server and than start connecting to it with your private key. No further applications are needed, you just have to use a bash terminal.

Create an SSH key

Start by creating a key pair inside of your .ssh folder.
Navigatie to ~/.ssh/:

cd ~/.ssh

and create the key pair with the command:

ssh-keygen

name it, by default the name is id_rsa. If you haven‘t already created keys before you can just hit enter, otherwise enter a new name.
Next you will be asked for a passphrase, which encrypts your key and ensures additional security. If you leave it empty the key won’t be encrypted.
Retype your passphrase or leave it empty and hit enter again.

Your key pair got created.

You will see now two new files. One with the ending .pub and another with just your chosen name. The .pub file is your public key. You have to upload it on your server. The default file is your private key, keep it secret!

Upload the public key

Next, upload your public key onto your server. The easiest way is to use the ssh-copy-id command. It‘s installed by default. Keep in mind that uploading your public key with ssh-copy-id is only possible if your server has activated password login. Go and copy your server IP address, you will need it and type:

ssh-copy-id <username>@<server_ip>

In case you have created your ssh key with a different name, you have to specify the path by using the option -i

ssh-copy-id -i ~/<path_to_your_public_key> <username>@<server_ip>

Establish a connection

Now you should be able to login via ssh into your server, test it with the command

ssh <username>@<server>

Leave a Comment

Your email address will not be published.