In the last lesson/lab we setup password authentication on our EC2 Instance. Click here to go to that lesson. This process workd not only for EC2 insance but for any Linux remote instance.
In this lab, we will setup password-less and keyless ssh so we can type something like
ssh user@11.22.33.44
from our local machine and we will be logged into the remote linux machine without providing password or keys.
if you're working on macOS or linux locally you can use your default shell/terminal. If you're on windows I recommend you download git bash.
https://git-scm.com/downloads
Once you download it, open it and all of us should be on the same page.
Steps ( You need to have password authentication enabled on EC2 instances to follow these steps properly. Click here to view the steps if you haven't done that )
- Navigate to the .ssh directory and list it's content .
cd ~/.ssh
this is just to note the no of contents. We will list the directory again later.
- ssh into the ec2 instance normally or with a password. Replace the example IP with your Instance IP
ssh ec2-user@11.22.33.44
- Navigate to the ssh directory of the EC2 instance and list the direcory to see the contents
cd ~/.ssh
ls
- Optional Remove the contents of that direcotry. A new file will be added later.
rm *
ls
the diretory should be empty now.
- Exit out of the remote computer
exit
6) On your local machine Use ssh-keygen utility to generate a key pair
```console
ssh-keygen
-
press enter when your're asked for name, password etc. This will select the default options.
-
Navigate to the .ssh folder in local machine to view the keys generated
cd ~/.ssh
You can see that some files have been added. These are the private and public keys(key-pair) that we generated with ssh-keygen earlier
- Copy the public key to the remote machine. Enter the password when prompted
ssh-copy-id ec2-user@32.33.34.34
- Now ssh-into the remote EC2 without password or keys
ssh ec2-user@32.33.34.34
congratulations!! you just setup password-less key-less ssh
- Navigate to the .ssh folder inside EC2 and list its contents
cd ~/.ssh
ls
-
you should see an authorized_keys file
-
View the contents of that file
cat authorized_keys
- the content of the public key we generated locally is added to this file
- On your local machine open/create the file
vim ~/.ssh/config
- Paste this config and edit the IP to the IP of your EC2 instance IP and change the location and name of your .pem file
Host ec2
HostName 18.206.86.235
User ec2-user
IdentityFile ~/cloudform-key.pem
- ssh into the instance using the Hostname
ssh ec2
You should be logged in to your ec2 instance.
Easy right?
That's it for this lesson. See you in the next one!