“昨晚我们的服务器被入侵了,数据全被加密勒索……”
“客户信息泄露,公司面临天价罚款……”
这些触目惊心的新闻,其实都源于同一个问题:服务器安全初始化没做到位。
在安装系统时,选择 "Minimal Install" (最小化安装) 模式。这确保了只安装最必要的软件包,极大减少了攻击面。
安装完成后,第一步是更新系统到最新状态,以获取最新的安全补丁和错误修复。
# 对于 CentOS/Rocky Linux/AlmaLinux/RHELyum update -y && sudo yum upgrade -y# 或者使用 dnf (新版本)dnf update -y && sudo dnf upgrade -y# 对于 Ubuntu/Debian:apt update && sudo apt upgrade -yapt dist-upgrade -y # 可选,处理有依赖关系的更新
安装常用的系统管理、诊断和网络工具。
# 对于 CentOS/RHEL 系列:yum install -y epel-release# 安装 EPEL 扩展源yum install -y vim wget curl git lsof net-tools bind-utils telnet htop iotop iftop nload ncdu jq yum-utils unzip zip supervisor lrzsz make gcc-c++ ntpdatetimedatectl set-timezone Asia/Shanghaintpdate ntp1.aliyun.com# 对于 Ubuntu/Debian 系列:apt install -y vim wget curl git lsof net-tools bind9-utils telnet htop iotop iftop nload ncdu jq apt-transport-https ca-certificates software-properties-common
# 创建新用户 (例如名为 admin)adduser admin# 为新用户设置密码passwd admin# 授予新用户 sudo 权限# 对于 CentOS/RHEL 系列,通常用户已在 'wheel' 组中usermod -aG wheel admin# 对于 Ubuntu,用户通常在 'sudo' 组中usermod -aG sudo admin
# 对于 CentOS/RHEL 8+/Rocky Linux 8+ (使用 firewalld):systemctl enable --now firewalldfirewall-cmd --permanent --add-service=ssh# 放行 SSH# 如果你的服务需要(如 HTTP/HTTPS):firewall-cmd --permanent --add-service=httpfirewall-cmd --permanent --add-service=httpsfirewall-cmd --reload# 对于 Ubuntu (使用 ufw):ufw enablesudo ufw allow ssh#ufw allow http#ufw allow https# 当前在学习阶段,可以将防火墙和selinux 关闭systemctl stop firewalldsystemctl disable firewalldsetenforce 0sed -i 's/enable/disable/g' /etc/selinux/config
# 修改 SSH 配置文件vim /etc/ssh/sshd_config...PermitRootLogin no # 禁止 root 登录PasswordAuthentication no # 强制使用密钥认证,更安全(确保已部署公钥后再设置此项!)PubkeyAuthentication yes # 启用密钥认证DNS NO...systemctl restart sshd
除了上述设置,还可以进一步加固 SSH:
修改默认端口 (可选但推荐): 在 /etc/ssh/sshd_config 中修改 Port 为一个非标准端口(如 2345)。记得在防火墙中放行新端口。
使用强密码策略或禁用密码登录(强烈推荐密钥登录)。
# CentOS/RHELyum install -y fail2ban# Ubuntuapt install -y fail2ban# 启动并设置开机自启systemctl enable --now fail2ban
systemctl list-unit-files --state=enabled # 查看所有开机自启的服务systemctl stop <service_name> # 停止某个服务systemctl disable <service_name> # 禁止开机自启
# 查看当前限制ulimit -n# 设置为 65535 或更多ulimit -n 102400
系统默认的本地可用端口范围是 32768-60999,大约只有 2.8w 个。我们需要扩大这个范围,并让端口可以快速回收重用。修改内核参数:
编辑 /etc/sysctl.conf,添加或
修改以下行:
# 扩大本地端口范围net.ipv4.ip_local_port_range = 10000 65000# 启用 TIME_WAIT 状态的端口快速回收net.ipv4.tcp_tw_reuse = 1# net.ipv4.tcp_tw_recycle = 1 # 在 NAT 环境下(如云服务器)可能导致问题,CentOS 7 默认已禁用,不建议开启# 增大最大跟踪的连接数(可选,但建议)net.ipv4.ip_conntrack_max = 1048576net.ipv4.netfilter.ip_conntrack_max = 1048576# 增加系统最大打开文件描述符数fs.file-max = 102400# 生效system -p
有时会遇到 mmap 相关的错误。
# 临时sysctl -w vm.max_map_count=262144# 永久:将echo "vm.max_map_count=262144" >> /etc/sysctl.conf
我们已经安装了 htop, iotop, nload 等,它们可以实时查看系统状态。
htop: 交互式进程查看器(比 top 更强大)。
iotop: 监控磁盘 I/O。
nload/iftop: 监控网络流量。
# CentOS/RHEL:yum install -y stress# Ubuntu:apt install -y stress
带宽测试: 使用 iperf3 工具(需要另一台服务器作为服务端)。
延迟和路由追踪: 使用 ping 和 traceroute/mtr。
完成以上步骤后,你的 Linux 主机已经具备了良好的安全基础和性能可观测性。以下是一个快速检查清单:
系统已更新至最新 (yum update / apt update)。
已创建具有 sudo 权限的普通用户。
SSH 已禁用 root 登录和密码认证(仅密钥)。
防火墙已开启并仅放行必要端口(SSH 等)。
Fail2Ban 已安装并运行。
已检查并禁用不必要的服务。
已安装必备的监控和诊断工具(htop, iotop, nload 等)。
(可选)已配置自动安全更新(根据环境谨慎选择)。
(可选)已进行简单的性能基准测试,了解服务器能力。