Skip to content

Instantly share code, notes, and snippets.

@hongkongkiwi
Last active September 20, 2024 16:01
Show Gist options
  • Save hongkongkiwi/fff178c3243ae5aaff8e to your computer and use it in GitHub Desktop.
Save hongkongkiwi/fff178c3243ae5aaff8e to your computer and use it in GitHub Desktop.
Simple one liner to generate an SSH key without a password in the default place with the comment as hostname then print out the public key for copy and paste.
HOSTNAME=`hostname` ssh-keygen -t rsa -C "$HOSTNAME" -f "$HOME/.ssh/id_rsa" -P "" && cat ~/.ssh/id_rsa.pub
@nth347
Copy link

nth347 commented Apr 26, 2022

I optimized the command:

echo "y\n" | HOSTNAME=`hostname` ssh-keygen -t rsa -C "$HOSTNAME" -f "$HOME/.ssh/id_rsa" -P ""

No need to press anything after running the command.

@hongkongkiwi
Copy link
Author

hongkongkiwi commented Apr 26, 2022

To optimise further without piping text or using HOSTNAME variable, I suppose this could also be used if you wanted to override existing key

rm -f "$HOME/.ssh/id_rsa"; ssh-keygen -t rsa -C "`hostname`" -f "$HOME/.ssh/id_rsa" -P "" -q

or this if you didnt want to override:

test -f "$HOME/.ssh/id_rsa" || ssh-keygen -t rsa -C "`hostname`" -f "$HOME/.ssh/id_rsa" -P "" -q

P.S. on my version of ssh-keygen I don't get any interactive prompt requiring y or n when I pass -P ""

@pligor
Copy link

pligor commented Aug 8, 2022

Why not auto copy in clipboard ?
You need to install xclip either in linux or mac:

linux: sudo apt-get install xclip

mac: brew install xclip

and then append to any of the above commands:

| xclip -selection clipboard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment