SSH Passwordless Login: How to Setup and Disable it in Linux?

Ein guter Serveradministrator verwendet mehrere starke Passwörter, die schwer zu merken sind; Es ist jedoch schwierig und unpraktisch, jedes Mal jedes Mal einzugeben, wenn Sie auf entfernte Server zugreifen. Um dieses Problem zu lösen, werden wir in diesem Tutorial darüber sprechen passwortlos SSH und wie Sie es unter Linux konfigurieren können!
Was ist passwortloses SSH?
Passwortloses SSH ermöglicht es Ihnen, einen Client zu exportieren SSH öffentlichen Schlüssel an den Remote-Server, damit sich der Client anmelden kann, ohne ein Passwort zu benötigen. Der Weg, dies zu erreichen, ist die Verwendung eines asymmetrischen Schlüsselpaars.
Das Authentifizierungsverfahren funktioniert wie folgt:
Wenn der Client versucht, auf die zuzugreifen SSH server, the server first checks if the client’s public key is authorized. If authorized, the authentication process continues. If it is not authorized, the process ends, and you cannot access the server.
Wenn der Server den öffentlichen Schlüssel des Clients autorisiert, verschlüsselt der Server eine Nachricht mit dem öffentlichen Schlüssel des Clients. Sobald der Server die Nachricht verschlüsselt hat, sendet er sie an den Client.
The client receives the message from the server. Once the message is received, the client tries to decrypt this message with the private key. If the client decrypts the message using the private key, the server will detect it, and the connection to the SSH server is established.
If the client cannot decrypt the message sent by the server, the connection process to the server is aborted.

Warum die passwortlose SSH-Anmeldung verwenden
Passwortloses SSH bietet mehrere Vorteile gegenüber herkömmlichen passwortbasierten Anmeldemethoden. Einige der wichtigsten Vorteile, die die Verwendung von passwortlosem SSH lohnenswert machen, sind:
Bequeme und sichere Anmeldung
The fact that you will not have to remember and enter passwords to connect to an SSH server makes the authentication process convenient. Moreover, it is based on public-private key cryptography, eliminating the risk of passwords getting stolen through der Mann in der Mitte und andere Phishing-Attacken.
mit dem Autoate Backup Tasks
Using Rsync and other utilities, you can automate the task of making a weekly backup of the content generated in an office in the United States and saving it in an office located in China. The process would be encrypted through SSH and completely automatic because you won’t have to enter any password.
Mounten Sie ein Remote-Dateisystem
Passwordless SSH is useful if you need to mount a remote file system with SSHFS. By accessing the SSHFS server without passwords, we can make the SSHFS server mount itself when we start our client.
In addition, multiple git servers use SSH public key authentication. Therefore, the pair of asymmetric keys you create to connect to an SSH server can have other uses, such as authenticating while connecting to our Git server.
Richten Sie die passwortlose SSH-Anmeldung unter Linux ein
# 1. Stellen Sie sicher, dass SSH-Server und -Client installiert sind
Als erstes müssen Sie sicherstellen, dass auf dem System, das als Server fungieren soll, ein SSH-Server installiert ist. Dazu müssen wir ein Terminal öffnen und den folgenden Befehl eingeben:
sudo apt-get install openssh-server
Wenn in unserem Betriebssystem kein neues Paket installiert wird, ist auf dem Computer, der als Server fungieren wird, der SSH-Server bereits installiert und läuft. Stellen Sie in ähnlicher Weise sicher, dass das System, das als Client fungiert, über die erforderlichen Pakete verfügt, um eine Verbindung zum SSH-Server herzustellen. Öffnen Sie dazu ein Terminal und geben Sie den folgenden Befehl ein:
sudo apt-get install openssh-client
Heutzutage haben die meisten Linux-Distributionen standardmäßig einen SSH-Client und -Server installiert.
# 2. Create the Asymmetric Key Pairs
Once you are sure that the SSH server and the client have the necessary packages, you can generate the asymmetric keys to access our SSH server without entering any password.
Dazu müssen Sie auf dem Computer, der als Client fungieren soll, ein Terminal öffnen und den folgenden Befehl eingeben:
ssh-keygen -b 4096 -t rsa
Die Bedeutung der einzelnen Befehlsparameter ist wie folgt:
ssh-keygen
: it is the command that generates the key pair.
-b 4096
: you are indicating that the asymmetric key to be generated has a size of 4096 bits. Other sizes that you can choose, for example, are 1024 or 2048.
-t rsa
: Indicates that the algorithm used to generate the key pair must be RSA. Other algorithms that we can use are DSA, ECDSA, RSA1, and ED25519.
Unmittelbar nach Ausführung des Befehls werden Sie nach dem Ort gefragt, an dem Sie die Schlüssel speichern möchten, und nach dem Namen, den Sie ihnen geben möchten. Drücken Sie einfach die Eingabetaste. Auf diese Weise werden die Schlüssel, die am Standardspeicherort gespeichert werden, d. /home/ Benutzer /.ssh/ und wird den Standardnamen id_rsa haben.
Next, you will be asked if you want to enter a password to encrypt the private key. Since we want to connect to the server without entering any password, press the Enter key without entering any password.
Finally, you are asked to re-enter the password. Since we have not entered any password, press the Enter key again.

After performing these steps, the asymmetric keys will be created in the location ~/.ssh.
# 3. Kopieren Sie den öffentlichen SSH-Schlüssel auf den Remote-Server
Es gibt zwei Arten von SSH-Schlüssel for organizing passwordless access via SSH: a non-secret public key (a public key) and a secret key (a private Taste), id_rsa is your private Schlüssel und id_rsa.pub ist Ihr öffentlicher Schlüssel.
You need to copy the public key to the remote server, either using the command ssh-copy-id or manually.
Die typische Syntax für den SSH-Befehl zum Kopieren des öffentlichen Schlüssels lautet wie folgt.
ssh-copy-id username@remote.host.name
Das folgende Beispiel ist vielleicht klarer:
ssh-copy-id root@192.168.1.6

An alternative option is to log in to the remote server and create a text file in the directory ~/.ssh, for example, by running the following command.
nano ~/.ssh/authorized_keys
Kopieren Sie den Inhalt Ihrer Datei ~ / .ssh / id_rsa.pub auf Ihrem lokalen Rechner in diese Datei, speichern Sie die Datei und beenden Sie den Texteditor.

# 4. Testen des passwortlosen Zugriffs über SSH
Um zu testen, ob das passwortlose SSH einfach versucht, auf den SSH-Server zuzugreifen:
$ ssh remote_username@server_ip_address
If all went well, you would be logged in immediately without any password.
Disable SSH Passwordless Login in Linux
Zu disable the SSH Passwordless login open the SSH configuration file using your preferred text editor. On most distributions, the configuration file is located at /etc/ssh/sshd_config.
Ortate the line that reads Leere Passwörter zulassen ja und Veränderung PermitEmptyPasswords ja zu PermitEmptyPasswords Nr.
Speichern Sie die Änderungen in der Konfigurationsdatei und beenden Sie den Texteditor.
Starten Sie SSH neu, um die Änderungen zu übernehmen:
sudo systemctl restart sshd
Zusammenfassung
Passwords, no matter how complicated, are less secure, and when you have many servers or have to login frequently, the process becomes inefficient. Passwordless ssh authentication using asymmetric keys is not just more convenient but also more secure and protects you against phishing attacks.
Sie können auch erkunden Symmetrische Verschlüsselung.