uwierzytelnianie ssh za pomocą klucza prywatnego

uwierzytelnianie ssh za pomocą klucza prywatnego
LitwinWileński
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 763
0

Nie udaje mi się uwierzytelnić po ssh za pomocą klucza prywatnego.

Na 1 komputerze (serwer) wsl Ubuntu na windows 10 zrobiłem tak:

  1. Zainstalowałem open-ssh.
  2. Zmieniłem konfigurację w /etc/ssh/sshd_config odkomentowując:
Kopiuj
PubkeyAuthentication yes
AuthorizedKeysFile	~/.ssh/authorized_keys

całość pliku (z usuniętymi komentarzami):

Kopiuj
Include /etc/ssh/sshd_config.d/*.conf

PubkeyAuthentication yes
AuthorizedKeysFile	~/.ssh/authorized_keys
KbdInteractiveAuthentication no
UsePAM yes

X11Forwarding yes
PrintMotd no
AcceptEnv LANG LC_*
Subsystem	sftp	/usr/lib/openssh/sftp-server

Na 2 komputerze (klient) windows 10 zrobiłem tak:

  1. Wygenerowałem klucz prywatny i publiczny ssh-keygen

Na 1 komputerze (serwer):

  1. Przekopiowałem klucz publiczny id_rsa.pub z klienta na serwer do ~/.ssh/authorized_keys
  2. Zrestartowałem ssh sudo service ssh restart

Na 2 komputerze (klient):

  1. ssh -i id_rsa <moj uzytkownik>@192.xxx.xx.xx mimo że podaję mu klucz prywatny to i tak żąda hasła (po haśle wchodzi). Jak dodatkowo w konfiguracji sshd dodałem PasswordAuthentication no to przy próbuje logowania z kluczem prywatnym dostaję PERMISSION DENIED.

logi serwera:
???
Nie widzę logów na serwerze, powinny być w /var/log/auth.log?

logi klienta:

Kopiuj
OpenSSH_for_Windows_8.1p1, LibreSSL 3.0.2
debug1: Connecting to 192.xx.xxx.xx [192.xx.xxx.xx] port 22.
debug1: Connection established.
debug1: identity file C:\\Users\\myuser\\.ssh\\id_rsa type 0
debug1: identity file C:\\Users\\myuser\\.ssh\\id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_8.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.9p1 Ubuntu-3ubuntu0.10
debug1: match: OpenSSH_8.9p1 Ubuntu-3ubuntu0.10 pat OpenSSH* compat 0x04000000
debug1: Authenticating to 192.xx.xxx.xx:22 as 'myuser'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:/cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
debug1: Host '192.xx.xxx.xx' is known and matches the ECDSA host key.
debug1: Found key in C:\\Users\\myuser/.ssh/known_hosts:8
debug1: rekey out after 13421778 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 13421778 blocks
debug1: pubkey_prepare: ssh_get_authentication_socket: No such file or directory
debug1: Will attempt key: C:\\Users\\myuser\\.ssh\\id_rsa RSA SHA256:xxx/xxxxx explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,sk-ssh-ed25519@openssh.com,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com,webauthn-sk-ecdsa-sha2-nistp256@openssh.com>
debug1: kex_input_ext_info: publickey-hostbound@openssh.com (unrecognised)
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: C:\\Users\\pt\\.ssh\\id_rsa RSA SHA256:xxxxxxxxxxxxxxxxx explicit
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: password
debug1: read_passphrase: can't open /dev/tty: No such file or directory

Nie widzi klucza publicznego? debug1: pubkey_prepare: ssh_get_authentication_socket: No such file or directory ?

Jakieś pomysły?

4w0rX4t4X
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 351
1

Czasem serwer wymaga aby uprawnienia do katalogu .ssh i pliku authorized_keys były ustawione na:

Kopiuj
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
LitwinWileński
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 763
0
Kopiuj
 sudo /usr/sbin/sshd -d
debug1: sshd version OpenSSH_8.9, OpenSSL 3.0.2 15 Mar 2022
debug1: private host key #0: ssh-rsa SHA256:xxx
debug1: private host key #1: ecdsa-sha2-nistp256 SHA256:/xxx
debug1: private host key #2: ssh-ed25519 SHA256:xxx
debug1: rexec_argv[0]='/usr/sbin/sshd'
debug1: rexec_argv[1]='-d'
debug1: Set /proc/self/oom_score_adj from 0 to -1000
debug1: Bind to port 22 on 0.0.0.0.
Server listening on 0.0.0.0 port 22.
debug1: Bind to port 22 on ::.
Server listening on :: port 22.
debug1: Server will not fork when running in debugging mode.
debug1: rexec start in 5 out 5 newsock 5 pipe -1 sock 8
debug1: sshd version OpenSSH_8.9, OpenSSL 3.0.2 15 Mar 2022
debug1: private host key #0: ssh-rsa SHA256:xxx
debug1: private host key #1: ecdsa-sha2-nistp256 SHA256:/xxx
debug1: private host key #2: ssh-ed25519 SHA256:xxx
debug1: inetd sockets after dupping: 3, 3
sys_get_rdomain: cannot determine VRF for fd=3 : Protocol not available
Connection from 192.xxx.xx.xx port 62598 on 192.xxx.xx.xx port 22
debug1: Local version string SSH-2.0-OpenSSH_8.9p1 Ubuntu-3ubuntu0.10
debug1: Remote protocol version 2.0, remote software version OpenSSH_for_Windows_8.6
debug1: compat_banner: match: OpenSSH_for_Windows_8.6 pat OpenSSH* compat 0x04000000
debug1: permanently_set_uid: 108/xxx [preauth]
debug1: ssh_sandbox_child: prctl(PR_SET_SECCOMP): Invalid argument [preauth]
debug1: list_hostkey_types: rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519 [preauth]
debug1: SSH2_MSG_KEXINIT sent [preauth]
debug1: SSH2_MSG_KEXINIT received [preauth]
debug1: kex: algorithm: curve25519-sha256 [preauth]
debug1: kex: host key algorithm: ssh-ed25519 [preauth]
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none [preauth]
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none [preauth]
debug1: expecting SSH2_MSG_KEX_ECDH_INIT [preauth]
debug1: SSH2_MSG_KEX_ECDH_INIT received [preauth]
debug1: rekey out after 134217728 blocks [preauth]
debug1: SSH2_MSG_NEWKEYS sent [preauth]
debug1: Sending SSH2_MSG_EXT_INFO [preauth]
debug1: expecting SSH2_MSG_NEWKEYS [preauth]
debug1: SSH2_MSG_NEWKEYS received [preauth]
debug1: rekey in after 134217728 blocks [preauth]
debug1: KEX done [preauth]
debug1: userauth-request for user myuser service ssh-connection method none [preauth]
debug1: attempt 0 failures 0 [preauth]
sys_get_rdomain: cannot determine VRF for fd=3 : Protocol not available
debug1: PAM: initializing for "myuser"
debug1: PAM: setting PAM_RHOST to "192.xxx.xx.xx"
debug1: PAM: setting PAM_TTY to "ssh"
debug1: userauth-request for user myuser service ssh-connection method publickey [preauth]
debug1: attempt 1 failures 0 [preauth]
debug1: userauth_pubkey: publickey test pkalg rsa-sha2-512 pkblob RSA SHA256:xxx [preauth]
debug1: temporarily_use_uid: 1000/1000 (e=0/0)
debug1: trying public key file /root/.ssh/authorized_keys
debug1: Could not open authorized keys '/root/.ssh/authorized_keys': Permission denied
debug1: restore_uid: 0/0
Failed publickey for myuser from 192.xxx.xx.xx port 62598 ssh2: RSA SHA256:k8QdMvd/vnd4GF4r2iLxERMr177aIKqDSdZBifp9YUM

Czemu roota chce czytać a nie myuser?
Could not open authorized keys '/root/.ssh/authorized_keys': Permission denied

LitwinWileński
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 763
0

wrzuciłem id_rsa.pub do home/myuser i zamieniłem

AuthorizedKeysFile ~/.ssh/authorized_keys

na /home/myuser/id_rsa.pub

teraz błąd wygląda tak:

Permissions for ...id_rsa are too open

tak jak tu: https://superuser.com/questions/1296024/windows-ssh-permissions-for-private-key-are-too-open

4w0rX4t4X
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 351
1
LitwinWileński napisał(a):

teraz błąd wygląda tak:

Permissions for ...id_rsa are too open

tak jak tu: https://superuser.com/questions/1296024/windows-ssh-permissions-for-private-key-are-too-open

Mam to samo:-)

Kopiuj
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "priv.key": bad permissions
LitwinWileński
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 763
1

zadziałało, musiałem na nowo wygenerować klucz

podsumowując problem leży w konfiguracji:

AuthorizedKeysFile ~/.ssh/authorized_keys
(nie wiem co tu ma być)

PaulGilbert
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 1027
0

Ja jeśli dobrze pamiętam, to pod windowsem kiedyś jak korzystałem, musiałem zmienić uprawnienia pliku na tylko do odczytu (400), żeby działało.

WR
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 2
1

Kiedyś myślałem, że o SSH wiem wszystko, ale ostatnio dowiedziałem się, że za jego pomocą można nie tylko zorganizować bezpieczne połączenie ze zdalnym serwerem, ale także np. przekierować przez niego porty. Idę czytać książki o SSH :)

4w0rX4t4X
  • Rejestracja: dni
  • Ostatnio: dni
  • Postów: 351
0
WestRiser napisał(a):

Kiedyś myślałem, że o SSH wiem wszystko, ale ostatnio dowiedziałem się, że za jego pomocą można nie tylko zorganizować bezpieczne połączenie ze zdalnym serwerem, ale także np. przekierować przez niego porty. Idę czytać książki o SSH :)

W wielu przypadkach możesz go użyć zamiast VPN. Mając dowolne konto shell możesz w domu postawić serwer nie mając globalnego IP. Tunele SSH to potęga.

superdurszlak
  • Rejestracja: dni
  • Ostatnio: dni
  • Lokalizacja: Kraków
  • Postów: 2002
0
WestRiser napisał(a):

Kiedyś myślałem, że o SSH wiem wszystko, ale ostatnio dowiedziałem się, że za jego pomocą można nie tylko zorganizować bezpieczne połączenie ze zdalnym serwerem, ale także np. przekierować przez niego porty. Idę czytać książki o SSH :)

Różne dziwne rzeczy można robić po SSH, przekierowywanie portów to małe piwo przy tym, że możesz zrobić X11 forwarding i uruchomić aplikację na zdalnym serwerze, ale mieć okno programu wyrenderowane po stronie klienta ;) jak pierwszy raz na to natrafiłem, to nawet nie byłem świadom, czego używam i jakim cudem otwieram emacsa na zdalnej maszynie, ale okno otwiera mi się na pulpicie.

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.