在现代网络安全中,服务器的访问控制是至关重要的一环,传统的用户名和密码认证方式虽然简单直接,但存在较大的安全隐患,如密码强度不足、容易遭受暴力破解等,越来越多的企业和组织选择通过禁用密码登录并改用SSH密钥认证来提高系统安全性,本文将详细介绍如何在Linux服务器上配置禁用密码登录,实现基于SSH密钥的安全访问。
一、生成SSH密钥对
1. 使用ssh-keygen工具生成密钥对
1.1 打开终端并输入以下命令:
ssh-keygen -t rsa -b 2048
1.2 按Enter键接受默认的文件路径和文件名:
Generating public/private rsa key pair. Enter file in which to save the key (/home/username/.ssh/id_rsa):
1.3 输入密钥的密码(可选):
Enter passphrase (empty for no passphrase): Enter same passphrase again:
1.4 生成完成后,你会看到类似如下信息:
Your identification has been saved in /home/username/.ssh/id_rsa. Your public key has been saved in /home/username/.ssh/id_rsa.pub. The key fingerprint is:
2. 将公钥上传到服务器
2.1 使用scp命令将公钥复制到服务器:
scp /home/username/.ssh/id_rsa.pub username@server_ip:~/
2.2 在服务器上,将公钥追加到~/.ssh/authorized_keys
文件中:
cat id_rsa.pub >> ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys
二、配置SSH服务禁止密码登录
1. 编辑SSH配置文件
1.1 使用vim编辑/etc/ssh/sshd_config
文件:
sudo vim /etc/ssh/sshd_config
1.2 修改以下参数:
PasswordAuthentication yes PasswordAuthentication no PubkeyAuthentication no PubkeyAuthentication yes
确保PasswordAuthentication
被设置为no
,而PubkeyAuthentication
被设置为yes
。
2. 重启SSH服务
2.1 保存文件后,重启SSH服务以使配置生效:
sudo systemctl restart sshd
或者在某些系统中:
sudo service sshd restart
三、测试与验证
1. 测试SSH密钥登录
1.1 使用私钥进行SSH连接:
ssh -i /home/username/.ssh/id_rsa username@server_ip
如果一切配置正确,你应该能够无密码登录到服务器。
四、常见问题与解答
1. 如何更改现有用户的密码策略?
可以通过锁定用户密码或使用特定命令限制密码登录来实现,锁定用户密码:
sudo passwd -l username
解锁用户密码:
sudo passwd -u username
2. 如何允许特定用户使用密码登录?
可以通过修改/etc/ssh/sshd_config
文件,添加Match语句来允许特定用户使用密码登录:
Match User specificuser PasswordAuthentication yes
然后重启SSH服务,对于其他用户,密码登录仍然被禁止。
各位小伙伴们,我刚刚为大家分享了有关“服务器禁止密码登录”的知识,希望对你们有所帮助。如果您还有其他相关问题需要解决,欢迎随时提出哦!
文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/25207.html<