6
0

服务器优化-1

2026-09-03
2026-09-03
服务器优化-1

1. 按需选择最小化安装系统

一个新的、干净的操作系统,在安装的时候,遵循“最小化安装 → 基础初始化 → 安全加固 → 运维基线 → 再安装业务依赖”的思路,安排好现有服务的需求,针对硬盘进行合理划分,避免非必要的软件安装,准备好必须的软件仓库和软件包,必要的基础网络包和基础应用包。为应对后续升级计划,需要提前留出部分的冗余,应对架构和软件升级。避免第三方不明确的自动化脚本安装任何服务。

2. SSH 是生产服务器最重要的入口

SSH服务是必须要重视的一个点,是我们管理服务器的关键入口,也是被攻击的重点,新的SSH服务需要更改一些配置,保障服务器的安全。

进行如下配置更新:

#编辑sshd_config配置文件
vi /etc/ssh/sshd_config
#SSH链接默认端口,修改默认22端口为1万以上端口号,避免被扫描和攻击。
Port 22221
#不使用DNS反查,可提高ssh连接速度
UseDNS no
#关闭GSSAPI验证,可提高ssh连接速度
GSSAPIAuthentication no
#禁止root账号登陆
PermitRootLogin no

可以配置ssh key,在测试新的ssh key成功的情况下,禁止ssh root,禁止ssh 密码登录。

注意!!!不要在修改配置ssh key之前,直接禁用ssh 密码登录

3.Selinux/Iptables策略设置

SELinux 是 NSA 主导、基于 LSM(Linux Security Module)的内核级强制访问控制(MAC)机制,RHEL/CentOS/Fedora 默认开启。

[root@centos-1 ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 


[root@centos-1 ~]# 


#Selinux的状态:
	enforcing 开启状态、permissive 提醒的状态 、disabled 关闭状态
命令行关闭:setenforce  0(临时生效)
getenforce                          # 查看当前模式
setenforce 1        # 临时切 Enforcing(0=Permissive;disabled 状态下不可用)
sestatus -v         # 详细状态 + 配置文件位置


[root@centos-1 ~]#   grubby --update-kernel ALL --args selinux=0   (永久生效)
[root@centos-1 ~]#   iptables -L -n

SELinux 管的是 "谁(主体)对什么(客体)做什么操作",全部通过安全上下文(Security Context)描述,格式为 user:role:type:level

   system_u   :   system_r   :   httpd_t   :   s0
   SELinux用户   角色         类型(核心)     MLS级别

SELinux 访问决策流程:

常用命令:

# 看文件/进程/端口的上下文
ls -Z /var/www/index.html          # 看文件上下文
ps -eZ | grep nginx                # 看进程上下文
semanage fcontext -l | grep http   # 查看文件上下文映射规则
semanage port -l | grep http       # 查看端口上下文

# 改文件上下文(推荐 semanage + restorecon,勿直接用 chcon)
semanage fcontext -a -t httpd_sys_content_t "/data/web(/.*)?"
restorecon -Rv /data/web           # 按映射重打标签

# 放行自定义端口(如 8080 给 http 服务)
semanage port -a -t http_port_t -p tcp 8080

# 布尔值开关(-P 永久生效)
getsebool -a | grep http
setsebool -P httpd_can_network_connect on     # 允许 httpd 主动对外连接
setsebool -P httpd_can_network_connect_db on

# 排障三件套(看 AVC 被拒原因)
ausearch -m avc -ts recent
sealert -a /var/log/audit/audit.log            # 给出人类可读建议
audit2why -a

iptables 是内核 netfilter 框架的用户态配置工具(IPv4;IPv6 用 ip6tables)。它按 "表(table)→ 链(chain)→ 规则(rule)" 组织,规则自上而下匹配,先命中先生效。

四表五链:

表

作用

链

raw

连接跟踪豁免(NOTRACK),优先级最高

PREROUTING, OUTPUT

mangle

修改报文(TOS/TTL/MARK)

全部五链

nat

地址转换(SNAT/DNAT/MASQUERADE)

PREROUTING, OUTPUT, POSTROUTING

filter

过滤放行(默认表)

INPUT, FORWARD, OUTPUT

security

配合 SELinux 打安全标签

INPUT, FORWARD, OUTPUT

内置五条链:PREROUTING(入站最先)→ 路由决策 → INPUT / FORWARD → OUTPUT(出站)→ POSTROUTING(出站最后)。

iptables 数据包流转路径:

规则结构:iptables [-t 表] 动作 链 [匹配条件] -j 动作

iptables -t filter -A INPUT -p tcp --dport 22 -s 192.168.1.0/24 -j ACCEPT
#        └表┘  └─┘└─┘   └协议┘  └──目标端口┘ └──源地址┘  └─策略┘



列出当前系统filter table的几条链规则:
[root@localhost ~]# iptables -L -n
列出nat表的链信息:
[root@localhost ~]#  iptables -t nat -L -n

清除规则
清除本机防火墙的所有规则设定:
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -X
[root@localhost ~]# iptables –Z
上面三条指令可以清除防火墙的所有规则,但是不能清除预设的默认规则(policy)

链管理:-A(追加) -I(插入头部) -D(删除) -L(列出) -F(清空) -P(设默认策略)

匹配:-p(协议) -s/-d(源 / 目的地址) --dport/--sport(端口) -i/-o(入 / 出接口) -m(扩展模块:conntrack/state/multiport/comment…)

动作 target:ACCEPT DROP REJECT LOG(记日志继续走)、SNAT DNAT MASQUERADE REDIRECT、MARK、RETURN、跳转到自定义链

# ① 查看
iptables -L -n -v --line-numbers        # 加行号方便删改
iptables -t nat -L -n -v

# ② 基础防护(VPS 标准模板)
iptables -P INPUT DROP                  # 默认拒绝入站
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT   # 回程放行
iptables -A INPUT -i lo -j ACCEPT                                          # 回环
iptables -A INPUT -p tcp --dport 22 -j ACCEPT     # SSH
iptables -A INPUT -p tcp --dport 80 -j ACCEPT     # HTTP
iptables -A INPUT -p tcp --dport 443 -j ACCEPT    # HTTPS
iptables -A INPUT -j LOG --log-prefix "DROP-IN: " --log-level 4           # 记录被拒

# ③ 局域网 NAT 共享上网(SNAT/MASQUERADE)
sysctl -w net.ipv4.ip_forward=1
iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o eth1 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -i eth1 -o eth0 -j ACCEPT

# ④ 端口映射(DNAT,公网 80 → 内网 10.0.0.10:8080)
iptables -t nat -A PREROUTING -d 203.0.113.5 -p tcp --dport 80 \
         -j DNAT --to-destination 10.0.0.10:8080
iptables -t nat -A POSTROUTING -d 10.0.0.10 -p tcp --dport 8080 -j MASQUERADE
iptables -A FORWARD -d 10.0.0.10 -p tcp --dport 8080 -j ACCEPT

# ⑤ 持久化(重启不丢)
# Debian/Ubuntu:  apt install iptables-persistent
iptables-save > /etc/iptables/rules.v4
iptables-restore < /etc/iptables/rules.v4
# RHEL/CentOS 7:  service iptables save(写入 /etc/sysconfig/iptables)

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或者给予支持!