Thursday, June 26, 2014

Connect To Amazon AWS EC2 Instance From a CentOS / RHEL Using SSH Client

i have created/launched Amazon Machine Image (AMI) instance powered by a CentOS/RHEL/Red Hat Enterprise Linux. How can I connect to my instance using OpenSSH Linux/Unix/OSX ssh client?

You can use OpenSSH ssh client program for logging into a remote machine and for executing commands on a remote machine. The same client can be used to login into Amazon AWS EC2 instance powered by a CentOS Linux / Fedora / RHEL.


Create a Key Pair For a New EC2 Instance

  1. Login to your Amazon EC2 console at https://console.aws.amazon.com/ec2/.
  2. Select the region i.e. click US East (N. Virginia). (See fig.01)
  3. From the left navigation pane, click Key Pairs.
  4. Click Create Key Pair.
  5. Type "key name" (such as "centos-aws-db-1") in the new Key pair name box, and then clickCreate. (See fig.02)

    Fig.01: AWS Create SSH Key Pair (click to enlarge)
    Fig.01: AWS Create SSH Key Pair (click to enlarge)
  6. Download the private key file, which is named centos-aws-db-1.pem, and keep it in a safe place. You will need it to access any instances that you launch with this key pair.
Fig.02: Create AWS EC2 SSH Key Pair For Login
Fig.02: Create AWS EC2 SSH Key Pair For Login

You need to use this key-pair for all your new instances. If you lose the key pair, you cannot connect to your Amazon EC2 instances.

Set private key permissions

Use the chmod command to make sure your private key file isn't publicly viewable:
 
chmod 0400 ~/.ssh/centos-aws-db-1.pem
 

Connecting to Amazon EC2 Instance from a RHEL/CentOS Linux/UNIX/OSX Using a SSH Client

The syntax is as follows:
 
ssh -i key-file-name-here.pam ec2-user@public-dns-name
 
In this example, I'm connecting using the key ~/.ssh/centos-aws-db-1.pem file, ec2-user name and the instance's DNS name is ec2-54-211-235-9.compute-1.amazonaws.com:
 
ssh -i ~/.ssh/centos-aws-db-1.pem ec2-user@ec2-54-211-235-9.compute-1.amazonaws.com
 
Sample outputs:
Fig. 03: Successfully ssh into my box powered by RHEL 7
Fig. 03: Successfully ssh into my box powered by RHEL 7

SSH user names for various Linux distros

  • For Amazon Linux, the default user name is ec2-user.
  • For CentOS/RHEL, the user name is often root or ec2-user.
  • For Ubuntu, the user name is ubuntu.
  • For SUSE Linux, the user name is root.
  • Also, check with your AMI provider for username.
References

0 comments:

Post a Comment