How to secure SSH Private key on Windows 10

I'm using the new ssh client for windows 10 and when trying to connect with a private key I'm getting this error:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions for 'MyPair.pem' are too open. It is required that your
private key files are NOT accessible by others. This private key will
be ignored. Load key "MyPair.pem": bad permissions [email protected]:
Permission denied (publickey).

I know that if I was on Linux I would need to run chmod 600 to set the file permissions, but what do you use on Windows 10?


Solution 1:

Keys must only be accessible to the user they're intended for and no other account, service, or group.

  • GUI:
    [File] PropertiesSecurityAdvanced
    1. Owner: The key's user
    2. Permission Entries: Remove all except for the key's user
    3. Set key's user to Full Control

  • Cmd:
    ::# Set Key File Variable:
        Set Key="%UserProfile%\.ssh\id_rsa"
    
    ::# Remove Inheritance:
        Icacls %Key% /c /t /Inheritance:d
    
    ::# Set Ownership to Owner:
        Icacls %Key% /c /t /Grant %UserName%:F
    
    ::# Remove All Users, except for Owner:
        Icacls %Key%  /c /t /Remove Administrator BUILTIN\Administrators BUILTIN Everyone System Users
    
    ::# Verify:
        Icacls %Key%
    
    ::# Remove Variable:
        set "Key="
    
    

  • PowerShell:
    # Set Key File Variable:
      New-Variable -Name Key -Value "$env:UserProfile\.ssh\id_rsa"
    
    # Remove Inheritance:
      Icacls $Key /c /t /Inheritance:d
    
    # Set Ownership to Owner:
      Icacls $Key /c /t /Grant $env:UserName:F
    
    # Remove All Users, except for Owner:
      Icacls $Key  /c /t /Remove Administrator BUILTIN\Administrators BUILTIN Everyone System Users
    
    # Verify:
      Icacls $Key
    
    # Remove Variable:
      Remove-Variable -Name Key
    
    

  • WSL/Cygwin:
    # Set Variables:
      # Key File:
        key="/path/to/key"
    
      # User:
        user="$(echo $USER)"
    
    # Set Ownership to Owner: (assumes user's name is also user's group name)
      chown $user:$user $key
    
    # Set Access Rights
      chmod 0600 $key
    
    # Verify
      ls -l $key