Skip to content

Linux ssh-keygen command

The Linux ssh-keygen command is used to generate, manage, and convert authentication keys for the Secure Shell (SSH) protocol. It creates a public and private key pair that can be used for secure communication between systems. These keys are crucial for authenticating users and establishing secure connections over a network. The ssh-keygen command offers various options for generating different types of keys, changing key passphrases, and converting keys to other formats. By using this command, users can enhance the security of their SSH connections and protect sensitive information from unauthorized access.

ssh-keygen Syntax:

Terminal window
ssh-keygen [options] [parameters]

Options:

OptionDescription
-tSpecifies the type of key to create
-bSpecifies the number of bits in the key
-CProvides a new comment
-fSpecifies the filename of the key file
-NSets the passphrase for the key
-qQuiet mode, doesn’t display any warning messages
-yOutputs the public key based on the private key

Parameters:

ParameterDescription
keyfileOptional parameter to specify the location of the key file
emailEmail address to associate with the SSH key (for -C option)
passphrasePassphrase to protect the private key (for -N option)
publickeyFile to output the public key (for -y option)

ssh-keygen Usage:

Generate a New SSH Key Pair

Terminal window
ssh-keygen

This command generates a new SSH key pair.

Generate a New SSH Key Pair with a Specific Key Type and Size

Terminal window
ssh-keygen -t rsa -b 4096

Generates a new SSH key pair with RSA as the key type and a key length of 4096 bits.

Save the SSH Private Key to a Specific File

Terminal window
ssh-keygen -t rsa -b 2048 -f ~/.ssh/id_rsa_custom

Generates a new SSH key pair with RSA as the key type, a key length of 2048 bits and saves the private key to the file “id_rsa_custom” in the “.ssh” directory.

Generate a New SSH Key Pair with a Specific Comment

Terminal window
ssh-keygen -t rsa -b 1024 -C "my_key"

Generates a new SSH key pair with RSA as the key type, a key length of 1024 bits, and a comment “my_key”.

Common Questions on ssh-keygen Usage:

How do I generate a new SSH key pair with ssh-keygen in Linux?

To generate a new SSH key pair using the ssh-keygen command in Linux, use the following syntax:

Terminal window
ssh-keygen -t rsa -b 2048 -C "[email protected]"

How do I specify the location and file name for the SSH key pair generated by ssh-keygen in Linux?

To specify a custom location and file name for the SSH key pair generated by ssh-keygen in Linux, you can use the following command:

Terminal window
ssh-keygen -t rsa -b 2048 -C "[email protected]" -f /custom/path/your_key_name

How do I generate an SSH key pair without a passphrase using ssh-keygen in Linux?

To generate an SSH key pair without a passphrase using the ssh-keygen command in Linux, you can execute the following command:

Terminal window
ssh-keygen -t rsa -b 2048 -C "[email protected]" -N ""

How can I generate a specific type of encryption algorithm for an SSH key pair using ssh-keygen in Linux?

To generate an SSH key pair with a specific type of encryption algorithm, such as ED25519, using the ssh-keygen command in Linux, you can use the following syntax:

Terminal window
ssh-keygen -t ed25519 -C "[email protected]"

How do I specify the number of bits for an RSA key pair generated by ssh-keygen in Linux?

To specify the number of bits (e.g., 4096) for an RSA key pair generated by ssh-keygen in Linux, you can use the following command:

Terminal window
ssh-keygen -t rsa -b 4096 -C "[email protected]"

How can I generate a SSH key pair with a comment for identification using ssh-keygen in Linux?

To generate an SSH key pair with a comment for identification using the ssh-keygen command in Linux, you can execute the following command:

Terminal window
ssh-keygen -t rsa -b 2048 -C "your_comment_here"

Applications of the ssh-keygen command

  • Generating a new SSH key pair
  • Changing the type of a generated key
  • Specifying the file name of the generated key
  • Adding a passphrase to a key
  • Displaying the fingerprint of a public key
  • Converting keys between different formats
  • Extracting the public key from a private key