Generating Public Private Rsa Key Pair Ubuntu

14.04.2020by
Generating Public Private Rsa Key Pair Ubuntu 5,8/10 2438 reviews
Main Page > Server Software > Linux > Ubuntu

This article shows how to configure a SSH connection for authentication by using the public-key method. To do this, a key pair is created at the client, the public part of the key is transferred to the server, and afterwards the server is set up for key authentication. The user can log on to the server without a login password, only the password is required to protect the private key.The operating systems used in this article are on the one hand a Ubuntu 12.10 at the client side and a Ubuntu 12.04 at the server side.This guide was also validated working with Ubuntu 16.04 as client and server.

Generating a Secure Shell (SSH) Public/Private Key Pair; Generating a Secure Shell (SSH) Public/Private Key Pair. Several tools exist to generate SSH public/private key pairs. The following sections show how to generate an SSH key pair on UNIX, UNIX-like and Windows platforms. $ ssh-keygen -b 2048 -t rsa -f filename. But its authentication mechanism, where a private local key is paired with a public remote key, is used to secure all kinds of online services, from GitHub and Launchpad to Linux running on Microsoft’s Azure cloud. Generating these keys from Linux is easy, and thanks to Ubuntu on Windows, you can follow the same process from Windows 10.

  • 1On the client
  • 2On the server
  • 3Notes for other distributions

On the client

The first configuration steps take place at the client side.

Home folder rights

By default, Ubuntu sets the user home directory permissions to 755. Nevertheless, for security reasons, check whether the permissions are set to 755 on your system and change them if necessary:

:~$ sudo chmod 755 /home/<USER>

Generate keypair

In the first step, a key pair with ssh-keygen is created at the client. If you use Ubuntu 18.04 on the server, the package openssh-server will be installed in the version 7.6.[1] Since this version, RSA bit lengths smaller than 1024 bits are no longer accepted.[2] In this example, a bit length of 4096 bits is selected for the RSA keys:

Please note: It is recommended to protect the key with a passphrase for security reasons. This means that the key is not available in plain text, but is AES-CBC encrypted:

If the private key is stolen by an attacker, he has to find out the password of the key in order to access the server with the key. If the key is available in plain text, an attacker can use this stolen key to directly access the server.

Transfer the public key to the server

To transfer the public key to the server, the first step is to use the SSH connection via password authentication yet. The ssh-copy-id tool copies the corresponding identity file to the server:

The above-mentioned procedure has created the following entry in the /home/tktest/.ssh/authorized_keyson the server:

Generating Public Private Rsa Key Pair Ubuntu

Test the key authentication

Now that the public key is transferred to the server, the connection can be tested from the client. In this case, it is important that the server does not ask for the user password, but of course the passphrase of the protected key is required!

The following dialog box appears for GUI-based systems:

