生成密钥
# 生成rsa密钥对
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): # 直接enter
Enter passphrase (empty for no passphrase): # 直接enter
Enter same passphrase again: # 直接enter
Your identification has been saved in /root/.ssh/id_rsa # 生成的私钥
Your public key has been saved in /root/.ssh/id_rsa.pub # 生成的公钥
安装公钥
# 进入公钥目录
cd /root/.ssh/
# 将公钥写入 /root/.ssh/authorized_keys文件
cat id_rsa.pub >> authorized_keys
设置.ssh权限,防止无法登录
chmod 600 authorized_keys
chmod 700 ~/.ssh
设置ssh打开密钥登录功能
vim /etc/ssh/sshd_config
# 启用密钥验证
PubkeyAuthentication yes
# 关闭密码登录
PasswordAuthentication no
# 允许root用户通过ssh登录
PermitRootLogin yes
重启ssh服务
sudo systemctl restart sshd
如果可以使用密码登录
cat /etc/ssh/sshd_config.d/50-cloud-init.conf
PasswordAuthentication yes
# 将yes改为no 或者直接删除该文件(50-cloud-init.conf)
PasswordAuthentication no