Troubleshooting a SSH connection failing on Windows OS

2024-10-06 2 min read Troubleshooting Barny Baron

Journey: 📊 Community Builder 📊

Subject matter: 💡 Troubleshooting 💡

Task: Troubleshooting a SSH connection failing on Windows OS

While working on a build utilising both Terraform and Ansible, linked here, I found I was unable to establish my SSH session within my VSCode platform. The following steps are how I solved the issue.

Troubleshooting

Problem observed:

When attempting to ssh to my new Ubuntu EC2 Instance, I encountered this error:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: UNPROTECTED PRIVATE KEY FILE! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Permissions for ‘ansible-ssh-key.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 “ansible-ssh-key.pem”: bad permissions ubuntu@ec2-54-162-75-71.compute-1.amazonaws.com: Permission denied (publickey).

Click to expand:

If using Linix, I could have run a command: ‘chmod 400 ansible-ssh-key.pem’ to amend the file, however as I am using VSCode on Windows OS, I needed an alternative solution.

Investigations:

After some searching online, I found the following page here.

Fix required:

The following steps are how I changed the file to ensure it met the conditions needed:

  1. Open PowerShell as Administrator
  2. Change directory to your .pem file
  3. Run commands:
  • icacls.exe <key_name>.pem /reset
  • icacls.exe <key_name>.pem /grant:r “$($env:username):(r)”
  • icacls.exe <key_name>.pem /inheritance:r

Click to expand:

Then attempt the connection: ssh -i <key_pair>.pem < user>@< hostname >

Connection success:

Click to expand:

If this post helps just one other person, it has served its purpose well!

Cloud