Linux 中常用的免密连接。通过秘钥对实现。当不指定密钥文件时,使用的是默认的id_rsa
- 创建秘钥对命令
ssh-keygen
-t : 指定秘钥类型。默认是rsa. 可以省略 -C : 设置注释文字 -f : 指定秘钥文件存储的文件名。 默认是id_rsa
实例:
# ssh-keygen
# 指定文件名 # ssh-keygen -f /root/.ssh/gitlab -C "GITLAB"
/root/.ssh/config 配置
# 多个密钥对时。ssh连接时,需要指定使用的私钥文件. 当不指定时,使用的时默认的id_rsa文件。所以当你复制的公钥不是id_rsa.pub时,ssh连接也会需要输入密码,实现不了免密登录 # 指定密钥文件另一种方法是在linux 主机的 /root/.ssh 创建config文件。在config文件定义,在使用ssh连接时,会进行区分。 # touch /root/.ssh/config # cat /root/.ssh/config Host 192.168.1.194 Hostname 192.168.1.194 #PreferredAuthentications publickey IdentityFile /root/.ssh/nginx01 Host 192.168.1.191 Hostname 192.168.1.191 #PreferredAuthentications publickey IdentityFile /root/.ssh/nginx02 Host 192.168.1.190 Hostname 192.168.1.190 #PreferredAuthentications publickey # User root IdentityFile /root/.ssh/gitlab
congfig 配置gitee 和 github
# gitee Host gitee.com HostName gitee.com PreferredAuthentications publickey IdentityFile ~/.ssh/gitee_rsa # github Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/github_rsa
使用ssh 命令时指定秘钥文件。
# ssh -i /root/.ssh/nginx01 192.168.1.194
ssh-copy-id 命令:
-i : 复制指定的公钥到远程主机。默认复制的文件是 /root/.ssh/id_rsa.pub # ssh-copy-id -i /root/.ssh/nginx02.pub 192.168.1.190