news 2026/2/21 19:51:18

Linux服务器安全加固实战指南

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Linux服务器安全加固实战指南

前言

服务器暴露在公网,每天都在被扫描、被尝试暴力破解。

不做安全加固就是在裸奔。这篇整理Linux服务器的安全加固方案,从SSH加固到防火墙配置,覆盖生产环境常用的安全措施。


一、SSH安全加固

SSH是最常见的攻击入口,必须重点加固。

1.1 修改默认端口

# /etc/ssh/sshd_configPort22222# 改成非标准端口

改完重启:

systemctl restart sshd

改端口能过滤掉90%的自动化扫描。

1.2 禁止root登录

# /etc/ssh/sshd_configPermitRootLogin no

创建普通用户,需要root权限时用sudo:

useradd-madminpasswdadminusermod-aGsudoadmin# Debian/Ubuntu# 或usermod-aGwheel admin# CentOS/RHEL

1.3 禁用密码登录

只允许密钥登录:

# /etc/ssh/sshd_configPasswordAuthentication no PubkeyAuthenticationyes

确保先把公钥配好:

# 本地生成密钥ssh-keygen-ted25519-C"your-email@example.com"# 上传公钥ssh-copy-id-i~/.ssh/id_ed25519.pub-p22222admin@server

1.4 限制登录来源

只允许特定IP登录:

# /etc/ssh/sshd_configAllowUsers admin@192.168.1.*

或者用防火墙限制:

# iptablesiptables-AINPUT-ptcp--dport22222-s192.168.1.0/24-jACCEPT iptables-AINPUT-ptcp--dport22222-jDROP# firewalldfirewall-cmd--permanent--add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" port protocol="tcp" port="22222" accept'firewall-cmd--reload

1.5 完整的sshd_config示例

Port22222PermitRootLogin no PasswordAuthentication no PubkeyAuthenticationyesMaxAuthTries3ClientAliveInterval300ClientAliveCountMax2AllowUsers admin X11Forwarding no UseDNS no

二、防火墙配置

2.1 iptables基础规则

#!/bin/bash# firewall.sh# 清空规则iptables-Fiptables-X# 默认策略iptables-PINPUT DROP iptables-PFORWARD DROP iptables-POUTPUT ACCEPT# 允许本地回环iptables-AINPUT-ilo-jACCEPT# 允许已建立的连接iptables-AINPUT-mstate--stateESTABLISHED,RELATED-jACCEPT# 允许SSH(改成你的端口)iptables-AINPUT-ptcp--dport22222-jACCEPT# 允许HTTP/HTTPSiptables-AINPUT-ptcp--dport80-jACCEPT iptables-AINPUT-ptcp--dport443-jACCEPT# 允许ping(可选)iptables-AINPUT-picmp --icmp-type echo-request-jACCEPT# 保存规则iptables-save>/etc/iptables.rules

开机自动加载:

# /etc/rc.local 或 systemd serviceiptables-restore</etc/iptables.rules

2.2 firewalld配置

CentOS 7+默认用firewalld:

# 查看状态firewall-cmd--state# 开放端口firewall-cmd--permanent--add-port=80/tcp firewall-cmd--permanent--add-port=443/tcp firewall-cmd--permanent--add-port=22222/tcp# 移除端口firewall-cmd--permanent--remove-port=22/tcp# 重新加载firewall-cmd--reload# 查看开放的端口firewall-cmd --list-ports

2.3 限制连接速率

防止暴力破解和CC攻击:

# 限制SSH连接速率iptables-AINPUT-ptcp--dport22222-mstate--stateNEW-mrecent--setiptables-AINPUT-ptcp--dport22222-mstate--stateNEW-mrecent--update--seconds60--hitcount4-jDROP

60秒内超过4次新连接就封禁。


三、fail2ban防暴力破解

3.1 安装

# Debian/Ubuntuaptinstallfail2ban# CentOSyuminstallepel-release yuminstallfail2ban

3.2 配置

# /etc/fail2ban/jail.local[DEFAULT]bantime=3600findtime=600maxretry=5banaction=iptables-multiport[sshd]enabled=trueport=22222filter=sshd logpath=/var/log/auth.log# Ubuntu# logpath = /var/log/secure # CentOSmaxretry=3

3.3 常用命令

