Sunday, 18 November 2012

Generating SSH Public-Private Key

The pair ssh public-private key is a very common way to identify a specific computer, rather using the MAC address, which could be changed by software.

As the their name figure, the public key is a key which anyone could have and use, and the private key is a key that only the owner should have. They are related such way, that a  message cipher by the public key only could be decipher by the private key, and vice versa. Check the wiki page for more information.

One of the possible use of the public-private key is in the git repository: Each repository contains a set of public keys. When anyone tries to commit (push) code to the repository, the repository will ask the computer to decipher a message ciphered by public keys. Only if the computer could decipher correctly anyone of the ciphered messages, which means it has its public key in the set of allowed public keys, the code will be committed (pushed).


Check if they already exist

By default, the generated keys will be stored in the .ssh folder in your home directory. There will be two files: id_rsa and id_rsa.pub. The first one is private, which you SHOULD NEVER SHARE IT, and the second one is public, which means it should be given when anyone request you the ssh key.

Because the ssh keys are quite important, it is good to check if it already exists.
1. Go to your home directory
cd (Linux & Mac os)
cd C:/usr/USERNAME (Windows)
2. Go to the default ssh directory
cd .ssh
If you see some error telling that the directory does not exists, then the keys could be generated safely. Skip this part. Other wise, continue
3. Back up the existence keys.
mkdir MyOldKeys
mv id_rsa* MyOldKeys (Linux & Mac os)
move id_rsa* MyOldKeys (Windows)

1. Unix based systems (Linux and Mac OS)
On Unix based systems, you just need one simple command in the terminal or console to generate the ssh keys.
ssh-keygen -t rsa -C "YourMailAccount"

where -t is used to specify the type of keys to be generated and -C, some comment.

During the process, it could ask you about the passphrase. It is quite important that you remember the passphrase, because it could be used every time when the private key is used.

2. Windows
On windows, there are several ways to generate the ssh keys. The most simple one is download the msysgit client for windows, which through the git bash, the process is the same as any Unix based systems.

The another way is using the puttyGen, another good utility from Putty.
http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html 

Once downloaded, you just have to click on the "Generate" button and move the mouse on the white space to introduce some random data.

When the process finishes, you can click on "Save public key" and "Save private key" to save both keys.

Souce:
GitHub - Generating SSH Keys

No comments:

Post a Comment