Ssh To Amazon Ec2

Ssh To Amazon Ec2

Reading time1 min
#Cloud#Security#AWS#EC2#SSH#AWSSystemsManager

Mastering Secure SSH Access to Amazon EC2: Beyond the Basics

Secure and efficient SSH access to Amazon EC2 instances is foundational for any cloud infrastructure operation, directly impacting both security posture and operational agility. Forget the old SSH routines—this guide challenges the norm by revealing lesser-known best practices and advanced configurations that elevate your EC2 instance access from standard to bulletproof.


Why Rethink Your EC2 SSH Access?

By default, many engineers rely on simple steps: generate an SSH key pair, open port 22 in a security group, and connect. While this works, it leaves room for improvement in security and efficiency:

  • Port 22 is a constant target for automated brute force attacks.
  • Using the same key pair endlessly increases risk if keys are compromised.
  • Managing multiple users becomes cumbersome quickly.
  • Direct internet access to instances isn’t always desirable.

Let’s explore how you can go beyond these basics to strengthen your SSH setup.


Step 1: Use EC2 Instance Connect

Amazon's EC2 Instance Connect offers a secure method to connect without directly managing SSH keys on each instance:

  • It uses IAM policies to grant temporary, short-lived SSH access.
  • Eliminates the need to store private keys on client machines.
  • Works out-of-the-box for Amazon Linux 2 and Ubuntu instances.

How to Enable & Use EC2 Instance Connect:

  1. Ensure your instance has the correct IAM role attached:
    For example:

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "ec2-instance-connect:SendSSHPublicKey"
          ],
          "Resource": [
            "arn:aws:ec2:region:account-id:instance/instance-id"
          ]
        }
      ]
    }
    
  2. Verify security group rules:
    Allow inbound traffic on port 22 but only from trusted IPs or, better yet, configure time-limited firewall rules or VPN access.

  3. Connect using CLI command:

    aws ec2-instance-connect send-ssh-public-key \
      --region us-east-1 \
      --instance-id i-0123456789abcdef0 \
      --availability-zone us-east-1a \
      --instance-os-user ec2-user \
      --ssh-public-key file://my-public-key.pub
    
  4. Then ssh normally:

    ssh -i my-private-key.pem ec2-user@ec2-instance-ip
    

With Instance Connect, you don’t have to permanently upload your public key; it only stays valid for 60 seconds.


Step 2: Harden Security Groups - Avoid Opening Port 22 Everywhere

The classic mistake is leaving port 22 open to the world (0.0.0.0/0). Attackers scan these aggressively.

Best Practices:

  • Restrict inbound SSH traffic to trusted IP ranges or VPN CIDR blocks.
  • For dynamic IPs, consider creating an automation script that updates security groups when your IP changes.

Example using AWS CLI to restrict port 22:

aws ec2 authorize-security-group-ingress \
    --group-id sg-0123456789abcdef0 \
    --protocol tcp \
    --port 22 \
    --cidr YOUR_HOME_IP/32

Step 3: Use Jump Hosts (Bastion Hosts)

Instead of direct internet-facing SSH:

  • Deploy a hardened bastion host in a public subnet.
  • Lock down this bastion with multi-factor authentication (MFA) and limited user access.
  • Your actual application instances sit in private subnets with no inbound internet access.

Newer AWS offerings allow using AWS Systems Manager Session Manager as an alternative to jump hosts — removing even the need for bastions.


Step 4: Leverage AWS Systems Manager Session Manager

This lets you connect securely over HTTPS without opening any inbound ports or using SSH keys:

aws ssm start-session --target instance-id

No key management headaches! You also get detailed logging of all commands executed — great for auditing.

Make sure the instance has the proper IAM role with SSM permissions and that SSM agent is running.


Step 5: Rotate and Manage SSH Keys with AWS Secrets Manager

Instead of manually managing static keys:

  1. Store private keys or PEM files securely in AWS Secrets Manager.
  2. Automate rotation scripts that generate new key pairs regularly.
  3. Update authorized_keys on your instances automatically via user data scripts or configuration management tools (Ansible/Chef).

Here’s a simplified example of rotating keys via Lambda + Secrets Manager — ensuring no stale keys linger around.


Step 6: Configure Client-Side Best Practices

Don’t overlook local SSH hygiene:

  • Use SSH config files (~/.ssh/config) with aliases to simplify connection strings and enforce options like preferred keyfiles and connection timeouts.

Example:

Host my-ec2
    HostName ec2-instance-ip
    User ec2-user
    IdentityFile ~/.ssh/my-key.pem
    ServerAliveInterval 60
    ForwardAgent yes
  • Use ssh-agent to securely cache private keys during active sessions instead of repeatedly typing passphrases.

Bonus Tips: Advanced SSH Tunnel & Agent Forwarding Setup

For development workflows requiring multiple hops or database connections:

ssh -A -J bastion-user@bastion-host ec2-user@private-instance-ip

Here, -J sets up a jump host, and -A forwards your agent—transfer your active ssh credentials safely along the connection chain without exposing private keys on intermediate hosts.


Wrapping Up

Mastering secure SSH access isn’t just about setting up a connection — it’s about integrating identity management, minimizing attack surface, automating key rotation, and embracing modern AWS tools like EC2 Instance Connect and Systems Manager Session Manager. These best practices don’t just protect your infrastructure; they streamline your workflow for faster, safer cloud operations.

Implement one or several of these steps today—your future self (and your security team) will thank you!


Have you tried any of these advanced methods? Drop your questions or experiences below—I love hearing how others secure their cloud environments!