# 启动systemctl start fail2ban systemctlenablefail2ban# 查看状态fail2ban-client status fail2ban-client status sshd# 查看被封禁的IPfail2ban-client get sshd banned# 解封IPfail2ban-clientsetsshd unbanip1.2.3.4

四、系统加固

4.1 关闭不需要的服务

# 查看运行的服务systemctl list-units--type=service--state=running# 关闭不需要的systemctl stop postfix systemctl disable postfix

常见可以关闭的服务:

  • postfix(不需要发邮件)
  • cups(不需要打印)
  • avahi-daemon(不需要mDNS)
  • rpcbind(不用NFS)

4.2 内核参数加固

# /etc/sysctl.conf# 禁止IP转发(非路由器)net.ipv4.ip_forward=0# 禁止ICMP重定向net.ipv4.conf.all.accept_redirects=0net.ipv4.conf.default.accept_redirects=0# 禁止源路由net.ipv4.conf.all.accept_source_route=0net.ipv4.conf.default.accept_source_route=0# 启用SYN Cookie防护net.ipv4.tcp_syncookies=1# 忽略ping广播net.ipv4.icmp_echo_ignore_broadcasts=1# 记录可疑数据包net.ipv4.conf.all.log_martians=1

应用配置:

sysctl-p

4.3 文件权限检查

# 关键文件权限chmod600/etc/shadowchmod644/etc/passwdchmod600/etc/ssh/sshd_configchmod700~/.sshchmod600~/.ssh/authorized_keys# 查找SUID文件(潜在风险)find/-perm-4000-typef2>/dev/null# 查找无主文件find/-nouser-o-nogroup2>/dev/null

4.4 设置umask

# /etc/profile 或 /etc/bashrcumask027

新建文件默认权限更严格。


五、日志与审计

5.1 配置日志

确保关键日志开启:

# 查看日志配置cat/etc/rsyslog.conf# 关键日志文件/var/log/auth.log# 认证日志(Ubuntu)/var/log/secure# 认证日志(CentOS)/var/log/messages# 系统日志/var/log/lastlog# 最后登录/var/log/wtmp# 登录历史/var/log/btmp# 失败登录

5.2 查看登录记录

# 当前登录用户wwho# 最近登录last-10# 失败登录lastb-10# 登录统计lastlog

5.3 简单的入侵检测

#!/bin/bash# security_check.shecho"=== 安全检查$(date)==="echo-e"\n[1] 当前登录用户"wecho-e"\n[2] 最近登录"last-10echo-e"\n[3] 失败登录尝试"lastb2>/dev/null|head-20echo-e"\n[4] SSH认证失败"grep"Failed password"/var/log/auth.log2>/dev/null|tail-10grep"Failed password"/var/log/secure2>/dev/null|tail-10echo-e"\n[5] 开放端口"ss-tlnpecho-e"\n[6] 可疑进程(CPU > 50%)"psaux--sort=-%cpu|head-10echo-e"\n[7] 定时任务"foruserin$(cut-f1-d: /etc/passwd);docrontab-l-u$user2>/dev/null|grep-v"^#"|grep-v"^$"doneecho-e"\n[8] 最近修改的文件(24小时内)"find/etc-mtime-1-typef2>/dev/nullecho-e"\n=== 检查完成 ==="

六、自动化安全更新

6.1 Debian/Ubuntu

aptinstallunattended-upgrades dpkg-reconfigure unattended-upgrades

配置文件:

# /etc/apt/apt.conf.d/50unattended-upgradesUnattended-Upgrade::Allowed-Origins{"${distro_id}:${distro_codename}-security";};Unattended-Upgrade::Mail"admin@example.com";Unattended-Upgrade::Automatic-Reboot"false";

6.2 CentOS/RHEL

yuminstallyum-cron systemctlenableyum-cron
# /etc/yum/yum-cron.confupdate_cmd=security apply_updates=yes

七、安全加固检查清单

一键检查脚本:

