Frustrated like I was when trying to figure out how to use the key/pair generated via EC2 to connect to your Instance? This solution was a lifesaver for me and I hope it provides some relief to you as well.

With Powershell, run the following commands:

# Set the path of your .pem file to a variable
$path = ".\test.pem"

# Reset to remove explicit permissions
icacls.exe $path /reset

# Give current user explicit read-permission
icacls.exe $path /GRANT:R "$($env:USERNAME):(R)"

# Disable inheritance and remove inherited permissions
icacls.exe $path /inheritance:r
#Use the following to get public key of pem file on local machine
ssh-keygen -y -f test.pem

On the Linux Server, use the root account or sudo-privileged account:

#Create the user in Linux with:
adduser username

#To Grant Root or Sudo Privileges (Optional)
#For Ubuntu or Debian:
usermod -a G sudo username
#For CentOS or Redhat:
usermod -a -G wheel username
# In user's home directory (using the newuser's account):

mkdir .ssh
chmod 700 .ssh
touch .ssh/authorized_keys
chmod 600 .ssh/authorized_keys
Paste the public key from Powershell into user's authorized_keys file on Linux server

Using the root account, you will need to add the username into the AllowUsers line in sshd_config

vi /etc/ssh/sshd_config
AllowUsers newuser

#Restart sshd service after the change
sudo systemctl restart sshd
Credit to: https://www.youtube.com/watch?v=gpQOiln8Q4k&list=WL&index=24&ab_channel=Amarindaz

By Tony

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.