Any serious DevOps will only ssh by key file. Not with password, right? And mostly our powerful key file can unlock many critical envs. Have you ever uploaded your private key to other envs, like jumpbox? What if your key is magically stolen by hackers somehow?
Time to protect your sensitive ssh key by passphrase. And live with it, headache-free.
Original Article: http://dennyzhang.com/ssh_passphrase
Update Per Audience Feedback:
Cheat Sheet for impatient users. Recommend to read this post through, even for experienced users.
Name SummaryLoad key file | ssh-add ~/.ssh/id_rsa |
Remove all loaded keys | ssh-add -D |
Whether it's encrypted | grep "ENCRYPTED" id_rsa |
Add/Change passphrase | ssh-keygen -p -f id_dsa |
Remove passphrase | ssh-keygen -p -P $passwd -N "" -f id_rsa |
Load key without prompt | Check link: here |
We can easily use ssh-keygen to add passphrase. This certainly gives us extra security benefit. Next, what's the impact of this change?
# Change file mode to allow overwrite chmod 700 id_rsa # Add passphrase to key file ssh-keygen -p -f id_rsa # Denny-mac:.ssh mac$ ssh-keygen -p -f id_rsa # Key has comment 'id_rsa' # Enter new passphrase (empty for no passp... # Enter same passphrase again: # Your identification has been saved with ...
Pity that ssh-add itself doesn't have native support for this[1]. Here is a workaround. A bit tricky, I admit.
# Specify your passphrase here export YOUR_PASSPHRASE="XXX" # Load protected key without prompt echo "echo $YOUR_PASSPHRASE" > /tmp/mypass chmod 700 /tmp/mypass cat id_rsa| SSH_ASKPASS=/tmp/mypass ssh-add - # Verify loaded certificate ssh-add -l
Run below command. You will be asked to input old passphrase and new one. If the key is not encrypted, just press enter in the terminal.
ssh-keygen -p -f ~/.ssh/id_dsa
Use openssl to remove passphrase.[2] You will need to manually input old passphrase.
openssl rsa -in id_rsa -out id_rsa_new
Same can be done by ssh-keygen.[3] The amazing part is no required human intervene. Totally automated.
ssh-keygen -p -P "$OLDPASS" -N "" -f id_rsa
More Reading: Reverse SSH Tunnel: Export Your Mac Laptop To The Internet.
Article Number: 528
Posted: Tue, Mar 5, 2019 9:36 AM
Last Updated: Tue, Mar 5, 2019 9:36 AM
Online URL: http://kb.ictbanking.net/article.php?id=528