Upon the initial launch of the AMI, you have the option to access it using the "connect" user ID through two different methods. Here is a detailed guide on how to use each method:
Option 1: Indirect Login via "ec2-user" (available immediately after launch)
-
Identify your AMI host
Use the command below to assign your AMI public name to a variable:
remoteUser> export AWS_AMI_HOST=ec2-44-199-189-148.compute-1.amazonaws.com
-
Connect to the "ec2-user"
Use the following command to SSH into the "ec2-user" account using its PEM file:
remoteUser> ssh -i ami-ec2-user.pem ec2-user@$AWS_AMI_HOST
-
Switch to the "connect" User
Once logged in, use the command below to switch to the "connect" user:
ec2-user> sudo su – connect
Option 2: Direct login as "connect" User (setup required)
-
Generate an SSH key pair (skip if you already have one)
On your local machine, generate an SSH key pair using the following command:
ssh-keygen -t rsa -b 2048
-
Transfer the public key to the EC2 instance
- Copy the public key (id_rsa.pub) content. You can view the content using:
cat ~/.ssh/id_rsa.pub
Now, log in to your EC2 instance with your PEM key as usual and execute the following commands to add the public key to the "connect" user's authorized keys:
- Switch to the "connect" user:
sudo su - connect
- Create a .ssh directory (if it does not exist) and navigate to it:
mkdir -p ~/.ssh cd ~/.ssh
- Edit or create the authorized_keys file and add the content of id_rsa.pub to it. You can use a text editor like vi to do this:
vi authorized_keys
- Paste the content of id_rsa.pub into this file, save, and exit the editor.
- Copy the public key (id_rsa.pub) content. You can view the content using:
-
Adjust permissions on the .ssh directory and authorized_keys file
To secure the SSH setup, set the appropriate permissions on the .ssh directory and authorized_keys file:
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
-
Login directly as the "connect" User
Now, from your local machine, you can log in directly as the "connect" user using the private key (id_rsa) you generated earlier:
ssh -i ~/.ssh/id_rsa connect@ec2-44-199-189-148.compute-1.amazonaws.com
This setup maintains a high level of security as it relies on SSH key authentication rather than passwords.