Step-by-step guide on creating a key pair, creating a new non-root user, assigning the key pair to that user, and securing SSH access on an AWS instance:
- Log in to the AWS Management Console.
- Go to the EC2 Dashboard.
- In the sidebar, click on "Key Pairs" under the "Network & Security" section.
- Click on "Create Key Pair."
- Provide a name for the key pair and select "Create." This will download the private key (.pem file) to your local machine. Ensure to keep this key secure.
- Open
PowerShell
as Administrator. - Run the following command to retrieve the public key from the private key (.pem file):
ssh-keygen -y -f "jon.smith.pem"
This generates the public key. Alternatively, you can use Puttygen to extract the public key.
- Connect to your AWS instance using SSH as the root user.
- Create a new user:
sudo adduser jonsmith
- Set an empty password for the user:
sudo passwd -d jonsmith
- Set password expiry date to Never:
sudo chage -I -1 -m 0 -M 99999 -E -1 jonsmith
- Switch to the new user:
su - jonsmith
- Create the necessary directory and file:
mkdir ~/.ssh
vi ~/.ssh/authorized_keys
- Open the
authorized_keys
file and paste the retrieved public key in a single line. - Set appropriate permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
- Open the SSH configuration file:
sudo vi /etc/ssh/sshd_config
- Ensure the following settings:
PubkeyAuthentication yes
PasswordAuthentication no
Save the file and exit the editor.
- Restart the SSH service:
sudo service ssh restart
These steps will create a new key pair, a new non-root user, assign the key pair to that user, and secure SSH access on your AWS instance by disabling password authentication and enabling public key authentication.