Generate SSH Keys on Linux (2024)

SSH stands for Secure Shell, and as its name is saying, it is used to establish a secure connection between the client and its server. By default, every Linux based operating system supports SSH. SSH protocol is typically used for accessing, commanding, and transferring files remotely. So, in this post, you will demonstrate to generate SSH keys and use them to protect the server and precious information.

SSH Key Generation

When we generate an SSH key pair, it is generated in two steps. One is the creation of an SSH key on the client-side, and the second is copying it to the server or any remote host. A key pair consists of Private and Public key files named id_rsa and id_rsa.pub respectively in the ~/.ssh directory.

The IP address of my client system is

$ ip a

Generate SSH Keys on Linux (1)

192.168.18.130

An SSH key can be generated by running the “ssh-keygen” command in the terminal.

$ ssh-keygen

Generate SSH Keys on Linux (2)

It will ask you to enter the file name in which you want to save the private and public key, or you can go with the default selected files “id_rsa” and “id_rsa.pub” in the “.ssh” directory (/home/user/.ssh/id_rsa). Press Enter to select the default provided file.

Generate SSH Keys on Linux (3)

Next, it will ask for the Passphrase. A passphrase is actually kind of an extra security layer for securing the connection between host and client. When you log in to the host, it will ask for the passphrase again. So either enter the passphrase, or you can leave it empty and hit Enter without providing any passphrase.

Once you are done with the passphrase, the SSH key should be generated.

Generate SSH Keys on Linux (4)

You can notice in the screenshot that the generated Key is “RSA 3072”. What does that mean?

Algorithm Type and Size of SSH key

By default, the generated key’s algorithm type is RSA, and its bit size is 3072 bit. But you can change it if you want.

There are three major types of Algorithm for generating SSH keys.

RSA – Rivest Shamir Adleman. It is a key with a minimum size of 2048, and it is based on the difficulty of factoring large numbers.

DSA – Digital Signature Algorithm. This key is mostly used with 1024 size.

ECDSA – Elliptic Curves Digital Signature Algorithm. It supports 256, 384, and 521 bits.

Now, if you want to provide your desired algorithm type and bit size, you can provide the algorithm type followed by the -t phrase after the ssh-keygen command, and you can also provide the bit size along with it followed by the -b phrase. The example is as follows,

$ ssh-keygen -t rsa -b 4096

Generate SSH Keys on Linux (5)

As you can see in the screenshot, the key’s algorithm type is RSA, and the bit size is 4096. That’s great.

Copying the SSH key to the Host

You can simply copy the SSH key to the host by running the command given below in the client’s terminal.

$ ssh-copy-id username@host-ip-address

Make sure to replace the username and host-ip-address with your user name and host’s IP address. My host’s user name and IP address is

Username: linuxuser
IP Address: 192.168.18.131

You may face an error of connection refused by port 22 at this stage. In case of error, kindly visit our dedicated article (How to fix: Connection refused by port 22 Debian/Ubuntu – Linux Hint) for handling such error.

Generate SSH Keys on Linux (6)

After running the above command, it will confirm from you to continue the connection; type “yes” to continue.

Generate SSH Keys on Linux (7)

Once it is copied successfully, you are ready to log in to the server’s machine using the SSH key.

Log In to the Server

After successfully copying the SSH key to the host, we can log in to the host using the ssh command and by providing the username and IP address of the host using the following syntax.

$ ssh username@host-ip-address

Don’t forget to replace the user name and IP address with your host’s user name and IP address.

Generate SSH Keys on Linux (8)

And here you are logged in to the host’s machine now if you type the “ip a” command in the terminal.

$ ip a

Generate SSH Keys on Linux (9)

It will show the IP address of the host machine because you are on the server right now.

Wrap Up

This is how you can generate SSH keys, copy them to the host machine and access the host using the SSH keys. Thank you so much!

Generate SSH Keys on Linux (2024)

FAQs

How do I generate a SSH key in Linux? ›

To generate an SSH key on your Linux server, run the command ssh-keygen . The command can take flags if you would like to customize the type of key that is generated and the signing algorithms that are used to generate the key. This example generates a standard 2048-bit RSA key without a passphrase.

How do I manually generate SSH keys? ›

For Windows 10 & 11
  1. Press the Windows key or open up the Start Menu. Type “cmd”.
  2. Under “Best Match”, click “Command Prompt”.
  3. In the command prompt, use the ssh-keygen command: ...
  4. The system will now generate the key pair and display the key fingerprint and a randomart image. ...
  5. Open your file explorer.

How do I get my SSH key for Linux? ›

Finding SSH key locally
  1. Whether you use macOS or Linux, open your Terminal application.
  2. Run cd ~/. ssh/ in your Terminal.
  3. If the folder exists, run ls and check if a pair of key exists : id_ed25519 and id_ed25519. pub. Using id_rsa and id_rsa. pub is fine too. We are just advocating the use of ed25519.
