进入到home目录(如果没有.ssh目录,则使用ssh命令连接一次其他主机就会生成)
cd ~/.ssh
生成公钥和私钥对:
ssh-keygen -t rsa
然后敲(三个回车),就会生成两个文件id_rsa(私钥)、id_rsa.pub(公钥)
示例:
创建一个 SSH key
$ ssh-keygen -t rsa -C "your_email@example.com"
代码参数含义:
-t 指定密钥类型,默认是 rsa ,可以省略。
-C 设置注释文字。
-f 指定密钥文件存储文件名。
以上代码省略了 -f 参数,因此,运行上面那条命令后会让你输入一个文件名,用于保存刚才生成的 SSH key 代码,如:
Generating public/private rsa key pair. # Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter]
当然,你也可以不输入文件名,使用默认文件名(推荐),那么就会生成 id_rsa 和 id_rsa.pub 两个秘钥文件。
接着又会提示你输入两次密码(该密码是你push文件的时候要输入的密码),
当然,你也可以不输入密码,直接按回车。那么push的时候就不需要输入密码,如:
Enter passphrase (empty for no passphrase): # Enter same passphrase again:
接下来,就会显示如下代码提示,如:
Your identification has been saved in /c/Users/you/.ssh/id_rsa. # Your public key has been saved in /c/Users/you/.ssh/id_rsa.pub. # The key fingerprint is: # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
当你看到上面这段代码的收,那就说明,你的 SSH key 已经创建成功。
将公钥拷贝到要免密登录的目标机器和自己机器
ssh-copy-id -i .ssh/id_rsa.pub root@192.168.1.102
ssh文件夹下的文件功能解释
(1)~/.ssh/known_hosts :记录ssh访问过计算机的公钥(public key)
(2)id_rsa :生成的私钥
(3)id_rsa.pub :生成的公钥
(4)authorized_keys :存放授权过得无秘登录服务器公钥
问题记录:
linux root 目录下没有.ssh目录
# ssh localhost
说明: .ssh 是记录密码信息的文件夹,如果没有登录过root的话,就没有 .ssh 文件夹,因此登录 localhost ,并输入密码就会生成了。