ubuntu/debian生成公私钥
登入到服务器执行命令生成公私钥(ed25519)
ssh-keygen -t ed25519
#会提示你输入名字和密码,不需要则跳过
#生成的公私钥会保存到当前用户的 ~/.ssh目录
#私钥名(id_ed25519),公钥名id_ed25519.pub
#把生成好的公私钥保存下来编辑 SSH 配置文件
打开ssh配置文件
vi /etc/ssh/sshd_config编辑sshd_config
# 1. 启用密钥登录(默认已开启,确保没有注释
PubkeyAuthentication yes
# 2. 关闭密码登录(关键,将 yes 改为 no)
PermitRootLogin no
#(如果此步骤对关闭密码登入不生效,请看本章内容最下方步骤)
PasswordAuthentication no
# 允许 root 密钥登录(默认)
PermitRootLogin yes
# 禁止 root 登录(如需,谨慎设置)
PermitRootLogin no将公钥内容追加到 authorized_keys
#1.进入.ssh
cd .ssh
#2.创建authorized_keys文件
mkdir authorized_keys
#3.编辑
vi authorized_keys
#把生成的公钥(名id_ed25519.pub)插入到authorized_keys文件中
#给authorized_keys赋予权限(必须 600,否则登录失败)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys重启SSH
systemctl restart sshd
service sshd restart # 旧系统备用注意
如果以上方法无法关闭密码登入则需要以下操作
1.进入到ssh/sshd_config.d目录
cd /etc/ssh/sshd_config.d
ls #查看当前目录的文件
#是否会看到(01-permitrootlogin.conf)(01-cloud.conf)等文件
#编辑看到的文件如(01-cloud.conf)
vi 01-cloud.conf
#把文件内容改成如下
PermitRootLogin no2重启SSH生效配置
评论