Although you can maintain your code using local Git repositories, at some point you will want to either clone from, or push to, a remote Git repository. Although it is possible to use HTTP authentication, it can be both more secure, and certainly more convenient to use an SSH and a key to manage your authentication.
This recipe will show you how to generate an RSA SSH key that is suitable for use with Git, and also to authenticate against Linux servers.
Let's create an SSH key for Git:
Create a new RSA key using the
ssh-keygen
command:ssh-keygen -t rsa -C "My SSH Key"
Replace
"My SSH Key
" with an identifying text such asMy laptop
. This helps when you are managing multiple keys.You will be prompted for a
passphrase
after running the preceding command; it's highly recommended that you create one to ensure the security of your key; otherwise, if you lose your private key, any scallywag who finds it can use it to access your systems. You can alleviate the tedium of typing in the password using anssh-agent
to store the details for the duration of a session.When you use the
ssh-keygen
command, you will see that it produces an output similar to the following screenshot:
By default, the ssh-keygen
command will create a new set of files in your home directory, within a hidden directory named .ssh
. This includes both your public and private keys. Remember, never, ever share your private key. If you suspect that it has been shared at all, delete it and then revoke it from any system it was previously used with and create a new key pair.