Windows OpenSSH server asks for a password for an admin user: administrators_authorized_keys
On Windows, sshd reads keys for Administrators members from C:\ProgramData\ssh\administrators_authorized_keys, not the profile .ssh folder. Set its ACL.
You put your public key in C:\Users\user1\.ssh\authorized_keys on a Windows machine running the OpenSSH server, but the server still asks for a password or replies Permission denied (publickey). If that account is in the local Administrators group, this is expected: sshd does not read that file for administrators.
Why the key is ignored
The default sshd_config on Windows (in C:\ProgramData\ssh\) ends with:
Match Group administrators
AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys
So for every member of Administrators, sshd reads one shared file, C:\ProgramData\ssh\administrators_authorized_keys, instead of the file in the user's profile. Accounts that are not administrators keep using .ssh\authorized_keys in their profile.
Fix
Put the public key, one line per key, in C:\ProgramData\ssh\administrators_authorized_keys. Then set the file's permissions from an elevated PowerShell prompt, as Microsoft's guide shows:
icacls.exe "C:\ProgramData\ssh\administrators_authorized_keys" /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F"
Microsoft's guide says the file should only have entries for SYSTEM, which needs full control, and Administrators. The command above removes inherited entries and sets exactly those two. If another user or group can change the file, sshd refuses to use it. On Windows in another language, the group name differs; use the SID with an asterisk instead, /grant "*S-1-5-32-544:F".
A new key in the file works on the next login. No restart is needed for that.
Other choices
- Use the profile file for admins too. Comment out the two
Match Group administratorslines insshd_config, then restart the service withRestart-Service sshd. sshd then reads.ssh\authorized_keysin each profile, as it does for other users. - Use a non-admin account for SSH if the work does not need admin rights.
See what sshd says
By default the Windows OpenSSH server logs to Event Viewer, under the OpenSSH logs. A file with the wrong permissions shows up there as Bad permissions. Try removing permissions for user: followed by the account to remove, then Authentication refused. For a log file instead, set SyslogFacility LOCAL0 (and, for more detail, LogLevel DEBUG3) in sshd_config and restart the service; logs then go to C:\ProgramData\ssh\logs.
Related
- Authorized keys and permissions
- PuTTY "Server refused our key": why and how to fix it
- ssh-copy-id "ERROR: No identities found" and "All keys were skipped"
- sshd_config.d drop-in ignored: the first value wins (KexAlgorithms and others)
- SSH error messages, what each one means
Sources
- Microsoft Learn: Key-based authentication in OpenSSH for Windows
- Microsoft Learn: OpenSSH Server configuration for Windows, AuthorizedKeysFile and SyslogFacility
- Win32-OpenSSH wiki: Logging Facilities
- Default Windows sshd_config, Match Group administrators
- Win32 OpenSSH source: w32-sshfileperm.c, the permission check
- Microsoft Learn: Restart-Service