Log in with an SSH Private Key on Linux/Mac

  • Last updated on: 2016-01-05
  • Authored by: Brint Ohearn

This example demonstrates how to use a private key to log in to a Linux server with a private key by using a Terminal session on OS X. However, you can follow the same process to use a private key when using any terminal software on Linux.

You need two software applications to complete this process:

  1. ssh - SSH client software is installed on your Linux or OS X operating system by default
  2. Your favorite text editor

In this example, we use the vim text editor.

Note: These instructions apply to the Linux and OS X operating systems. For information about using SSH private keys on Windows operating systems, see Logging in with an SSH Private Key on Windows.

Logging in with a private key

  1. Using your favorite text editor, create a file to store your private key. In this example, we use the file deployment_key.txt.

  2. To edit the file in vim, type the following command

       vim deployment_key.txt

After the editor starts, press ‘i’ to turn on insert mode. Then, paste the private key into the file.

Note: You need to include the BEGIN and END lines to use the private key.

  1. To save your changes, press Escape. Then, type ““:wq”to write the file and return to the command line.

  2. After saving the file, run the following command to change the file permissions to 600 to secure the key. You can also set them to 400. This step is required:

       chmod 600 deployment_key.txt
  3. After saving the key, use it to login to the SSH client as shown in this example that loads the key in file deployment_key.txt, and logs in as user demo,to IP

       ssh -i deployment_key.txt demo@
  4. When the prompt displays to confirm the connection request, type yes. Then, hit enter. If your SSH key requires a password, you will be prompted to enter it to complete the connection.

Continue the conversation in the Rackspace Community.