Key is valid for? 4096. What keysize do you want? 1y (expires after 1 year. Pgp command line guide.

After entering the password that protected the key when it was created, you are authenticated on the system:

On the server

This paragraph shows some additional configuration steps on the server side to harden the public-key authentication.

sshd configuration

In Ubuntu, it is generally sufficient to carry out the above-mentioned procedure for public-key authentication. In some situations it makes sense to deactivate password authentication completely.

Please note: After changing the following setting, it is no longer possible to log in with a password via ssh: PasswordAuthentication no.

From the client, the connection is tested again:

In the above example, the dialog for entering the key password has been aborted. Since the log-on via password was deactivated, it was not possible to log-on to the system.

Forbid password authentication for just one user

Another way in which password authentication is not completely deactivated is to disable password authentication for specific users. This allows a user who does not have sudo privileges to log on to the server, for example. To gain root privileges, at least one additional password must be found for a user with sudo privileges. Plus, there's a way to completely exclude users from ssh:

This example:

  • Prohibits SSH access for the user test
  • Deactivates password authentication for the user tktest
  • Password authentication is retained for all other users

Notes for other distributions

For other Linux distributions, the required steps may differ slightly. We would be happy to supplement our experiences with other distributions, which you are welcome to share with us via the feedback function.

Red Hat

One reader told us that the procedure described Red Hat does not work 1:1 in Red Hat. In the home directory of the user, the write permission was set for the group. After a chmod 755 it worked to connect via ssh to the server without asking for a password.

References

  1. Package: openssh-server (1:7.6p1-4) (packages.ubuntu.com)
  2. OpenSSH 7.6 Release Notes (openssh.com)


LES v3 with three displays under Windows and Ubuntu
Retrieved from 'https://www.thomas-krenn.com/en/wikiEN/index.php?title=SSH_public_key_authentication_under_Ubuntu&oldid=3714'

This tutorial series will teach you how to use GPG in Linux terminal. I will not tell you a bunch of theory to overwhelm you. Instead, I show you quick and dirty examples to get you started, and explain the basic theory along the way.

This is part 1 of this series. At the end of this post, you should be able to generate your own public/private keypair and a revocation certificate. This certificate is used to revoke your public/private keypair when your private key is compromised or you forget the passphrase for your private key.

GPG can be used for encryption and for signing. This software is pre-installed on most Linux distributions. Currently the stable version is GPG 2.0. I’m using the modern version GPG 2.2 on Arch Linux.

Check Your GPG Version

First Let’s check out the version of GPG on your system and some interesting tidbits. Run the following command.


As you can see, I’m using GPG 2.2.8, which is the latest version. We also know that the configuration directory is ~/.gnupg, which will hold our public/private key files. The default option file is ~/.gnupg/gpg.conf and ~/.gnupg/dirmngr.conf. It also tells us what algorithms are supported.

If you look closely, you can see that the insecure hash algorithm SHA1 is still supported in version 2.2.8 SHA1 is obsolete and you don’t want to use it to generate signature.

Create Your Public/Private Key Pair and Revocation Certificate

Use gpg --full-gen-key command to generate your key pair.

It asks you what kind of key you want. Notice there’re four options. The default is to create a RSA public/private key pair and also a RSA signing key. Let’s hit Enter to select the default.

Next it asks you the key length. The default is 2048 bits long. 1024 RSA key is obsolete. The longer 4096 RSA key will not provide more security than 2048 RSA key. So hit Enter to select the default.

After that it asks you how long the key should be valid, 2 years is fine. You can always update the expiration time later on.

Now it asks you if it’s correct. Notice that the default is No. So press y then Enter to confirm it’s correct.

And now we need to provide some user identification information for the key. This is important because this information will be included in our key. It’s one way of indicating who is owner of this key. The email address is a unique identifier for a person. You can leave Comment blank.

Select Okay.

Now it asks you to enter a passphrase to protect your private key. Enter a good and long passphrase and remember it. Because if you forget this passphrase, you won’t be able to unlock you private key.

Once you enter and confirm your passphrase. GPG will generate your keys.

It will take a while for GPG to generate your keys. So you can now do other stuff.

It took about 4 minutes on my system to generate my key pair.

This first line tells us that GPG created a unique identifier for public key. This unique identifier is in hex format. When someone wants to download you public key, they can refer to you public key via your email address or this hex value.

The third line tells us that GPG created a revocation certificate and its directory.Your should never share you private key with anyone.If you private key is compromised, you can use revocateion certificate to revoke your key. That means you tell the rest of the world that the old public key shall not be used any more.I suggest that you open this revocation certificate with your text editor to see what’s inside there.

Let’s look at the last three lines. They tell us the public key is 2048 bits using RSA algorithm. The public key ID 4F0BDACC matchs the last 8 bits of key fingerprint. The key fingerprint is a hash of your public key.

It also lists our user ID information: your name and your email address. And it also indicates the subkey which is 2048 bits using RSA algorithm and the unique identifier of the subkey.

Generating Public Private Rsa Key Pair Ubuntu Download

Now you can find that there are two files created under ~/.gnupg/private-keys-v1.d/ directory. These two files are binary files with .key extension.

Export Your Public Key

Others need your public key to send encrypted message to you and only your private key can decrypt it. Use the following command to export your public key. --armor option means that the output is ASCII armored. The default is to create the binary OpenPGP format. user-id is your email address.

The exported public key is written to pubkey.asc file.

Export Your Private Key

Issue the following command to export your private key.

The exported key is written to privkey.asc file.

Protect Your Private Key and Revocation Certificate

Your private key should be kept in a safe place, like an encrypted flash drive. Treat it like your house key. Only you can have it and don’t lose it. And you must remember your passphrase, otherwise you can’t unlock your private key.

You should also protect your revocation certificate. If others have your revocation certificate, they can immediately revoke your public/private keypair and generate a fake public/priavte keypair.

Create Public Private Key Pair Ubuntu

In part 2 we will look at how to encrypt message with your public key and how to decrypt it with your private key. Take care!

[Total: 6 Average: 4.7]
Comments are closed.