How to Generate Your Own SSH Keys Using SSH-Keygen

Mihir SuratwalaMihir Suratwala
4 min read

Last Blog Review

In the last blog we understood, how the ssh works, this is important as we should have proper knowledge how the private key connects with the public key. The public key is kept where in the EC2 machine and the match happens for securely connecting the EC2 machine (Linux OS). This will be important going forward when we understand how to connect 2 EC2 machines together.

What is ssh key-gen ??

ssh key-gen is a utility tool, which helps to create and manage ssh keys which includes public and private keys for successful authentication over ssh to remote server.

How to use ssh key-gen ??

  1. This command is used to generate the key file for ssh which means it will create 2 files, public key file and private key file.

  2. So, if we write below ssh command in EC2 machine it will generate "id_rsa" (private key file) and "id_rsa.pub" (public key file)

     "ssh-keygen" 
     "ls"
    
  3. Then open the public key file using cat command & copy the data of it. Now go into "authorized_keys" file by vim and paste the data of "id_rsa.pub" you copied earlier.

     cat id_rsa.pub 
     vim authorized_keys
    
  4. Then open the private key file "cat id_rsa" and copy the data of it and give it to user whom you want to allow the access over ssh to EC2 machine in the form of .pem file.

  5. So, now the person to whom you have given the data of private key file "id_rsa" can connect to EC2 machine from his/her local machine because the public key file data (id_rsa.pub) is already present in the "authorized_keys" file. In this way we can generate the public and private key file ourself as well and give it to the person whom we wish to allow the access to our machine.

But in what situation the ssh keys generated by ssh keygen are helpful, when we already have the .pem file which we get while creating EC2 machine ??

  1. Accessing Git Repositories -

    • Scenario: We are working with Git repositories hosted on platforms like GitHub need to securely push or pull code without using HTTPS (and thus entering a password).

    • SSH Keygen use: You configure your SSH key with your Git service, allowing for secure, password-less interactions with your Git repository. This is particularly important in automated scripts or when accessing private repositories. Once the work is done, you can just delete the public key generated by ssh keygen of the private key shared over git repo, to stop the access of the git repo to your machine.

  2. Multiple Keys for Multiple Server’s/User’s -

    • Scenario: We are working with multiple user’s in the organization or multiple platforms and if we want maintain security between those platform. Then we can’t give all the .pem file which we generate while creating EC2 machine.

    • SSH Keygen use: You configure your SSH key and create multiple private and public keys. Once you create those, then give the private key to the required platforms and copy the multiple public keys together with each public starting from a new line. This allows multiple user’s to access the EC2 machine. Once the work is done then the public key can be deleting stopping the access of other user’s

  3. Flexibility in Key Algorithms

    • EC2 Key Pair: When we create a key pair in EC2, AWS generates an RSA key pair by default, though you can choose other types of keys, like ED25519, by using the AWS CLI or console. However, this is limited to what AWS offers.

    • ssh-keygen: With ssh-keygen, we have full control over the key algorithm. We can generate RSA, ED25519, ECDSA, or other key types, with specific lengths and levels of security based on our needs.

Conclusion →

In this blog we understood what is ssh keygen, how it’s used, and what are the advantage of using ssh keygen in practical situation. As you can’t give the private .pem file key generated while creating EC2 machine maintaining security. Instead you can create your own ssh keys give the private key to required user’s and then just delete the public key of it later on in the EC2 machine, which stops their access to EC2 machine.

Image credit → https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.keycdn.com%2Fsupport%2Fcreate-ssh-key&psig=AOvVaw1d5cyYDSZgsRH1S0zVr5V_&ust=1741560471693000&source=images&cd=vfe&opi=89978449&ved=0CBQQjRxqFwoTCNjZ7vip-4sDFQAAAAAdAAAAABAE

💡
That’s a wrap for today’s post! I hope this has given you some valuable insights. Be sure to explore more articles on our blog for further tips and advice. See you in the next post!
0
Subscribe to my newsletter

Read articles from Mihir Suratwala directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mihir Suratwala
Mihir Suratwala

Hi, How are you !! Hope you doing good.... I got introduced to Cloud initially. As I went ahead learning what is cloud and how it works, then got to know a field which is DevOps that makes Cloud model more effective. So, as I started working & got good experience on AWS. I have been learning the DevOps tool and technologies on how to use it with the Cloud, which will give me good understanding on how Cloud and DevOps go hand in hand to deploy my applications.