Wie richtet man SSH ohne Passwortabfrage ein?

Der Zugriff per SSH ist für Administratoren das Mittel der Wahl, um aus der Ferne Komponenten zu administrieren. Um nicht ständig ein Passwort für den Zugriff eingeben zu müssen, was bei einer hohen Anzahl von Servern auf Dauer lästig werden, zeige ich dir heute, wie man SSH ohne Passwortabfrage einrichtet. Das heißt, du wirst nicht ständig nach dem Passwort deines Accounts auf der Remote-Maschine gefragt.

Attention: For the english version, click here!

Vorbereitend hierzu, prüfe, ob auf der lokalen als auch auf dem Server in der Ferne SSH installiert ist. Ansonsten installiere es einfach mit, falls es ein Ubuntu-System ist oder aptitude als Paketmanager nutzt. Vorher das Repository zu updaten, ist immer eine gute Idee, wenn man ein neues Paket installieren möchte.

apt update
apt install openssh-server

SSH-Schlüssel erstellen und kopieren

Nach der Vorarbeit erstellst du einen neuen Schlüssel auf dem lokalen Computer.

ssh-keygen

Wähle dabei den Speicherort, falls dir der Vorschlag nicht zusagt und wähle ein Passwort für den Key aus. Das Ganze sieht wie folgt aus:

ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/admin/.ssh/id_rsa):  Enter passphrase (empty for no passphrase):  Enter same passphrase again:  Your identification has been saved in /home/admin/.ssh/id_rsa
Your public key has been saved in /home/admin/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:XNIxPM6MEFaOG8uWTA8wO9ldJFDtSQ+Zuj1lu9GgNb0 dennis@pop-os
The key's randomart image is:
+---[RSA 3072]----+
|    o +=++=o     |
|     B.+ +Oo     |
|    + *.+Oo= .   |
|     = Ooo* B .  |
|      B So = = . |
|     .  . + o E  |
|           . o   |
|            .    |
|                 |
+----[SHA256]-----+

Danach solltest du mittels ssh-add den neu generierten Key dem SSH-Agent hinzufügen.

ssh-add ~/.ssh/id_rsa

Folgender Output sollte dann zu sehen sein:

ssh-add ~/.ssh/id_rsa
Enter passphrase for /home/admin/.ssh/id_rsa: 
Identity added: /home/admin/.ssh/id_rsa (admin@client)

Gebe dein zuvor vergebenes Passwort ein und du kannst mittels ssh-copy-id deinen erstellten Key auf den Zielserver kopieren. Hierzu musst du deinen Nutzer auf dem Zielserver, als auch die IP angeben. In meinem Fall sieht das wie folgt aus:

ssh-copy-id dennis@192.168.0.7

Jetzt musst du einmalig dein Passwort auf dem Remote-System eingeben und der Key wird transferiert.

Wenn du auf deinem lokalen System Windows installiert ist, wirst du feststellen dass es den Befehl ssh-copy-id nicht gibt. Hier habe ich eine Artikel darüber geschrieben, wie der Key auch unter Windows auf das Zielsystem kopiert werden kann.

SSH ohne Passwortabfrage eingerichtet

Damit wäre die Arbeit erledigt. Wenn du jetzt eine SSH-Verbindung zu deinem Zielsystem aufbauen möchtest, wirst du kein Passwort eingeben müssen. Das heißt im Umkehrschluss du hast den passwordless SSH-Zugriff erfolgreich eingerichtet. Viel Spaß beim Ausprobieren!


How to set up passwordless SSH access

Accessing servers via SSH is the preferred method for administrators to remotely manage components. To avoid constantly entering a password for access, which can become cumbersome when dealing with a large number of servers, I will show you today how to set up SSH without a password prompt. This means you won’t be asked for the password of your account on the remote machine all the time.

To prepare for this, check if SSH is installed on both the local and remote server. If not, you can easily install it on an Ubuntu system or using the aptitude package manager. It’s always a good idea to update the repository before installing a new package.

apt update
apt install openssh-server

Create SSH key and copy it to the destination

After the initial setup, you will create a new key on the local computer.

ssh-keygen

Choose a location for the key if you don’t like the suggested one and select a password for the key. Here’s how it looks:

sh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/admin/.ssh/id_rsa):  Enter passphrase (empty for no passphrase):  Enter same passphrase again:  Your identification has been saved in /home/admin/.ssh/id_rsa
Your public key has been saved in /home/admin/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:XNIxPM6MEFaOG8uWTA8wO9ldJFDtSQ+Zuj1lu9GgNb0 dennis@pop-os
The key's randomart image is:
+---[RSA 3072]----+
|    o +=++=o     |
|     B.+ +Oo     |
|    + *.+Oo= .   |
|     = Ooo* B .  |
|      B So = = . |
|     .  . + o E  |
|           . o   |
|            .    |
|                 |
+----[SHA256]-----+

Afterward, you should add the newly generated key to the SSH agent using the ssh-add command.

ssh-add ~/.ssh/id_rsa

You should see the following output:

ssh-add ~/.ssh/id_rsa
Enter passphrase for /home/admin/.ssh/id_rsa: 
Identity added: /home/admin/.ssh/id_rsa (admin@client)

Enter the password you provided earlier, and you can copy your created key to the target server using ssh-copy-id. You need to specify your user on the target server as well as the IP address. In my case, it looks like this:

ssh-copy-id dennis@192.168.0.7

Now, you’ll need to enter your password on the remote system once, and the key will be transferred.

If you have Windows installed on your local system, you’ll notice that the ssh-copy-id command is not available. I have written an article about how to copy the key to the target system on Windows here.

With this, the work is done. If you want to establish an SSH connection to your target system now, you won’t have to enter a password. In other words, you have successfully set up passwordless SSH access. Enjoy experimenting!

Show comments

Join the discussion

1 reply to “Wie richtet man SSH ohne Passwortabfrage ein?”

  1. Thorsten says:

    Nice one!
    Ich persönlich kann empfehlen, den key ggf. mit weiteren Optionen generieren zu lassen. Mein Standard:
    ssh-keygen -t ed25519 -a 420 -f .ed25519 -C „, sudo on “
    Mit -t ed25519 gebe ich vor, dass ein elliptic curve key generiert wird. Das -C gibt einen Kommentar mit, der auch auf dem Zielsystem eingesehen werden kann.
    Generell sollten man für jeden Host einen eigenen key anlegen.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert