skip to Main Content
apple@Apples-MacBook-Pro-2 ~ % ssh ec2-13-127-165-161.ap-south-1.compute.amazonaws.com                                                   
hostkeys_find_by_key_hostfile: hostkeys_foreach failed for /Users/apple/.ssh/known_hosts: Not a directory
The authenticity of host 'ec2-13-127-165-161.ap-south-1.compute.amazonaws.com (13.127.165.161)' can't be established.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Failed to add the host to the list of known hosts (/Users/apple/.ssh/known_hosts).
[email protected]: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
apple@Apples-MacBook-Pro-2 ~ % 

I tried following instructions from How to Connect to Amazon EC2 Remotely Using SSH but it didn’t work:

How to connect to Amazon EC2 remotely using SSH:

Download the .pem file.

In Amazon Dashboard choose "Instances" from the left side bar, and then select the instance you would like to connect to.

Click on "Actions", then select "Connect"

Click on "Connect with a Standalone SSH Client"

Open up a Terminal window

Create a directory:

 # mkdir -p ~/.ssh
Move the downloaded .pem file to the .ssh directory we just created:

 # mv ~/Downloads/ec2private.pem ~/.ssh
Change the permissions of the .pem file so only the root user can read it:

 # chmod 400 ~/.ssh/ec2private.pem
Create a config file:

 # vim ~/.ssh/config
Enter the following text into that config file:

 Host *amazonaws.com
 IdentityFile ~/.ssh/ec2private.pem
 User ec2-user
Save that file.

Use the ssh command with your public DNS hostname to connect to your instance.
e.g.:

# ssh ec2-54-23-23-23-34.example.amazonaws.com

Debug output:

OpenSSH_9.0p1, LibreSSL 3.3.6
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts' -> '/Users/apple/.ssh/known_hosts'
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts2' -> '/Users/apple/.ssh/known_hosts2'
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to ec2-54-23-23-23-34.example.amazonaws.com port 22.
ssh: Could not resolve hostname ec2-54-23-23-23-34.example.amazonaws.com: nodename nor servname provided, or not known
apple@Apples-MacBook-Pro-2 ~ % 

2

Answers


  1. As specified in the instructions, you can either need to have the PEM file in your home directory’s .ssh, or you can specify the PEM file directly. Remember to change the PEM file’s permissions (chmod 400 file.pem) :

    ssh -o ServerAliveInterval=15 [email protected] -i /path/to/your/file.pem
    
    Login or Signup to reply.
  2. The steps from the article you show are setting-up an automatic connection without having to specify the .pem file. That is probably confusing things for you, try this simpler method:

    • Put the .pem file in your current directory
    • chmod 400 file.pem
    • ssh -i file.pem [email protected]
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search