Mac ssh登陆远程Linux不需输入密码

步骤如下:

1. 使用ssh-key-gen在本地Mac上生成公钥和密钥

jsmith@local-host$ [Note: You are on local-host here]

jsmith@local-host$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Pess enter key]
Your identification has been saved in /home/jsmith/.ssh/id_rsa.
Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub.
The key fingerprint is:
33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jsmith@local-host

2.把产生的公钥拷贝到远端host

一般Linux中可以直接使用 ssh-copy-id命令完成:

jsmith@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
jsmith@remote-host's password:
Now try logging into the machine, with "ssh 'remote-host'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.
{ssh-copy-id会把公钥追加到远程主机的~/.ssh/authorized_keys中}


但是Mac 下使用ssh-copy-id报错: -bash: ssh-copy-id: command not found
解决办法1: 
使用命令代替ssh-copy-id:   cat ~/.ssh/id_rsa.pub | ssh usrname@remotehost "umask 077;  mkdir -p .ssh; cat>>.ssh/authorized_keys"
其中id_rsa.pub是生成的公钥, usrname是远程用户名,remotehost是远程主机ip
解决办法2:
直接使用scp把公钥拷贝到remotehost,然后登陆的remotehost上创建文件~/.ssh/authorized_keys, 并把公钥的内容追加到这个文件中。
jsmith@local-host$ scp ~/.ssh/id_rsa.pub  usrname@remotehost:~/.ssh/
jsmith@local-host$  cat id_rsa.pub>>~/.ssh/authorized_keys

3.再次登陆远程主机
jsmith@local-host$ ssh remotehost
这时,不用输入密码就可以成功登陆了~

参考资料:http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/

你可能感兴趣的:(Linux,Mac,OS)