# Vps Security Hardening

VPS 安全加固自动化 Skill。通过 SSH 登录 VPS 并执行完整的安全策略配置。 触发场景： - "加固VPS安全"、"VPS安全配置"、"新VPS初始化" -...

- Repository: https://github.com/wlzh/skills (694 stars)
- Category: DevOps
- License: MIT
- Last updated: 2026-10-27
- Runs scripts: yes, review before use

## Install

Claude Code: `npx skills add wlzh/skills --skill vps-security-hardening -g -a claude-code -y`
Codex: `npx skills add wlzh/skills --skill vps-security-hardening -g -a codex -y`
Cursor: `npx skills add wlzh/skills --skill vps-security-hardening -g -a cursor -y`
Gemini CLI: `npx skills add wlzh/skills --skill vps-security-hardening -g -a gemini-cli -y`

## Description

VPS 安全加固自动化 Skill。通过 SSH 登录 VPS 并执行完整的安全策略配置。 触发场景： - "加固VPS安全"、"VPS安全配置"、"新VPS初始化" - "配置SSH安全"、"修改SSH端口"、"禁用root密码登录" - "VPS安全加固"、"服务器安全设置"、"hardening" 功能（7招安全加固）： 1. 创建 sudo 用户，禁用 root 密码登录 2. 修改 SSH 端口（支持 Ubuntu 不同版本） 3. Fail2ban 自动安装配置 4. SSH 密钥登录支持 5. SSH 登录通知（可选） 6. UFW 防火墙配置 7. Docker 安全提醒 Author: github.com/wlzh Version: 1.0.0

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# VPS 安全加固 Skill

> 版本: 1.0.7 | 作者: github.com/wlzh | 参考: https://x.com/gxjdian/status/2033751314208059507

## 概述

本 Skill 用于自动化 VPS 安全加固流程，基于「7招安全加固」最佳实践，通过 SSH 远程执行一系列安全配置命令。

## ⚠️ 重要警告

**在执行任何操作前，务必确保：**

1. **VPS 平台防火墙已开放新的 SSH 端口** - 否则将被锁死无法登录！
2. 如使用云服务商（AWS/阿里云/腾讯云等），需在安全组/防火墙规则中放行端口
3. 建议先保持原 22 端口连接，新开一个终端测试新端口成功后再关闭 22

## 🔴 前置条件：开启 root 密码登录

**如果 VPS 不支持 root 密码登录，必须先通过 VNC/控制台 开启！**

很多云服务商（AWS、阿里云等）默认禁用 root 密码登录，只允许密钥登录。在运行此 Skill 前，需要先开启：

**通过 VPS 平台控制台（VNC）执行以下命令：**

```bash
# 1. 开启密码认证
sed -i 's/^#\?PasswordAuthentication.*/PasswordAuthentication yes/' /etc/ssh/sshd_config

# 2. 开启 root 登录
sed -i 's/^#\?PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config

# 3. 重启 SSH 服务
systemctl restart ssh

# 4. 设置 root 密码（会提示输入两次）
passwd root
```

**执行完成后，才能使用此 Skill 进行 SSH 登录和后续配置。**

## 所需信息（执行时收集）

运行此 Skill 时，需要用户提供：

| 参数 | 说明 | 示例 |
|------|------|------|
| `VPS_IP` | VPS 的 IP 地址 | `192.168.1.100` |
| `ROOT_PASSWORD` | root 密码 | `MyP@ssw0rd` |
| `NEW_USER` | 新建的 sudo 用户名 | `admin` |
| `NEW_USER_PASSWORD` | 新用户密码 | `UserP@ss123` |
| `SSH_PORT` | 新的 SSH 端口 | `22222` |

## 执行流程

### Phase 0: 环境检查

```bash
# 检查本地是否安装 sshpass（用于自动输入密码）
which sshpass || echo "需要安装 sshpass: brew install sshpass 或 apt install sshpass"
```

### Phase 1: SSH 连接与系统检测

1. **检测 root 密码登录是否开启**
2. **检测 Ubuntu 版本**（影响 SSH 配置方式）
3. **登录后执行系统更新**

```bash
# 检测 Ubuntu 版本
lsb_release -a

# 更新系统
apt update && apt upgrade -y

# 检查必要工具
which ufw || apt install ufw -y
which sudo || apt install sudo -y
which fail2ban-client || apt install fail2ban -y
```

**Ubuntu 版本与 SSH 配置方式：**

| Ubuntu 版本 | SSH 配置方式 |
|-------------|-------------|
| 22.10, 23.04, 23.10 | socket 激活，需配置 `/etc/systemd/system/ssh.socket.d/` |
| 24.04+ | 直接修改 `/etc/ssh/sshd_config` 或 `sshd_config.d/` |

### Phase 2: 创建 Sudo 用户（第一招）

```bash
# 创建用户
useradd -m -G sudo -s /bin/bash ${NEW_USER}

# 设置密码
echo "${NEW_USER}:${NEW_USER_PASSWORD}" | chpasswd

# 验证用户创建成功
id ${NEW_USER}
```

### Phase 3: 配置 SSH 安全设置（第二招）

The rest: https://github.com/wlzh/skills/blob/HEAD/vps-security-hardening/SKILL.md
