新闻中心
新闻中心与新手教程
新闻中心与新手教程
发布时间:2024-10-13 01:51:15
让我们深入探讨linux系统的安全ssh登录,包括详细的配置步骤、最佳实践和故障排查方法。ssh(secure shell)是远程登录linux系统的主要方式,确保其安全性至关重要。
# 生成ssh密钥对
ssh-keygen -t ed25519 -c "your_email@example.com"
# 将公钥复制到服务器
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@remote_host
# ssh客户端配置 (~/.ssh/config)
host myserver
hostname 192.168.1.100
user myuser
identityfile ~/.ssh/id_ed25519
port 22222
# ssh服务器配置 (/etc/ssh/sshd_config)
port 22222
permitrootlogin no
passwordauthentication no
pubkeyauthentication yes
permitemptypasswords no
x11forwarding no
allowusers myuser
maxauthtries 3
logingracetime 60
clientaliveinterval 300
clientalivecountmax 2
# 重启ssh服务
sudo systemctl restart sshd
# 使用ssh登录
ssh myserver
# 设置防火墙规则 (使用ufw)
sudo ufw allow 22222/tcp
sudo ufw enable
# 使用fail2ban防止暴力攻击
sudo apt install fail2ban
sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
# fail2ban配置示例
[sshd]
enabled = true
port = 22222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
# 重启fail2ban服务
sudo systemctl restart fail2ban
现在,让我们详细介绍linux系统安全ssh登录的步骤和故障排查方法:
ssh-keygen -t ed25519 -c "your_email@example.com"ssh-copy-id -i ~/.ssh/id_ed25519.pub user@remote_hosthost myserver
hostname 192.168.1.100
user myuser
identityfile ~/.ssh/id_ed25519
port 22222
ssh myserverport 22222 # 更改默认端口
permitrootlogin no # 禁止root直接登录
passwordauthentication no # 禁用密码认证
pubkeyauthentication yes # 启用公钥认证
permitemptypasswords no # 禁止空密码
x11forwarding no # 禁用x11转发
allowusers myuser # 只允许特定用户登录
maxauthtries 3 # 最大尝试次数
logingracetime 60 # 登录尝试的超时时间
clientaliveinterval 300 # 客户端活跃信号间隔
clientalivecountmax 2 # 最大活跃信号次数
sudo systemctl restart sshdsudo ufw allow 22222/tcp
sudo ufw enable
sudo iptables -a input -p tcp --dport 22222 -j accept
sudo iptables-save > /etc/iptables/rules.v4
sudo apt install fail2bansudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local
enabled = true
port = 22222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
sudo systemctl restart fail2bansudo apt update && sudo apt upgradesudo tail -f /var/log/auth.logping remote_hostsudo systemctl status sshdsudo ufw status 或 sudo iptables -lssh -v user@remote_hostchmod 600 ~/.ssh/authorized_keyschmod 700 ~/.sshchmod 600 ~/.ssh/id_ed25519restorecon -r -v ~/.sshsudo fail2ban-client status sshdsudo fail2ban-client set sshd unbanip top 或 htopnetstat -tnpa | grep 'established.*sshd'sudo grep sshd /var/log/auth.logjournalctl查看系统日志: journalctl -u sshdallowusers或allowgroups限制可以ssh登录的用户这些深入的解释和示例应该能够帮助你全面理解和实施linux系统的安全ssh登录。记住,ssh安全是一个持续的过程,需要定期审查和更新你的安全措施。同时,在实施任何更改时,务必小心谨慎,以免意外锁定自己。
感谢提供:05互联