How to connect to an EC2 instance over SSH

This article is a simple guide detailing the prerequisites and step-by-step procedures for connecting to EC2 instances via SSH. It covers Unix-based systems and Windows users, including the conversion of private key files for PuTTY.

Introduction

SSH (Secure Shell) is a popular, secure protocol for remote access to servers and other network devices. When you launch an EC2 instance, you can connect to it using SSH to manage the server, install and update software, configure settings, and perform other administrative tasks.

  • Prerequisites
  • Connecting over Unix-based systems
  • Connecting over Windows

Prerequisites

Before you can connect to an EC2 instance over SSH, you need the following:

  • Running EC2 instance and accessible over the network.
  • The EC2 instance’s IP address or public DNS.
  • The appropriate security group configured to allow SSH access (port 22) from your IP address or network.
  • The private key file (.pem) corresponding to the key pair used when launching the instance.

Remember to keep your private key file secure and not share it with anyone. It is the key to accessing your EC2 instance.

If you encounter any issues while connecting, double-check that your instance is running, that the security group allows SSH access from your IP address, or that you are using the correct private key file.

Connecting over Unix-based systems

To connect to your EC2 instance via SSH at Unix-based systems like Linux, macOS, or others, follow these steps:

Locate the private key file

Find the private key file (.pem) you downloaded when you created the EC2 instance.

Make sure you have the correct permissions to use the private key file. On Linux or macOS, you can set the permissions using the command:

chmod 400 /path/to/your/private-key.pem

Connect to the EC2 instance using SSH

Open a terminal or command prompt and use the `ssh` command to connect to your EC2 instance:

ssh -i /path/to/your/private-key.pem ec2-user@<public-dns-or-ip>
  • Replace /path/to/your/private-key.pem with the real path to your private key file.
  • Replace <public-dns-or-ip> with the public DNS or IP address of your EC2 instance.
  • If you’re using a different username than ec2-user (e.g., for Ubuntu instances, it’s usually ubuntu), modify the command accordingly.

Here’s an example of the SSH command:

 ssh -i ~/Downloads/my-key-pair.pem ec2-user@ec2-1-25-67-980.us-west-2.compute.amazonaws.com

Accept the server’s host key

If it’s your first time connecting to the instance, the server may prompt you to accept its host key. Type yes and press Enter to continue.

Connection to EC2 instance

Success! You should now have successfully connected to your EC2 instance via SSH.

Connecting over Windows

If you’re using Windows and don’t have the ssh command available, you can use an SSH client like PuTTY:

  • First, convert your private key file (.pem) to a format compatible with PuTTY (.ppk) using PuTTYgen. If you do not know how, just check the section below.
  • Open PuTTY and enter the public IPv4 DNS or IP address of your EC2 instance in the “Host Name (or IP address)” field. Keep “Connection type” as “SSH”.
  • From the “Connection” category in left tree navigation, select “SSH -> Auth” and click on “Credentials”.
  • For “Private key file for authentication” click on “Browse” and select the private key file (.ppk).
  • Click on “Open” to initiate the SSH connection. At “Putty Security Alert”, click “Yes” to accept the server’s host key.

Generating a .ppk file from a .pem file

To convert your private key file (.pem) to a .ppk file for use with PuTTY, follow these steps:

  • Open PuTTY Key Generator (PuTTYgen) and click on “Load” to select your private key file (.pem).
  • Click on “Save private key” to convert the key to PuTTY’s .ppk format and save it to a location of your choice.
  • You can now connect the converted .ppk file with PuTTY to your EC2 instance.

Conclusion

This article has shown how to connect to an Amazon EC2 instance over SSH. Following the steps outlined above, you can securely access your EC2 instance and manage it remotely using SSH.

Be sure to store your private key file securely and avoid sharing it with others, as it serves as the gateway to accessing your Amazon EC2 instance.

If you face any connection issues, verify that your instance is operational, that your security group permits SSH access from your IP address, and that you employ the correct private key file.

Did you find connecting to your EC2 instance over SSH easy? Do you have tips or another way to connect to an EC2 instance? Let us know in the comments below. We’d love to hear your ideas and experiences.

Thanks for reading!

Like this article? Please share it with your friends and help us grow. Happy coding! 😊

This entry was posted in Tutorials and tagged , , , , , , , , , , , . Bookmark the permalink.

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.