How To SSH Into AWS EC2 Instance with Mac Terminal

Are you using Amazon Web Services (AWS) and need to access your EC2 instance using SSH on your Mac? SSH (Secure Shell) is a widely used protocol that allows secure remote access to servers and enables you to execute commands on the remote machine. In this article, we will guide you through the process of SSH-ing into your EC2 instance using the Mac Terminal. So, let’s get started and learn how To SSH into AWS EC2 instance with Mac Terminal.

Introduction

SSH provides a secure way to establish a connection between your local computer and a remote server, such as an EC2 instance. By using SSH, you can remotely access your EC2 instance, manage files, execute commands, and perform various administrative tasks. In this guide, we will focus on how to SSH into your EC2 instance from your Mac Terminal.

Understanding SSH and EC2 Instances

SSH, as mentioned earlier, is a network protocol that enables secure communication and remote access between two computers. It provides a secure channel over an unsecured network, allowing you to perform various operations on a remote machine securely.

EC2 instances, on the other hand, are virtual servers offered by Amazon Web Services (AWS). They allow you to run applications and host websites in a scalable and flexible manner. EC2 instances are the backbone of many web services and applications running on AWS.

Prerequisites for SSH

Before we dive into the process of SSH-ing into your EC2 instance, there are a few prerequisites you need to take care of:

  1. Amazon Web Services (AWS) Account: You should have an active AWS account to create and manage your EC2 instances.
  2. Mac Terminal: Ensure that you have the Terminal application installed on your Mac. It comes pre-installed with macOS and can be accessed through the Applications > Utilities folder.

Generating SSH Key Pair

To establish an SSH connection with your EC2 instance, you need to generate an SSH key pair. Follow these steps to generate an SSH key pair:

Open the Terminal application on your Mac. In the Terminal window, type the following command.

ssh-keygen -t rsa -b 2048

This command will generate an RSA key pair with a key size of 2048 bits.

You will be prompted to enter a file in which to save the key pair. Press Enter to accept the default file location.

Next, you will be prompted to enter a passphrase. It is recommended to set a passphrase for added security. However, you can also leave it blank by pressing Enter. Note that leaving it blank makes the key less secure.

Once the key pair is generated, you will see a message confirming the location of the public and private keys.

Launching an EC2 Instance

To SSH into an EC2 instance, you first need to launch one. Follow these steps to launch an EC2 instance:

Go to the AWS Management Console and log in to your AWS account.

Open the EC2 service.

Click on the “Launch Instance” button.

Select an Amazon Machine Image (AMI) based on your requirements. Choose an AMI that suits your operating system and software needs.

Select an instance type, which determines the hardware of your EC2 instance.

Configure the instance details, such as the number of instances, network settings, and storage options.

Add any additional storage if required.

Configure security groups to control inbound and outbound traffic.

Review your configuration and launch the instance.

Locating Public IP Address of the Instance

Before establishing an SSH connection, you need to know the public IP address of your EC2 instance. Follow these steps to locate the public IP address:

Go to the EC2 Dashboard on the AWS Management Console.

Select the running instance for which you want to locate the public IP address.

In the instance details section at the bottom, you will find the public IP address.

Opening Mac Terminal

Now that you have the SSH key pair and the public IP address of your EC2 instance, you are ready to open the Mac Terminal and proceed with the SSH connection.

Open the Terminal application on your Mac.

In the Terminal window, type the following command to change the permissions of your private key file:

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

Replace “/path/to/your/private-key.pem” with the actual path to your private key file.

Configuring SSH Permissions

Before establishing an SSH connection, you need to configure the SSH permissions to ensure that only the key pair holder can access the EC2 instance.

In the Terminal window, type the following command to modify the SSH configuration file:

nano ~/.ssh/config 

This command opens the SSH configuration file in the Nano text editor.

Add the following lines to the file:

Host your-instance-name 
HostName your-public-ip-address 
User ec2-user 
IdentityFile /path/to/your/private-key.pem

Replace “your-instance-name” with a name of your choice, “your-public-ip-address” with the public IP address of your EC2 instance, and “/path/to/your/private-key.pem” with the actual path to your private key file.

Press Ctrl + X to exit Nano and save the changes.

How To SSH Into AWS EC2 Instance with Mac Terminal – Steps

Now it’s time to establish the SSH connection to your EC2 instance using the Mac Terminal.

In the Terminal window, type the following command to connect to your EC2 instance:

ssh your-instance-name 

Replace “your-instance-name” with the name you provided in the SSH configuration file.

If prompted, type “yes” to confirm the authenticity of the host.

If you set a passphrase for your private key, you will be prompted to enter it.

Once the passphrase is entered (if applicable), you will be logged in to your EC2 instance via SSH.

Accessing EC2 Instance with SSH

Congratulations! You have successfully SSH-ed into your EC2 instance using the Mac Terminal. Now you can execute commands, install software, transfer files, and perform various administrative tasks on your EC2 instance.

Troubleshooting SSH Connection Issues

If you encounter any issues while establishing the SSH connection, here are a few troubleshooting steps you can follow:

  1. Double-check the public IP address of your EC2 instance.
  2. Ensure that your private key file has the correct permissions (chmod 400).
  3. Verify that the SSH configuration file (config) is properly configured.
  4. Check if the security groups associated with your EC2 instance allow SSH traffic (port 22).

Enhancing SSH Security

To enhance the security of your SSH connection, consider implementing the following best practices:

  1. Disable SSH password authentication and use only key-based authentication.
  2. Set strong passphrases for your private key.
  3. Regularly update your SSH key pair.
  4. Limit SSH access to specific IP addresses or IP ranges.
  5. Monitor SSH logs for any suspicious activity.

Conclusion

SSH provides a secure and convenient way to access and manage your EC2 instances on AWS. In this article, we discussed how to SSH into your EC2 instance using the Mac Terminal. We covered the prerequisites, generated an SSH key pair, launched an EC2 instance, located the public IP address, configured SSH permissions, established the SSH connection, and provided troubleshooting tips. By following these steps, you can efficiently manage your EC2 instances and perform various administrative tasks.

FAQs

Q: Can I SSH into my EC2 instance from a Windows machine?

A: Yes, you can SSH into your EC2 instance from a Windows machine using tools like PuTTY or the Windows Subsystem for Linux (WSL).

Q: What is the default username for SSH into an EC2 instance?

A: The default username for SSH into an EC2 instance depends on the operating system. For Amazon Linux 2, the username is “ec2-user”. For Ubuntu, it is “ubuntu”. Please refer to the AWS documentation for the default usernames of other operating systems.

Q: How can I revoke SSH access to an EC2 instance?

A: To revoke SSH access to an EC2 instance, you can remove the associated security group rule allowing SSH traffic or modify the inbound rules to restrict the allowed IP addresses.

Q: Can I SSH into my EC2 instance without using a key pair?

A: SSH authentication using key pairs is the recommended method for secure access to EC2 instances. However, you can configure password-based authentication, although it is less secure and not recommended.

Q: Is it possible to use a custom SSH key for authentication?

A: Yes, you can use a custom SSH key for authentication by specifying the path to the custom private key file in the SSH configuration. Make sure the key file has the correct permissions (chmod 400).

For more articles on AWS help and queries, please visit us at kapilnawani.com.