返回提示词目录第 1318 页

服务器安全加固与验证

按发行版和服务需求编排加固步骤,提供执行命令、验证与回滚方法,降低访问中断风险。

完整提示词

<context>
Hardening guides are long and unordered, and the order is what causes outages: a firewall enabled before SSH is allowed, password login disabled before key login was tested, SELinux or AppArmor switched off to make an app work, and sysctl values copied from a decade-old blog that break networking. A useful hardening pass removes the most exposure first, changes one thing at a time, proves it can still be reached after each change, and leaves the service doing its job. Benchmarks such as the CIS benchmarks go deeper and are the reference for audits.
</context>

<task>
Harden a {{distribution}} server.
{{#services}}
It must keep providing:
{{services}}
{{/services}}

1. If you do not know which services and ports must stay reachable or how administrators log in, ask and stop; hardening without that list breaks things.
2. **Before you start:** a snapshot or backup, and out-of-band console access from the provider confirmed working.
3. Steps, in this order, each with the reason, the commands for {{distribution}}, a "Check:" line and a "Rollback:" line:
   1. Apply all updates and reboot if the kernel changed.
   2. Named admin accounts with sudo; no shared logins; lock unused accounts.
   3. SSH: key-only authentication, no root login, an `AllowUsers` or `AllowGroups` list, a short `LoginGraceTime`. Put the settings in a drop-in that sorts first in `/etc/ssh/sshd_config.d/`, because the first value read wins and cloud images often ship a file there that re-enables password login. Validate with `sshd -t`, confirm the effective values with `sshd -T`, and test a new session before closing the current one.
   4. Firewall default-deny inbound, allowing SSH (ideally from known addresses) and the listed services, using the distribution's tool (ufw, firewalld or nftables). Note that Docker publishes ports around ufw and how to handle it if containers run.
   5. Automatic security updates and how reboots are handled.
   6. Remove or disable what is not needed: list listening sockets (`ss -tulpn`) and enabled units, and disable anything not on the service list.
   7. Brute-force protection for SSH (fail2ban or sshguard) if SSH is reachable from the internet.
   8. Time sync, persistent journald with size limits, auditd with a small rule set for authentication, sudo and changes to users and SSH config, and shipping logs off the host if possible.
   9. Keep SELinux or AppArmor enforcing; show how to read denials and fix policy instead of disabling it.
   10. Service sandboxing for the server's own systemd units (`NoNewPrivileges`, `ProtectSystem`, `PrivateTmp`, a dedicated user), and file permissions on secrets.
   11. A small set of kernel parameters with a reason each (for example `kernel.kptr_restrict`, reverse-path filtering, ignoring ICMP redirects), nothing that changes networking the services rely on.
4. Finish with a scan (Lynis or the distribution's OpenSCAP profile) and say how to read its output.
</task>

<constraints>
- One change at a time, each verified; never batch SSH and firewall changes together.
- Do not change the SSH port as a security measure in place of keys; mention it only as noise reduction.
- Use commands and package names that exist on {{distribution}}; if unsure, say so.
- Do not install agents, scanners or repositories beyond what a step needs.
</constraints>

<output_format>
## Before you start
Checklist.
## Steps
The numbered steps with commands, "Check:" and "Rollback:" lines.
## Service notes
Anything specific to the listed services (ports, users, sandboxing options).
## Verify
A final checklist of what should now be true, with commands.
## Not covered
Bullets: what full CIS-level hardening, intrusion detection or compliance would add.
</output_format>

可替换内容

  • {{distribution}}
  • {{#services}}
  • {{services}}
  • {{/services}}

分类与标签

编程开发、服务器运维、安全加固、远程登录、防火墙、回滚验证

前往 AI 图片创作