Аутентификация открытого ключа ssh с помощью powershell

I’m trying to create a SSH connection between my personal laptop running with Windows 11 and Powershell 7.3.9 and
my VPS running on Debian 12 (bookworm). I’m not using Putty on purpose.

I first wanted to get clear of all existing ssh keys (I’m beginning with SSH and made several tries) and the tried to
create and copy a new one, everything seemed to work as planned but when I try to log in, it keeps asking me my password,
and if I authorize only public key connection I get permission denied

sshd_config on my server is basically the same as the one provided by
k4yt3x


Erase old keys

  • server : delete keys in ~/.ssh/authorized_keys
  • local : Remove-Item -Path $env:USERPROFILE\.ssh\* -Force

Create new key


Copy id on server

I wrote and executed this script in Powershell:

# Public key path
$publicKeyPath = "$env:USERPROFILE\.ssh\id_rsa.pub"
# Server user name, port and domain
$username = "user"
$domain = "domaine"
$port = "port"
# Read public key content
$publicKey = Get-Content $publicKeyPath
# Concatenate public key content with username
$sshContent = "$username`:$publicKey"
# Send the public key to the server and add it to authorized_keys
$sshContent | ssh -p $port "$username@$domain" "cat >> ~/.ssh/authorized_keys"

then I checked permissions on my server

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Despite all this, password is still asked on login.

OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to domaine [adresse_ip] port XXXX.
debug1: Connection established.
debug1: identity file C:\\Users\\user\\.ssh\\id_rsa type 0
debug1: identity file C:\\Users\\user\\.ssh\\id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_for_Windows_8.6
debug1: Remote protocol version 2.0, remote software version OpenSSH_9.2p1 Debian-2+deb12u1
debug1: compat_banner: match: OpenSSH_9.2p1 Debian-2+deb12u1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to domaine:port as 'user'
debug1: load_hostkeys: fopen C:\\Users\\user/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen __PROGRAMDATA__\\ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen __PROGRAMDATA__\\ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:key
debug1: load_hostkeys: fopen C:\\Users\\user/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen __PROGRAMDATA__\\ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen __PROGRAMDATA__\\ssh/ssh_known_hosts2: No such file or directory
debug1: Host '[domain]:port' is known and matches the ED25519 host key.
debug1: Found key in C:\\Users\\user/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: pubkey_prepare: ssh_get_authentication_socket: No such file or directory
debug1: Will attempt key: C:\\Users\\user\\.ssh\\id_rsa RSA SHA256:key explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],ssh-dss,ssh-rsa,rsa-sha2-256,rsa-sha2-512>
debug1: kex_input_ext_info: [email protected] (unrecognised)
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: C:\\Users\\user\\.ssh\\id_rsa RSA SHA256:key explicit
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
user@domaine: Permission denied (publickey).

I don’t understand what I’m missing.

:/>  Пропала папка мои документы windows 10

Para aquellos que usan el sistema operativo Windows, pueden usar el equivalente a chmod, que es el comando icacls. Para dar permisos de lectura, escritura y ejecucion a un usuario en un archivo o carpeta, se usa el siguiente comando:

Mostrará una lista de opciones

image

Para el ejemplo utilzare cuatro archivos para leer, escribir, ejecutar y sin acceso

image

Como se puede ver utilzando el chmod del wsl se muestra que los archivos en acceso total (F) que vendria a ser equivalente a chmod 777

Utilizaremos el nombre del usuario y permisos es una combinación de R, W y X, que representan los permisos de lectura, escritura y ejecución.

Utilizaremos la siguiente sintaxis:

 icacls nombre_archivo grant nombre_usuario:permisos

Donde archivo o carpeta es el archivo o carpeta que queremos dar permisos y usuario es el usuario al que le queremos dar permisos. En este caso, le estamos dando permisos de lectura, escritura y ejecución al usuario.

Pasos

Paso 1

Para eliminar permisos explicitos, se usala opcion /reset, que se usa de la siguiente manera:

 Restablece el archivo para eliminar permisos explícitosicacls .\archivo1.txt reset

Paso 2

Para dar el permiso explicito al usuario actual, se usa el comando icacls, seguido de la ruta del archivo o carpeta, seguido de la opción /grant:r, seguido del nombre del usuario actual, seguido de los permisos que queremos darle. Por ejemplo, para dar permisos de lectura, escritura y ejecución al usuario actual en el archivo archivo1.txt, se usa el siguiente comando:

 Dar permiso explícito al usuario actualicacls .\archivo1.txt grant:r 
  • F : acceso completo
  • R : acceso de solo lectura
  • W : acceso de solo escritura
  • X : acceso de solo ejecución
  • RX : lectura y ejecución del acceso
:/>  Как перезагрузить сервер windows через командную строку

Paso 3

 Deshabilita la herencia y elimina los permisos heredadosicacls .\archivo1.txt inheritancer:r
  • r : Quita todas las ACE heredadas del archivo o carpeta
  • ACE : Entradas de control de acceso

Ejecutando permisos

Lectura

image

Escritura

image

Ejecucion

image

Quitar permisos

Para quitar permisos de lectura, escritura y ejecución a un usuario en un archivo o carpeta, se usa el siguiente comandos:

icacls nombre_archivo reset
icacls nombre_archivo inheritancer:r

image