In case you are using WSL on windows
The most simple answer is to just type: sudo ssh -i keyfile.pem <user>@ip
without changing the file permissions.
The reason why this happens?
Another resource
You can't modify the permissions of files on Windows's filesystem
using chmod on Bash on Ubuntu on Windows. You'll have to copy the
private key to your WSL home directory (~) and do it there.
On the other hand, sudo
should never be utilized with ssh. The reason why issuing with sudo
works is that it's now likely being executed as root, and this is not the correct way to do this and is a massive security risk, as Allowing for anything other the 600/400 permissions defeats the purpose of utilizing an SSH key, compromising the security of the key.
The best way to do that is by copying the file to $HOME/.ssh
:
cp keyfile.pem ~/.ssh
Doing sudo chmod 400 keyfile.pem
to it.
Then ssh -i keyfile.pem <user>@ip
.