Heute möchten wir euch zeigen wie man die Authentifizierung gegen ein OpenSSH-Server mittels SSH-Key realisiert.
Unser Schlüsselpärchen erzeugen wir mit dem Befehl ssh-keygen und übergeben die Option -t rsa und -b 4096. Die Option -t definiert welcher Algorithmus und -b welche Schlüssellänge genutzt werden soll:
root@icinga2-node1a:~# ssh-keygen -t rsa -b 4096 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: -cutted output- root@icinga2-node1a The key's randomart image is: +---[RSA 4096]----+ | .... .=. . | | o o.+oo.+. . | | . + o+oE. . | | . o.o + | | S. + . | | . .. | | . | | | | | +-----------------+ root@icinga2-node1a:~#
Wir sehen in der Ausgabe den Punkt “Enter passphrase”, dort sollte man unbedingt ein Kennwort vergeben. Da es sonst dem Szenario gleich kommen würde, dass wir unsere EC-Karte ohne PIN betreiben!
Alternativ kann man die Schlüssel auch mit einer Elliptische-Kurven-Kryptografie erzeugen. Hierzu bietet sich die Elliptische-Kurve ed25519 an, die ab OpenSSH 6.5 unterstützt wird:
root@icinga2-node1a:~# ssh-keygen -t ed25519 Generating public/private ed25519 key pair. Enter file in which to save the key (/root/.ssh/id_ed25519): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_ed25519. Your public key has been saved in /root/.ssh/id_ed25519.pub. The key fingerprint is: -cutted output- root@icinga2-node1a The key's randomart image is: +--[ED25519 256]--+ | . | | o | | . E | | o + o | | = S o . | | + + + . | | +.* o | | ..+* | |.o.o oo | +-----------------+ root@icinga2-node1a:~#
Sofern man den Pfad während der Generierung nicht verändert hat findet man die Zertifikate unter:
root@icinga2-node1a:~# ls -la ~/.ssh/ total 28 drwx------ 2 root root 4096 Jan 26 12:26 . drwx------ 5 root root 4096 Jan 25 17:04 .. -rw------- 1 root root 464 Jan 26 12:09 id_ed25519 -rw-r--r-- 1 root root 101 Jan 26 12:09 id_ed25519.pub -rw------- 1 root root 3326 Jan 26 12:26 id_rsa -rw-r--r-- 1 root root 745 Jan 26 12:26 id_rsa.pub -rw-r--r-- 1 root root 666 Jan 25 10:29 known_hosts root@icinga2-node1a:~#
Für die Verteilung unseres Öffentlichen Schlüssels wird bereits einen Befehl mitgeliefert:
root@icinga2-node1a:~# ssh-copy-id netways@icinga2-node1b The authenticity of host 'icinga2-node1b (192.168.56.12)' can't be established. ECDSA key fingerprint is -cutted output- Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys netways@icinga2-node1b's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'netways@icinga2-node1b'" and check to make sure that only the key(s) you wanted were added. root@icinga2-node1a:~#
Direkt im Anschluss kann man testen ob der Verbindungsaufbau mittels SSH-Key funktioniert:
root@icinga2-node1a:~# ssh netways@icinga2-node1b Enter passphrase for key '/root/.ssh/id_rsa': The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. You have new mail. Last login: Wed Jan 24 18:22:11 2018 from 192.168.56.1 netways@icinga2-node1b:~$
Im letzten Schritt müssen wir unserem OpenSSH-Server die Anmeldung per Benutzername und Kennwort abgewöhnen. Hierzu editieren wir folgenden Optionen in unserer OpenSSH-Server Konfiguration:
icinga2-node3b:~# vi /etc/ssh/sshd_config --- #PasswordAuthentication yes +++ PasswordAuthentication no --- #AuthorizedKeysFile %h/.ssh/authorized_keys +++ AuthorizedKeysFile %h/.ssh/authorized_keys --- PermitRootLogin no +++ PermitRootLogin without-password icinga2-node3b:~# service sshd restart
Sobald der Neustart des OpenSSH-Servers abgeschlossen ist, wird nur noch die Anmeldung per SSH-Key akzeptiert. Wenn man sich jetzt regulär verbindet, wird zwar zunächst eine Verbindung aufgebaut, jedoch bei der Übergabe des Benutzernamens die Verbindung mit der Meldung “publickey” beendet.
0 Comments