What are SSH keys:

SSH key is a more secure alternative to passwords for logging into your machine.

With SSH you will have two keys, one .pub key, and a private key and the idea is you will store the .pub key in your server and the private key in your local machine, after doing this you can just ssh into your server without the need of the password. it is more secure since it is key-based authentication.

 

To generate SSH Keys:

ssh-keygen

This will ask you where you wish to save your keys, it should be in the .ssh directory, the default is usually a good choice, but you can specify a path if you wish by giving it a path when you are prompted to do so.

This will generate two keys in the directory you specified.

1.key

2.key.pub

 

Installing the public key:

In order to be able to log into your server using SSH, first, you need to copy your .pub key into your server.

To do this:

  1. log in at the user to your server using the current password method
  2. mkdir .ssh all of your keys should be in this directory
  3. touch .ssh/authorized_keys create a file called authorized_keys to store a list of all pub keys
  4. copy your pub key to this file, note you can have multiple keys for multiple devices, each key will have its own line
  5. edit the file and dir permission:
  6. chmod 700 .ssh
  7. chmod 644 .ssh/authorized_keys

Now if you have done everything correctly you should be able to log into your server using ssh.

e.g let's say we have a user called "student" and our private key which we generated above using ssh-keygen method is stored in ~/.ssh/private_key

~ is a short cut for the home directory or C/user/username/ in windows.

ssh student@127.0.0.1 -p 2200 -i ~/.ssh/private_key

You can always log into your server using a different pair of keys, as long as the server has the pub key and your local machine/mobile has the private.

It goes without saying .pub key and the private key are pair and only work with each other, you can not use a private key generated in one occasion to a public key generated on a different occasion.

 

{% url 'home' %}