ssh-keygen :产生公钥与私钥对.
ssh-copy-id :将本机的公钥复制到远程机器的authorized_keys文件中,ssh-copy-id也能让你有到远程机器的home, ~./ssh , 和 ~/.ssh/authorized_keys的权利
以下是实现步骤:
第一步:在本地机器上使用ssh-keygen产生公钥私钥对
- zhz@zhz:~/$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/zhz/.ssh/id_rsa): - Enter passphrase (empty for no passphrase): [Press enter key
- same passphrase again: [Pess enter key]
- Your identification has been saved in /home/zhz/.ssh/id_rsa.
- Your public key has been saved in /home/zhz/.ssh/id_rsa.pub.
- The key fingerprint is:
- 用cat命令查看是否生成产生公钥私钥对
- zhz@zhz:~$ cat .ssh/id_rsa.pub
- 如果生成成功,则进行下一步
- 第二步:用ssh-copy-id将公钥复制到远程机器中
- 但是默认MAC没有ssh-copy-id命令,但是我们可以手动给心爱的mac安装这个常用到的命令
- 这个是github上找到的解决办法https://github.com/beautifulcode/ssh-copy-id-for-OSX
- 执行下面的这个命令,请求并执行ssh-copy-id安装命令
- sudo curl -L https://raw.githubusercontent.com/beautifulcode/ssh-copy-id-for-OSX/master/install.sh | sh
- 本人执行的时候会因为/usr/local下没有bin文件夹而失败,只需要在/usr/local文件夹下sudo mkdir bin新建一个bin目录
- 然后出现Installed ssh-copy-id into /usr/local/bin说明安装成功,接下来便可用用ssh-copy-id将公钥复制到远程机器中
- ssh-copy-id -i .ssh/id_rsa.pub 用户名字@xxx.xxx.xxx.xxx
- 这时候会让你输入登录密码,输入登录密码之后便OK了
- 之后再执行ssh 用户名@xxx.xxx.xxx.xxx便不需要密码