#!/bin/bash# hardening_check.shecho"=== Linux安全加固检查 ==="# SSH配置检查echo-e"\n[SSH配置]"grep-E"^(Port|PermitRootLogin|PasswordAuthentication)"/etc/ssh/sshd_config2>/dev/null# 防火墙状态echo-e"\n[防火墙]"ifcommand-vfirewall-cmd&>/dev/null;thenfirewall-cmd--state2>/dev/nullelifcommand-vufw&>/dev/null;thenufw statuselseiptables-L-n|head-20fi# fail2ban状态echo-e"\n[fail2ban]"systemctl is-active fail2ban2>/dev/null||echo"未安装/未运行"# 内核参数echo-e"\n[内核参数]"sysctlnet.ipv4.tcp_syncookies net.ipv4.ip_forward2>/dev/null# 开放端口echo-e"\n[开放端口]"ss-tlnp|grepLISTEN# 运行服务数echo-e"\n[运行服务]"systemctl list-units--type=service--state=running|wc-lecho"个服务正在运行"# 自动更新echo-e"\n[自动更新]"ifdpkg-l|grep-qunattended-upgrades;thenecho"已配置(unattended-upgrades)"elifrpm-qyum-cron&>/dev/null;thenecho"已配置(yum-cron)"elseecho"未配置"fiecho-e"\n=== 检查完成 ==="

总结

Linux安全加固核心措施:

层面措施优先级
SSH改端口+禁密码+禁root
防火墙默认DROP,只开必要端口
防暴破fail2ban
系统关闭无用服务
内核sysctl加固
更新自动安全更新
审计日志+定期检查

加固顺序建议:

1. SSH加固(改端口、禁密码、禁root) ↓ 2. 配置防火墙(默认DROP) ↓ 3. 安装fail2ban ↓ 4. 关闭无用服务 ↓ 5. 配置自动更新 ↓ 6. 设置定期检查

安全加固不是一次性的事情,需要持续关注和更新。


有问题评论区交流。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/17 4:44:01

终极指南:7天从零构建SFML游戏开发技能

终极指南&#xff1a;7天从零构建SFML游戏开发技能 【免费下载链接】SFML-Game-Development-Book Full and up-to-date source code of the chapters of the "SFML Game Development" book 项目地址: https://gitcode.com/gh_mirrors/sf/SFML-Game-Development-Boo…

作者头像 李华
网站建设 2026/2/15 20:55:59

LibreCAD终极使用指南:新手必看的10个技巧 [特殊字符]

LibreCAD终极使用指南&#xff1a;新手必看的10个技巧 &#x1f680; 【免费下载链接】LibreCAD LibreCAD is a cross-platform 2D CAD program written in C14 using the Qt framework. It can read DXF and DWG files and can write DXF, PDF and SVG files. The user interf…

作者头像 李华
网站建设 2026/2/21 3:13:25

Audiobookshelf移动应用:构建个人专属有声图书馆的完整指南

想要随时随地享受高质量有声书体验吗&#xff1f;Audiobookshelf移动应用正是你需要的完美解决方案&#xff01;这个开源项目为有声书爱好者提供了完整的移动端管理平台&#xff0c;让你轻松打造个人专属的有声图书馆。无论你是通勤路上、运动健身还是睡前放松&#xff0c;都能…

作者头像 李华
网站建设 2026/2/20 23:29:39

未来生态的构建路径超越摘要:构建人机协同的下一代信息生态系统

引言&#xff1a;从修补漏洞到重绘蓝图面对AI摘要引发的GEO优化失效、商业震荡、内容危机与认知挑战&#xff0c;零散的应对如同在旧船体上修补漏洞。我们需要的是重绘蓝图&#xff0c;构想一个超越当前矛盾、能充分发挥人机各自优势的下一代信息生态系统。这个生态不应是平台、…

作者头像 李华
网站建设 2026/2/14 9:48:20

家庭相册语音标注:翻看老照片时听见背后的故事

家庭相册语音标注&#xff1a;翻看老照片时听见背后的故事 在整理父母的老相册时&#xff0c;你是否曾对着一张泛黄的照片出神——那是在三亚海边的全家福&#xff0c;父亲站在中间笑得开怀&#xff0c;而你那时才刚上小学。你想知道他当时心里在想什么&#xff1f;如果这张照片…

作者头像 李华
网站建设 2026/2/19 2:02:59

SubFinder智能字幕搜索:影视爱好者的终极解决方案

SubFinder智能字幕搜索&#xff1a;影视爱好者的终极解决方案 【免费下载链接】subfinder 字幕查找器 项目地址: https://gitcode.com/gh_mirrors/subfi/subfinder 还在为找不到合适的字幕而烦恼吗&#xff1f;SubFinder智能字幕搜索工具通过创新的多源搜索技术&#xf…

作者头像 李华