Jak nastavit SSH přihlášení bez hesla v AlmaLinux

Zabezpečená skořápka, populárně známý jako SSH, je zabezpečený síťový protokol, který uživatelům umožňuje bezpečné připojení ke vzdáleným hostitelům, jako jsou servery. Je založen na architektuře klient-server a používá dvě hlavní metody ověřování – heslo a ssh-key ověření páru.

SSH-klíč párové ověřování využívá použití klíčů SSH, což jsou kryptografické klíče používané k ověření a zabezpečení komunikace mezi klientem a serverem. Ověřování párů klíčů SSH je výhodné před ověřením hesla, protože poskytuje bezpečnější ověření, které není náchylné k útokům brutální síly.

V tomto tutoriálu ilustrujeme, jak můžete nastavit klíče SSH AlmaLinux.

Vytvoření páru klíčů RSA SSH v AlmaLinuxu

Pro zahájení show vytvoříme RSA pár klíčů, který obsahuje veřejný a soukromý klíč. Tyto klíče demystifikujeme později v průvodci. Chcete-li vytvořit pár klíčů, spusťte příkaz :

$ ssh-keygenOR $ ssh-keygen -t rsa

Výše uvedené příkazy vytvářejí a 2048-bit pár klíčů RSA, který je považován za dostatečně dobrý, aby nabídl slušné šifrování pro bezpečnou komunikaci. Můžete však vytvořit a 4096-bit pár klíčů, který je robustnější a nabízí lepší šifrování. Chcete-li to provést, jednoduše předejte -b flag. This is exactly what we are going to do.

$ ssh-keygen -b 4096

Hned po stisknutí ENTER, you will be asked to provide the path in which the keys will be stored. By default, this is the ~/.ssh directory. Unless required to change it to a different path, just go with the default directory by pressing ENTER.

Create SSH Key Pair in AlmaLinux
Vytvořte párový klíč SSH v AlmaLinuxu

Thereafter, you will be required to provide a passphrase or a password. While optional, this adds an extra layer of protection when authenticating.

However, this is limiting when you want to configure passwordless ssh-key authentication to a remote host. If this is your goal, then simply press ‘ENTER’ to skip providing the keyphrase.

Create Passphrase in AlmaLinux
Create Passphrase in AlmaLinux

Here is the entire output of the command.

SSH Key Pair Summary
SSH Key Pair Summary

At this point, your keys should be stored in the ~/.ssh directory which is a hidden directory in your home directory. Just to confirm this, run the command:

$ ls -la ~/.ssh
Confirm SSH Directory
Confirm SSH Directory

A few points to note:

  • The id_rsa is the private key. As the name suggests this should be kept extremely confidential and should never be divulged or shared. An attacker can easily compromise your remote host once they get a hold of the private key.
  • The id_rsa.pub is the public key, which can be shared without any problem. You can save it to any remote host that you want to connect to.

Copy SSH Public Key to Remote Linux Server

The next step is to copy or transfer the public key to the remote server or host. You can do this manually, but the ssh-copy-id command easily allows you to do this.

The ssh-copy-id command takes the following syntax:

$ ssh-copy-id user@remote-host-ip-address

In our setup, we have a remote host with IP 172.105.135.246 and a configured remote user called jack.

To copy the public SSH key, we will run the command:

$ ssh-copy-id jack@172.105.135.246

If this is the first time connecting to the host, you will get the output shown below. To proceed with the authentication, type ‘yes’ and hit ENTER to proceed.

Once you provide the password and press ‘ENTER‘ the public key is placed in the authorized_file file in the ~/.ssh directory on the remote host.

Copy SSH Key to Remote Linux Host
Copy SSH Key to Remote Linux Host

On your local system, the known_hosts file is created in the ~/.ssh directory. The file contains the SSH fingerprints for remote hosts that you have connected to.

$ ls -la ~/.ssh
Confirm SSH Host File
Confirm SSH Host File

You can view it as follows.

$ cat ~/.ssh/known_hosts
View SSH Host File
View SSH Host File

SSH Passwordless Login to Remote Linux

With the public key now saved on the remote host, we can now login to the remote host without SSH password authentication. To test this, we will try logging in normally to the remote host.

$ ssh jack@172.105.135.248
SSH Passswordless Linux Login
SSH Passwordless Linux Login

From the output, you can see that we straight away dropped to the remote system’s shell. This confirms that we have successfully configured SSH Passwordless authentication.

Now confirm that the public key is saved in the authorized_keys file on the remote host.

$ ls -la ~/.ssh/
Check SSH Authorized Keys
Check SSH Authorized Keys

To view the file, use the cat command as follows.

$ cat ~/.ssh/authorized_keys 
View SSH Authorized Keys
Zobrazit autorizované klíče SSH

Disable SSH Password Authentication

We are not yet done, the password authentication is still enabled and this can potentially subject the remote server or host to brute-force attacks.

To eliminate this attack vector, it is highly advised to disable password authentication. This ensures that login is only possible through an SSH key pair. To achieve this, open the sshd_config soubor, který je hlavním konfiguračním souborem SSH.

$ sudo vim /etc/ssh/sshd_config

Locate the Ověřování heslem směrnice. Pokud je okomentováno, odkomentujte jej a nastavte na ‚Ne‘.

PasswordAuthentication no

Save the changes and exit the file.

Then restart SSH to apply the change made.

$ sudo systemctl restart sshd

This successfully disables password authentication and only users with the private SSH key can log in.

At this point, SSH password authentication has been disabled on the remote server and the only possible way of accessing the remote server is through veřejný klíč ověření.

We have managed to set up SSH keys on Almalinux a dokonce pokračoval v konfiguraci bezheslo SSH autentizace pomocí SSH-key pár. Dále jsme zakázali ověřování heslem, abychom odvrátili útoky brutální síly.

Source: https://www.linuxshelltips.com/ssh-passwordless-login-almalinux/