如何用SSH公钥设置Mac与服务器免密连接

帅旋
订阅
充电
|
  1. 生成部署专用密钥
1
ssh-keygen -t ed25519 -C "deploy@www.yoursite.com" -f ~/.ssh/id_ed25519_json

注意,这里改为你的域名,或者服务器IP。

  1. 把公钥装到服务器
  • 若有 ssh-copy-id:
1
ssh-copy-id -i ~/.ssh/id_ed25519_json.pub arthinking@www.yoursite.com
  • 若没有,用手动方式:
1
cat ~/.ssh/id_ed25519_json.pub | ssh arthinking@www.yoursite.com 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'
  1. 本机配置 ~/.ssh/config 指定该密钥
1
mkdir -p ~/.ssh && chmod 700 ~/.ssh
1
2
3
4
5
6
7
8
9
10
cat >> ~/.ssh/config <<'EOF'
Host www.yoursite.com
User arthinking
Port 22
IdentityFile ~/.ssh/id_ed25519_json
IdentitiesOnly yes
AddKeysToAgent yes
UseKeychain yes
EOF
chmod 600 ~/.ssh/config
  1. 加载到 macOS 钥匙串(避免每次输入密钥口令)
1
ssh-add --apple-use-keychain ~/.ssh/id_ed25519_json
  1. 测试免密
1
ssh arthinking@www.yoursite.com 'echo ok'