How to access your EC2 instance via SSH only from specific IP-Addresses

In this article, we will learn how to create a Security Group, that enables SSH traffic through port 22 from a Managed Prefix List. The Managed Prefix List allows us to conveniently manage your public IP-addresses that can connect to our EC2 instance.
Table of Content
- Create Managed Prefix List
- Create Security Group
- Attach Security Group to EC2 instance
- Access your EC2 instance via SSH
- Conclusion
1. Create Managed-Prefixlist
- Go to the AWS service
VPCand selectManaged prefix lists. - Click the button
Create prefix list Prefix list name: IP-Whitelist for SSH accessMax entries: 10Address-family: IPv4- Under
Prefix list entriesclick Add new entry - Click Create prefix list
2. Create Security Group
- Go to the AWS service
VPCand selectSecurity groups. - Click Create security group
Security group name: SSHDescription: Allows SSH access from a managed prefix listVPC:- Under
Inbound rulesclick Add rule Type: SSHSource: Custom- Scroll down to Prefix lists and select your created newly created IP-Whitelist for SSH access prefix list.
- Click Create security group
3. Attach Security Group to EC2 instance
- Go to the AWS service
EC2and select yourEC2 instance - Go to
Actions->Securityand clickChange security groups - Under Associated security groups select your newly created Security Group
SSH - Click Add security group
- Click Save
Now you should be able to connect via SSH to your EC2 instance. Of course you also need a valid Key pair assigned to your EC2 instance and have the corresponding private key on your local machine or client.
4. Access your EC2 instance via SSH
- Go to the AWS service
EC2and select yourEC2 instance. - Make sure under
Details->Key pair assigned at launchyou have aKey pairassigned and you have the correspondingPrivate Key(usually a .cer file) saved on your local machine. - On your local machine open a new
terminaland navigate to yourPrivate Keycer file. - Connect to your
EC2 instancewith following commandReplace the ip-address 10.20.30.40 with your Public ip-address of yourssh -i privateKey.cer ubuntu@10.20.30.40EC2 instanceand ubuntu with your user.
5. Conclusion
In this article we learned how to create a Managed prefix list to simplify the process of adding and modifying IP addresses that are permitted to establish an SSH connection to our EC2 instance. We have also covered the steps involved in creating a Security Group and configuring an Inbound rule specifically designed to allow SSH access via Port 22 from our newly created Managed prefix list. By assigning this Security group to our EC2 instance, we have successfully established SSH connectivity, provided that we have a valid Key pair associated with our EC2 instance and have saved the corresponding Private Key on our local machine or client.




