Home Linux server How to generate an SSH key

How to generate an SSH key

by The Linux Digest Guy

Having a SSH key can be very handy to avoid having to enter your password every time you connect to a server via SSH. You can create a SSH key in your terminal in just a few seconds.

1. Open your terminal

2. Start the ssh-keygen tool

The ssh-keygen tool is included in the openssh-client package.

ssh-keygen -t rsa -b 4096

This will make a SSH key with the type RSA and bit length of 4096. This is larger than the default of 2048 bits. A larger key can be more secure. 4096 bits should be sufficient here.

3. Press enter to save in the default location

If this is not your first SSH key. You can also enter a new path to save the file.

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

4. Decide on a passphrase or no passphrase

If you enter a passphrase here. You will have to enter this phrase every time you use the key. If you don’t want a passphrase, just press enter.

5. Start using your new key!

Your identification has been saved in /myuser/.ssh/id_rsa.
 Your public key has been saved in /myuser/.ssh/id_rsa.pub.
 The key fingerprint is:
 SHA256:XbSuo8IpvoaHmwSuaWhnj7F0m0kuiB99a7ikpEq4yzA myuser@desktop
 The key's randomart image is:
 +---[RSA 4096]----+
 |            .    |
 |           . .   |
 |            o    |
 |         . o     |
 |.       S . .    |
 |o. .       .     |
 |Eo==o=..  o      |
 |B@+O%.O. . .     |
 |@+BB*@…          |
 +----[SHA256]-----+

The ssh-key tool has now saved two files in your ~/.ssh directory:

id_rsa

This is your public key. The contents of this file will be stored on the servers you need access to.

6. Put your public key where ever you need to use it

There are two ways you can copy your public key to a Linux server.

The quickest way is to use ssh-copy-id. This will automatically add the key to the remote users authorized keys:

ssh-copy-id [email protected]

An other way is to manually add the key to the servers authorized keys file.

cat  ~/.ssh/id_rsa.pub 
 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDV+mzuCEKk5qg1z0hKzKerNn8F4XD7o4PyTsCnUNWjWNtlfaY1IYw/QtYxeBh4blDuCXSyJNgf46eYiebjPCD8Vi3cpbqKxlVZKSxBnmhYSh6BVL+jlsxDgv/S0IueUp+KirIsnLC7c6q+dmbebZeuik5DwTS4i5njxBsZTK4E+y7q5Y6L7CzoKn7axdXlVnHs2e6z5mlQ0sFef0E0L9bwQzUwWP2+yQoUP5LsKRaDl6kOb6l8Dx6wNd1ycFyrBhntQyAWEIscfIR1eHU4DkhkIwqC328grBP6jjPxVhsl1cJdQPqs6GbLqUmddyCRLYVWTFHKnJ+X8Fh5DHNIcDHbCr8Mhqt1C2S9LVM9DEKnmkMmH3Ev6Fwa8HA83DJMqYBM4+UIWGtzZvP1DYqUxY7ixSvSh2OkReluHzo1u9cC8k1v6h2TOFoZUaQQNGN/aU2lMSXVm5AH240amPd9OygMiH+Kzd+JriQ5WztwXXZ9UxqRi1SZWvzU+KIHW6oYEBbZbrfsWSv0Q6Cih6UMdaUMn3XAqQMJ5O7Crgy4yjVoL+9umxo2q9AAzH+YdSgrbnNXovWP0Jpcgj0bsRX6rVC70usvqYueocxk6/X8wEiBrAKJl9yACP1PIwpYKKCSSLN+92Gr5t/9HDJpWimV67xH7ub7JL+M0TQlD7h+6S0GKQ== myuser@desktop

Now log in to your server and add the contents of the public key to the file .ssh/authorized_keys2.

nano ~/.ssh/authorized_keys2
2

Related Posts

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy