I am experiencing something strange. I am working on a script to remote install Splunk on servers via a script. In this script I ssh to my practice servers (5 amazon EC2 instances running Redhat).

However, when I run the script I get this error:

Warning: Identity file ~/Keypairs/jadam-macbookpro-ec2-1.pem not accessible: No such file or directory.

My first step was to obviously make sure that the path was there. I copy the command run and paste it into my terminal. I successfully connect to my instance using the keypair.

Here is the command in question. I have tried it both with and without sudo prefixing the command:

sudo ssh -i "$KEYPAIR" "$DST" "$REMOTE_SCRIPT"

REMOTE_SCRIPT is the script file to run, and DST is my server addresses pulled from a hosts file.

The resolved command is:

ssh -i ~/Keypairs/jadam-macbookpro-ec2-1.pem ec2-user@

The script fails, but copying the command and doing it manually works. I thought it was a permissions issue at first, which is why I ran it with sudo, but it thinks the directory doesn't exist.

Any ideas?

  • 1
    Change ~ to $HOME. – jordanm Jun 11 '15 at 15:44

Actually your resolved command isn't right. It's really this:

ssh -i "~/Keypairs/jadam-macbookpro-ec2-1.pem" "ec2-user@"

The double quotes stop the shell expanding ~ and it's treated there as a literal (passed directly on to ssh). Replace it with the environment variable $HOME and the problem will resolve itself:

sudo ssh -i "$KEYPAIR" "$DST" "$REMOTE_SCRIPT"

I should point out that ssh will evaluate the ~ as home directory, but in this case it will be doing so in root's context rather than yours, so will not be looking for the file in your $HOME/.ssh directory.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.