sshpem

Connect over SSH using a .pem file


I would like to know how to connect over SSH using a .pem file to any server.

Currently I'm executing the following command:

ssh user@mydomain.example

What option should I use?


Solution

  • Use the -i option:

    ssh -i mykey.pem myusername@mydomain.example
    

    As noted in this answer, this file needs to have correct permissions set. The ssh man page says:

    SSH will simply ignore a private key file if it is accessible by others.

    You can change the permissions with this command:

    chmod go= mykey.pem
    

    That is, set permissions for group and others equal to the empty list of permissions.


    If you're going to connect to this server with this key many times in the future, it might be worth configuring it once and for all. Open the file ~/.ssh/config (create it if it doesn't exist), and add:

    Host mydomain.example
        IdentityFile /path/to/mykey.pem
        User myusername
    

    Now you can just type ssh mydomain.example, and it will use the specified key file and user name. See the ssh_config man page for more details.