带密码的ed25519 ssh key安全使用方法
直接生成ed25519
ssh-keygen -t ed25519 -C ""
如果需要带hostname可以把-C参数去掉, 默认生成 ~/.ssh/id_ed25519和~/.ssh/id_ed25519.pub
pub文件内容存入登录服务器~/.ssh/authorized_keys后, 直接用ssh 用户@IP 直接登录
生成过程示例
#示例运行账号是root,所以显示的路径是/root/.ssh/
ssh-keygen -t ed25519 -C "" #开始生成, 去掉-C参数则会带hostname信息, 也可以 用-C输入指定信息用于识别
Generating public/private ed25519 key pair. #提示信息
Enter file in which to save the key (/root/.ssh/id_ed25519): #询问保存路径,一般默认就行也可以指定。
Enter passphrase for "/root/.ssh/id_ed25519" (empty for no passphrase): #输入你的私钥密码,直接回车则不设置密码,但不建议。
Enter same passphrase again: #重复私钥密码
Your identification has been saved in /root/.ssh/id_ed25519 #生成的私钥
Your public key has been saved in /root/.ssh/id_ed25519.pub #生成的公钥
The key fingerprint is: #已下是提示信息
SHA256:+o0845BYVdSsxxxxxxxxxxxxxxPNy3OfgM
The key's randomart image is:
+--[ED25519 256]--+
| .oB++oo .oo |
|. . o . ... o |
| o . .= . |
| o + o.o * |
| + +..S= * |
| Eo.o+ * + |
| ..o+. . + . |
| . o+oo |
| . o=o. |
+----[SHA256]-----+
generate.sh内容
#!/usr/bin/env bash
set -euo pipefail
KEY="$HOME/.ssh/id_ed25519"
mkdir -p "$HOME/.ssh"
chmod 700 "$HOME/.ssh"
if [[ -e "$KEY" || -e "$KEY.pub" ]]; then
echo "错误:密钥文件已存在:$KEY"
echo "为避免覆盖现有密钥,请先备份或手动处理。"
exit 1
fi
ssh-keygen -t ed25519 -f "$KEY" -C ""
chmod 600 "$KEY"
chmod 644 "$KEY.pub"
echo
echo "密钥生成完成:"
echo "私钥:$KEY"
echo "公钥:$KEY.pub"
添加密钥让系统记住私钥密码
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
MacOS添加密钥
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
示例设置密钥缓存有效期8小时
eval "$(ssh-agent -s)"
ssh-add -t 8h ~/.ssh/id_ed25519
查看已加载的密钥
ssh-add -l
查看具体公钥:
ssh-add -L
ps:Windows下有安装openssh-client的也是类似方式
Linux下借助 keychain长期记住私钥密码方法
#这里用apt为例子
apt update
apt install keychain -y
cat>>~/.bashrc<<EOF
eval "\$(keychain --eval --quiet ~/.ssh/id_ed25519)"
EOF
source ~/.bashrc
ssh-keygen除了ed25519还有rsa和ecdsa可选,建议用大一点的算法参数,dsa已经属于淘汰类型不建议使用,都大同小异,rsa兼容性好,ed25519的密文比较简短。
ssh-keygen参数选择预览
# ssh-keygen ?
Too many arguments.
usage: ssh-keygen [-q] [-a rounds] [-b bits] [-C comment] [-f output_keyfile]
[-m format] [-N new_passphrase] [-O option]
[-t dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa]
[-w provider] [-Z cipher]
ssh-keygen -p [-a rounds] [-f keyfile] [-m format] [-N new_passphrase]
[-P old_passphrase] [-Z cipher]
ssh-keygen -i [-f input_keyfile] [-m key_format]
ssh-keygen -e [-f input_keyfile] [-m key_format]
ssh-keygen -y [-f input_keyfile]
ssh-keygen -c [-a rounds] [-C comment] [-f keyfile] [-P passphrase]
ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]
ssh-keygen -B [-f input_keyfile]
ssh-keygen -D pkcs11
ssh-keygen -F hostname [-lv] [-f known_hosts_file]
ssh-keygen -H [-f known_hosts_file]
ssh-keygen -K [-a rounds] [-w provider]
ssh-keygen -R hostname [-f known_hosts_file]
ssh-keygen -r hostname [-g] [-f input_keyfile]
ssh-keygen -M generate [-O option] output_file
ssh-keygen -M screen [-f input_file] [-O option] output_file
ssh-keygen -I certificate_identity -s ca_key [-hU] [-D pkcs11_provider]
[-n principals] [-O option] [-V validity_interval]
[-z serial_number] file ...
ssh-keygen -L [-f input_keyfile]
ssh-keygen -A [-a rounds] [-f prefix_path]
ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]
file ...
ssh-keygen -Q [-l] -f krl_file [file ...]
ssh-keygen -Y find-principals -s signature_file -f allowed_signers_file
ssh-keygen -Y match-principals -I signer_identity -f allowed_signers_file
ssh-keygen -Y check-novalidate -n namespace -s signature_file
ssh-keygen -Y sign -f key_file -n namespace file [-O option] ...
ssh-keygen -Y verify -f allowed_signers_file -I signer_identity
-n namespace -s signature_file [-r krl_file] [-O option]



