Makerere University

Enter a keyword or two into the search box above and click search..

Logging in with a SSH Private Key on Linux/Mac

You are here

We’re going to demonstrate how to use a private key to login to a Linux server with a private key. This tutorial will demonstrate how to do this from a Terminal session on OS X, however the steps are identical when using any terminal software on Linux.

All you will need is:

    ssh – SSH client software, comes installed by default
    Your favorite text editor

We will use vim in this example.

Logging in with a private key

Using your favorite text editor, open the file you’d like to keep your private key in. This file can be named whatever you’d like to call it. We’ll be calling our key deployment_key.txt:

    vim deployment_key.txt

Paste in the private key and save the file. If you’re using vim, once the empty file is opened, press ‘i’ to get into insert mode, paste the key, press Escape and then ‘:wq’ to write and quit.

The BEGIN and END lines are needed when using a private key. Once the file is saved, change the permissions to 600 or 400. This is required:

chmod 600 deployment_key.txt

Now login using the SSH client. Here’s an example where we’re loading the key in file deployment_key.txt, logging in as the user demo, to IP 192.237.248.66:

    ssh -i deployment_key.txt demo@192.237.248.66

When asked about if you’re sure you’d like to connect, type yes and hit enter. You will be prompted to enter the password for your key (if it has one), then you will be logged in to the server.