GIT – I’ve been using for quite some time now but never really understood how public key authentication works. I decided to search the web and try it out on different testing machines. So, if you want to find out how it works as well, here’s how it’s done.

First, you’ll need to log in to your machine, and create a necessary directory, change it’s permissions and go into it.

$ mkdir .ssh/; chmod 700 .ssh/; cd .ssh/

After that, you want to create a file called ‘authorized_keys’. This file contains the public key(s) that are allowed to log-in with your username.

$ touch authorized_keys; chmod 600 authorized_keys

Right now, it should look like this:

$ ls -lh

-rw——- 1 kevin kevin  394 Jun  3 13:00 authorized_keys

It’s time to generate the public/private rsa key pair:

$ ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/home/kevin/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/kevin/.ssh/id_rsa.
Your public key has been saved in /home/kevin/.ssh/

Your .ssh/ directory should look like this right now:

ls -lh

-rw——- 1 kevin kevin 394 Jun 3 13:00 authorized_keys
-rw——- 1 kevin kevin 1.8K Jun 3 14:16 id_rsa
-rw-r–r– 1 kevin kevin 394 Jun 3 14:16

Add the key to the authorized_key file

cat >> authorized_keys

Next, the private key (id_rsa) file to your local machine by using WinSCP.

Open PuTTYgen, click ‘load’ and search for the id_rsa file you just downloaded from your Linux machine. Enter your passphrase and hit OK. Now, click ‘Save private key’ and select a good destination to store your private key file.
At last, open PuTTy, enter the IP address or hostname of your Linux machine and go to Connection -> SSH -> Auth.

At the bottom you will find ‘Private key file for authentication:’. Hit browse, and select the key file we just saved with PuTTYgen. Hit open, enter your username, your passphrase and ta-da.


Print Friendly, PDF & Email



Bài viết liên quan