news 2026/4/15 13:46:19

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/4/14 11:17:22

SubFinder终极指南:一键解决所有字幕搜索难题

SubFinder终极指南&#xff1a;一键解决所有字幕搜索难题 【免费下载链接】subfinder 字幕查找器 项目地址: https://gitcode.com/gh_mirrors/subfi/subfinder 在影视娱乐数字化的今天&#xff0c;SubFinder作为一款智能字幕搜索工具&#xff0c;彻底改变了传统手动搜索…

作者头像 李华
网站建设 2026/4/10 6:35:22

NSMusicS音乐播放器:重新定义你的音乐体验

NSMusicS音乐播放器&#xff1a;重新定义你的音乐体验 【免费下载链接】NSMusicS NSMusicS&#xff08;Nine Songs Music World&#xff1a;九歌 音乐世界&#xff09;&#xff0c;open-source music software 项目地址: https://gitcode.com/GitHub_Trending/ns/NSMusicS …

作者头像 李华
网站建设 2026/4/12 16:15:33

Sigil插件系统终极指南:从零开始掌握电子书编辑自动化

Sigil插件系统终极指南&#xff1a;从零开始掌握电子书编辑自动化 【免费下载链接】Sigil Sigil is a multi-platform EPUB ebook editor 项目地址: https://gitcode.com/gh_mirrors/si/Sigil Sigil作为一款强大的跨平台EPUB电子书编辑器&#xff0c;其插件系统为用户提…

作者头像 李华
网站建设 2026/4/10 14:33:35

jQuery DateTimePicker 完整指南:3分钟掌握专业日期时间选择技巧

jQuery DateTimePicker 完整指南&#xff1a;3分钟掌握专业日期时间选择技巧 【免费下载链接】datetimepicker jQuery Plugin Date and Time Picker 项目地址: https://gitcode.com/gh_mirrors/da/datetimepicker jQuery DateTimePicker 是一款功能强大的日期和时间选择…

作者头像 李华
网站建设 2026/4/9 1:06:41

终极指南:5分钟掌握秋之盒ADB工具箱的图形化设备管理

终极指南&#xff1a;5分钟掌握秋之盒ADB工具箱的图形化设备管理 【免费下载链接】AutumnBox 图形化ADB工具箱 项目地址: https://gitcode.com/gh_mirrors/au/AutumnBox 还在为复杂的命令行操作而头疼吗&#xff1f;秋之盒ADB工具箱通过直观的图形化界面&#xff0c;彻底…

作者头像 李华
网站建设 2026/4/13 19:44:56

5分钟掌握Sigil插件:让你的电子书编辑效率翻倍!

5分钟掌握Sigil插件&#xff1a;让你的电子书编辑效率翻倍&#xff01; 【免费下载链接】Sigil Sigil is a multi-platform EPUB ebook editor 项目地址: https://gitcode.com/gh_mirrors/si/Sigil 还在为重复的电子书编辑工作烦恼吗&#xff1f;Sigil插件系统正是为你量…

作者头像 李华