Featured image of post How to SSH using private key on Windows

How to SSH using private key on Windows

This simple command allows you to SSH to a server from Windows Powershell using a private key (pem) file.

This simple command allows you to SSH to a server from Windows Powershell using a private key pem file:

1
ssh -i keyfile.pem username@server.com

You may see the following error upon trying the SSH command from Windows Powershell:

1
2
3
4
5
6
7
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions for '.\\ElastiCourse.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 ".\\ElastiCourse.pem": bad permissions

On Linux, this is fixed with the command chmod 600 ElastiCoure.pem on the private key file, however Windows does not have an equivalent method. Instead you can utilize the icacls.exe tool to fix permissions for the private key as follows (change directory in the path variable to match the key location if in a different directory):

1
2
3
4
$path = ".\ElastiCourse.pem"
icacls.exe $path /reset
icacls.exe $path /GRANT:R "$($env:USERNAME):(R)"
icacls.exe $path /inheritance:r

Now you should be able to SSH to your server from Powershell using pem key file.

Licensed under CC BY-NC-SA 4.0
Last updated on Aug 12, 2024
Built with Hugo
Theme Stack designed by Jimmy