生成密钥对的途径
方式一
通过客户端软件生成密钥对,将公钥拷贝到远程主机的需要登录的用户目录的.ssh目录中
过程略
方式二
在远程主机中生成密钥对
- 生成密钥对
## 建立密钥对
ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): <== 按 Enter
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): <== 输入密钥锁码,或直接按 Enter 留空
Enter same passphrase again: <== 再输入一遍密钥锁码
Your identification has been saved in /root/.ssh/id_rsa. <== 私钥
Your public key has been saved in /root/.ssh/id_rsa.pub. <== 公钥
The key fingerprint is:
0f:d3:e7:1a:1c:bd:5c:03:f1:19:f1:22:df:9b:cc:08 root@host
- 将私钥拷贝到本地
修改ssh登录配置
- 在远程主机上配置公钥
[root@host ~]$ cd .ssh
[root@host .ssh]$ cat id_rsa.pub >> authorized_keys
- 修改密钥文件的权限
[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh
- 修改sshd配置文件–需要root权限
vi /etc/ssh/sshd_config
—————————————————————————
## 开启sshd密钥登录
## 添加信息
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys # 指定公钥文件
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## 禁用密码登录(最好是在重启sshd服务之后验证密钥登录成功之后再禁用)
## 修改信息,将yes改为no
PasswordAuthentication no
—————————————————————————
- 重启sshd服务
service sshd restart
or
systemctl restart sshd