Log in with an SSH private key on Linux and macOS
Last updated on: 2019-09-19
Authored by: Brint Ohearn
This article demonstrates how to use a private key to log in to a Linux® server by using a private key with a Terminal session on macOS®. However, you can follow the same process to use a private key when using any terminal software on Linux.
Note: For information about using Secure Shell (SSH) private keys on Microsoft® Windows® operating systems, see Logging in with an SSH Private Key on Windows and Generate RSA keys with SSH by using PuTTYgen.
To complete this process, you need the following software applications:
- SSH client software that is installed on your Linux or macOS operating system by default.
- Your favorite text editor. This example uses the vim text editor.
- Your private key. For more information about generating a key on Linux or macOS, see Connect to a server by using SSH on Linux or Mac OS X.
Log in with a private key
Using a text editor, create a file in which to store your private key. This example uses the file deployment_key.txt.
To edit the file in vim, type the following command:
After the editor starts, press i to turn on insert mode.
Paste your private key, such as the one in the following image, into the file. Be sure to include the BEGIN and END lines.
To save your changes, press Esc.
Type :wq to write the file and return to the command line.
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
Use the key to log in to the SSH client as shown in the following example, which loads the key in file deployment_key.txt, and logs in as user demo to IP 18.104.22.168:
ssh -i deployment_key.txt email@example.com
When you are prompted to confirm the connection, type yes and then press Enter.
If your SSH key requires a password, enter it when prompted to complete the connection.