ey dee ey em
ey dee ey em

Reputation: 8623

How to generate and copy a SSH key in one line on macOS?

I use this command to generate a key:

ssh-keygen -t rsa -b 4096 -C "[email protected]"`

However I do not want to save it as a file, instead I would like to copy it to my clipboard so it is ready to be pasted.

How can I do that? How to combine this with some sort of copy to clipboard command?

I tried the following command but it didn't work:

pbcopy ssh-keygen -t rsa -b 4096 -C "[email protected]"ssh-keygen -t rsa -b 4096 -C "[email protected]"

Upvotes: 2

Views: 3438

Answers (1)

Ronan Boiteau
Ronan Boiteau

Reputation: 10138

You should create a script (or a function) to achieve this. Example with a script:

genkey.sh

#!/bin/bash
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f $1 && pbcopy < $1.pub

The first command generates a key at the location given in the first argument of the script. The second one, pbcopy, copies the content of the newly-generated public key in your clipboard.

When running the script, feed it the path to the private key you want to generate:

sh genkey.sh ~/.ssh/id_rsa

Upvotes: 2

Related Questions