Feb 12, 2024

How to generate SSH PEM key in Linux? ›

Generate SSH Keys in PEM Format to Connect to a Public or On-Premises sFTP Server
  1. Verify the key by opening the file in Notepad. The key must start with the following phrase. ...
  2. Use -m PEM with ssh-keygen to generate private keys in PEM format: ssh-keygen -t rsa -m PEM.

How to generate SSH key using command? ›

Generating a new SSH key
  1. Open Terminal .
  2. Paste the text below, replacing the email used in the example with your GitHub email address. ssh-keygen -t ed25519 -C "your_email@example.com" ...
  3. At the prompt, type a secure passphrase. For more information, see "Working with SSH key passphrases."

How to generate ssh key in terminal? ›

Generating SSH key pairs locally
  1. In a terminal, type the command ssh-keygen -t rsa, and press enter.
  2. When asked to enter file in which to save the key, press enter without typing in a name. ...
  3. Enter a password for the key pair, and press enter. ...
  4. Upload the public key file (the one ending in .

How to generate SSH key in Ubuntu? ›

How to Generate SSH Keys on Ubuntu
  1. Generate SSH Key Pair.
  2. Copy Public Key to Ubuntu Server. Using ssh-copy-id Script. Copy Public Key Manually. Using cat and SSH Together.
  3. Log In to the Remote Server.
  4. Set up Passwordless SSH Login (Optional)
Mar 7, 2024

How to generate SSH key in putty? ›

Generating SSH key pairs locally
  1. Download PuTTYgen (available here) and run the executable.
  2. Click on the Generate button and follow on-screen instructions.
  3. Create and confirm a Key passphrase.
  4. Save public and private keys. Saving the public key with the extension . ...
  5. Upload the public key file.

How to generate a private key? ›

Steps to Create Public/ Private Key Pair
  1. Step 1: Creating a Private Key. Type command openssl, hit enter and then use the following command to create private key: genrsa -out myprivatekey.pem. ...
  2. Step 2: Create Public Key. Type command openssl, hit enter and then use the following command to create public key:
Jan 2, 2023

How to download SSH private key in Linux? ›

To generate an SSH key pair on UNIX and UNIX-like platforms using the ssh-keygen utility:
  1. Navigate to your home directory: ...
  2. Run the ssh-keygen utility, providing as filename your choice of file name for the private key: ...
  3. Enter a passphrase for the private key, or press Enter to create a private key without a passphrase:

How to use SSH command in Linux? ›

For Linux
  1. Connect via SSH Command. Open a terminal and enter the following command: ssh @ . For example, if the IP address is 192.168. ...
  2. Enter Yes to Continue. When prompted with a warning message that says "The authenticity of host 'xxx' can't be established", enter "yes" to continue with the connection.

How to generate SSH key for Linux server Windows? ›

Generating and using an SSH key in Windows 10 or 11
  1. Start the Windows Subsystem for Linux (Windows Start button> search for the name of the installed distro, for example 'Ubuntu').
  2. Generate a public and private key with the ssh-keygen command: ssh-keygen -t ed25519. Explanation of command.

How to generate SSH key in bash? ›

If you don't have keys yet, you can generate them on your side using the Git Bash app.
  1. Open Git Bash and run the following: $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ...
  2. When you're prompted to “Enter a file in which to save the key”, press Enter.

How to generate SSH 2 RSA key on Linux? ›

On Mac/Linux:
  1. Open Terminal.
  2. Type: ssh-keygen -t rsa.
  3. Accept the default path and modify the file name if you wish ( Enter file in which to save the key (~/. ssh/id_dsa )
  4. Enter a passphrase and confirm (make sure this is SECURE, and noted somewhere for future reference)
  5. Private and public keys will be generated.

How to generate RSA key in Linux OpenSSL? ›

  1. Generate an RSA keypair with a 2048 bit private key. Execute command: "openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048" (previously “openssl genrsa -out private_key.pem 2048”) ...
  2. Extracting the public key from an RSA keypair. ...
  3. Viewing the key elements.

How to generate SSH key Linux with PuTTY? ›

To generate a new key, open PuTTYgen and click the Generate button. In the Key Generator window, check that the type of key to generate at the bottom is set to RSA. This will create an SSH-2 RSA key. The older SSH-1 was the standard's first version but is now considered obsolete.

How to generate SSH key in Linux id_ed25519? ›

You can generate keys with the 'ssh-keygen' command: $ ssh-keygen -t ed25519 Generating public/private ed25519 key pair. Enter file in which to save the key ($HOME/. ssh/id_ed25519): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in $HOME/.

Is ed25519 better than RSA? ›

Compared to traditional RSA keys, Ed25519 key generation is significantly faster. This advantage is especially valuable when dealing with systems that require frequent key generation, such as in large-scale infrastructure setups or automated processes.

Top Articles
Latest Posts
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 6046

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.