Skip to content

Instantly share code, notes, and snippets.

@werelax
Last active September 3, 2016 17:17
Show Gist options
  • Save werelax/ddb856d71513a57163e567e1dd57058b to your computer and use it in GitHub Desktop.
Save werelax/ddb856d71513a57163e567e1dd57058b to your computer and use it in GitHub Desktop.
Generate ssh .pem and .pub files for privateKey-based server auth

Generate a key pair for SSH authentication

Very useful to log into a server without sharing any passwords. A common use case: deploy scripts.

1. Generate the key pair

$ ssh-keygen -t rsa -b 2048 -v

When asked where to put the files, just type key. That would generate two files: ./key.pub and ./key.

2. Copy the public key to the target server

$ ssh-copy-id -i ./key.pub user@server

In OSX you can install ssh-copy-id with brew install ssh-copy-id.

3. Rename the private key and set the right permissions

$ mv ./key ./key.pem
$ chmod 0400 ./key.pem

4. Log in using the private key

$ ssh -i ./key.pem user@server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment