Skip to content

Instantly share code, notes, and snippets.

@yushiro
Created October 20, 2014 23:37
Show Gist options
  • Save yushiro/15e261152ffe75cebbbf to your computer and use it in GitHub Desktop.
Save yushiro/15e261152ffe75cebbbf to your computer and use it in GitHub Desktop.
在MacOS下设置SSH免密码登陆
在Mac SnowLeopard下经常登陆VPS, 需要手工输入密码, 非常的不方便(在WIN下用PUTTY已经设置了PublicKey登陆),所以花点时间折腾一下。
1. 修改/etc/ssh_config文件
原始的设置是
#Host * #ForwardAgent no
修改为
Host * ForwardAgent yes
2.用OpenSSH的ssh-keygen生成公钥和私钥
[root@mail ~]# ssh-keygen -b 1024 -t rsa -C vps@mac
Generating public/private dsa key pair.
#提示正在生成,如果选择4096长度,可能需要较长时间
Enter file in which to save the key (/root/.ssh/id_dsa): /root/.ssh/vps_mac_rsa
#询问把公钥和私钥放在那里,回车用默认位置即可
Enter passphrase (empty for no passphrase):
#询问输入私钥密语,为了增加安全性,一定要输入密码(自定义,非Login到ssh服务器的密码)
Enter same passphrase again:
#再次提示输入密语,再次确认密码并回车
Your identification has been saved in /root/.ssh/vps_mac_rsa.
Your public key has been saved in /root/.ssh/vps_mac_rsa.pub.
#提示公钥和私钥已经存放在/root/.ssh/目录下
The key fingerprint is:
71:e5:cb:15:d3:8c:05:ed:05:84:85:32:ce:b1:31:ce vps@mac
#提示key的指纹
3. 上传publickey(vps_mac_rsa.pub)到SSH服务器
scp /root/.ssh/vps_mac_rsa.pub 用户名@SSH服务器IP:/home/用户名/.ssh/vps_mac_rsa.pub
登陆到SSH,合并上传的PublicKey到/home/用户名/.ssh/authorized_keys
cd ~/.ssh cat vps_mac_rsa.pub >> authorized_keys
4. 把私钥添加到本地计算机的KeyChain
ssh-add -K /root/.ssh/vps_mac_rsa Enter passphrase for vps_mac_rsa: 输入步骤2中的密码 Identity added: vps_mac_rsa (vps_mac_rsa)
可以用ssh-add -l查看,本地计算机已经添加了多少私钥
root@buyvm:~$ ssh-add -l 1024 71:e5:cb:15:d3:8c:05:ed:05:84:85:32:ce:b1:31:ce vps_mac_rsa (RSA)
然后用SSH登陆VPS,就可以不用输入密码了
参考: SSH 免密碼登入 http://josephjiang.com/article/understand-ssh-key/ linux ssh 使用深度解析(key登录详解) http://blog.lizhigang.net/archives/249
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment