·18 分钟阅读·管理员
如何使用服务器自动化部署
本文详细指导如何为GitHub Actions自动化部署创建专用用户及组,涵盖用户组创建、SSH密钥配置、目录权限设置(含setgid)、sudo精细化授权、GitHub Secrets配置及安全加固等核心步骤,强调最小权限原则与生产环境安全性。
部署
服务器自动化部署用户配置指南
概述
如何创建一个专门的部署用户和组,用于 GitHub Actions 自动化部署,避免使用 root 权限。
一、用户和组规划
创建部署组
bash
# 创建部署组
sudo groupadd deployers
# 验证组是否创建成功
getent group deployers
创建部署用户
bash
# 创建用户并指定组、shell、home 目录
sudo useradd -m -s /bin/bash -G deployers github_user
# 设置用户密码(可选,主要用于 sudo 验证)
sudo passwd github_user
# 验证用户创建成功
id github_user
二、SSH 密钥配置
在服务器上生成密钥对(推荐)
bash
# 切换到部署用户
sudo -u github_user ssh-keygen -t ed25519 -C "github_actions_deploy" -f /home/github_user/.ssh/id_ed25519 -N ""
# 或者使用 RSA(兼容性更好)
sudo -u github_user ssh-keygen -t rsa -b 4096 -C "github_actions_deploy" -f /home/github_user/.ssh/id_rsa -N ""
配置 authorized_keys
bash
# 创建 .ssh 目录
sudo mkdir -p /home/github_user/.ssh
sudo chmod 700 /home/github_user/.ssh
sudo chown github_user:github_user /home/github_user/.ssh
# 复制公钥到 authorized_keys
sudo cp /home/github_user/.ssh/id_ed25519.pub /home/github_user/.ssh/authorized_keys
sudo chmod 600 /home/github_user/.ssh/authorized_keys
sudo chown github_user:github_user /home/github_user/.ssh/authorized_keys
或者:使用本地机器生成的密钥
如果您的本地机器已有密钥对,可以将公钥添加到服务器:
bash
# 从本地复制公钥到服务器
scp ~/.ssh/id_ed25519.pub user@your-server:/tmp/github_deploy_key.pub
# 在服务器上添加到 authorized_keys
sudo mkdir -p /home/github_user/.ssh
sudo touch /home/github_user/.ssh/authorized_keys
sudo chmod 600 /home/github_user/.ssh/authorized_keys
sudo chown github_user:github_user /home/github_user/.ssh/authorized_keys
sudo cat /tmp/github_deploy_key.pub | sudo tee -a /home/github_user/.ssh/authorized_keys
获取私钥内容(用于 GitHub Secrets)
bash
# 显示私钥内容(复制用于 GitHub Secrets)
sudo cat /home/github_user/.ssh/id_ed25519
# 或 RSA
sudo cat /home/github_user/.ssh/id_rsa
三、目录权限配置
配置 deployers 组对 /www/wwwroot 的权限
bash
# 将 deployers 组添加到 /www/wwwroot 目录的所有者组
sudo chgrp -R deployers /www/wwwroot
# 设置目录权限:所有者读写执行,组读写执行,其他读执行
sudo chmod -R 775 /www/wwwroot
# 确保新建文件继承组权限(设置 setgid)
sudo find /www/wwwroot -type d -exec chmod g+s {} \;
# 验证设置
ls -la /www/wwwroot
验证组权限
bash
# 验证目录所属组
ls -ld /www/wwwroot
# 应显示类似:drwxrwsr-x 2 root deployers 4096 ...
# 注意:s 标志表示 setgid 已设置
为未来部署用户预留
之后如需添加更多部署用户,只需:
bash
# 创建新用户并加入 deployers 组
sudo useradd -m -s /bin/bash -G deployers another_deploy_user
# 设置 SSH(重复第二章步骤即可)
将已有用户添加到部署组
如果用户 github_user 已经存在,需要添加到 deployers 组:
bash
# 将已有用户添加到组(append,不会移除其他组)
sudo usermod -aG deployers github_user
# 验证
groups github_user
# 或
id github_user
注意:
- 必须使用
-a参数(append),否则会替换用户的所有组为deployers - 更改后无需重新登录,当前会话可使用
newgrp deployers立即生效
如果同时添加多个组:
bash
sudo usermod -aG deployers,www-data github_user
四、Sudo 配置(如需系统级操作)
场景 1:使用 PM2 管理服务(推荐)
bash
# 编辑 sudoers
sudo visudo
添加以下内容:
sudo
# GitHub Actions 部署用户 PM2 权限
github_user ALL=(ALL) NOPASSWD: /usr/bin/pm2 restart leisure-article
github_user ALL=(ALL) NOPASSWD: /usr/bin/pm2 start leisure-article
github_user ALL=(ALL) NOPASSWD: /usr/bin/pm2 reload leisure-article
github_user ALL=(ALL) NOPASSWD: /usr/bin/pm2 list
github_user ALL=(ALL) NOPASSWD: /usr/bin/pm2 logs leisure-article
场景 2:使用 Systemd 管理服务
sudo
# GitHub Actions 部署用户 Systemd 权限
github_user ALL=(ALL) NOPASSWD: /bin/systemctl restart leisure-article
github_user ALL=(ALL) NOPASSWD: /bin/systemctl start leisure-article
github_user ALL=(ALL) NOPASSWD: /bin/systemctl stop leisure-article
github_user ALL=(ALL) NOPASSWD: /bin/systemctl status leisure-article
场景 3:使用 Docker
sudo
# GitHub Actions 部署用户 Docker 权限
github_user ALL=(ALL) NOPASSWD: /usr/bin/docker-compose restart
github_user ALL=(ALL) NOPASSWD: /usr/bin/docker-compose pull
github_user ALL=(ALL) NOPASSWD: /usr/bin/docker-compose up -d
测试 sudo 配置
bash
# 切换到部署用户测试
sudo -u github_user sudo -l
# 测试具体命令
sudo -u github_user sudo pm2 list
五、GitHub Secrets 配置
在 GitHub 仓库中配置以下 Secrets(Settings → Secrets and variables → Actions):
| Secret 名称 | 说明 | 示例值 |
|---|---|---|
DEPLOY_HOST |
服务器 IP 或域名 | 192.168.1.100 或 example.com |
DEPLOY_USER |
部署用户名 | github_user |
DEPLOY_SSH_KEY |
SSH 私钥内容 | -----BEGIN OPENSSH PRIVATE KEY-----... |
DEPLOY_PORT |
SSH 端口(可选) | 22 |
DEPLOY_PATH |
部署目标路径 | /www/wwwroot/leisure-article |
六、SSH 安全加固(可选但推荐)
限制 SSH 用户仅能执行特定命令
编辑 authorized_keys:
bash
sudo nano /home/github_user/.ssh/authorized_keys
添加限制(将公钥替换为以下格式):
command="cd /www/wwwroot/leisure-article && /usr/bin/bash",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI...
参数说明:
command: 强制执行的命令no-port-forwarding: 禁用端口转发no-X11-forwarding: 禁用 X11 转发no-agent-forwarding: 禁用 SSH agent 转发no-pty: 禁用 PTY 分配
禁用密码登录
编辑 SSH 配置:
bash
sudo nano /etc/ssh/sshd_config
添加或修改:
# 禁用密码登录
PasswordAuthentication no
# 禁用 root 登录
PermitRootLogin no
# 仅允许特定用户
AllowUsers github_user
重启 SSH 服务:
bash
sudo systemctl restart sshd
七、验证部署
测试 SSH 连接
从本地测试:
bash
# 使用私钥测试连接
ssh -i ~/.ssh/id_ed25519 github_user@your-server
# 或直接测试部署命令
ssh -i ~/.ssh/id_ed25519 github_user@your-server "cd /www/wwwroot/leisure-article && pwd"
测试文件上传权限
bash
# 在本地创建测试文件
echo "test" > test.txt
# 测试上传
scp -i ~/.ssh/id_ed25519 test.txt github_user@your-server:/www/wwwroot/leisure-article/
# 验证
ssh -i ~/.ssh/id_ed25519 github_user@your-server "cat /www/wwwroot/leisure-article/test.txt"
触发 GitHub Actions 部署
推送代码到 master 分支,观察部署流程是否成功。
八、故障排查
问题 1:Permission denied
bash
# 检查文件权限
ls -la /www/wwwroot/leisure-article
# 重新设置权限
sudo chown -R :deployers /www/wwwroot/leisure-article
sudo chmod -R 775 /www/wwwroot/leisure-article
问题 2:SSH 连接失败
bash
# 检查 SSH 日志
sudo tail -f /var/log/auth.log
# 检查 .ssh 目录权限
ls -la /home/github_user/.ssh
# 修复权限
sudo chmod 700 /home/github_user/.ssh
sudo chmod 600 /home/github_user/.ssh/authorized_keys
问题 3:PM2 命令需要 sudo
bash
# 验证 sudo 配置
sudo -u github_user sudo -l
# 确保 PM2 可执行
which pm2
九、完整配置脚本(一键执行)
将以下内容保存为 setup-deploy-user.sh,然后在服务器上执行:
bash
#!/bin/bash
# 配置变量
DEPLOY_USER="github_user"
DEPLOY_GROUP="deployers"
DEPLOY_PATH="/www/wwwroot"
echo "=== 开始配置部署用户 ==="
# 1. 创建组
echo "步骤 1: 创建部署组..."
sudo groupadd $DEPLOY_GROUP 2>/dev/null || echo "组 $DEPLOY_GROUP 已存在"
# 2. 创建用户
echo "步骤 2: 创建部署用户..."
sudo useradd -m -s /bin/bash -G $DEPLOY_GROUP $DEPLOY_USER 2>/dev/null || echo "用户 $DEPLOY_USER 已存在"
# 3. 配置 SSH
echo "步骤 3: 配置 SSH..."
sudo mkdir -p /home/$DEPLOY_USER/.ssh
sudo chmod 700 /home/$DEPLOY_USER/.ssh
sudo chown $DEPLOY_USER:$DEPLOY_USER /home/$DEPLOY_USER/.ssh
sudo -u $DEPLOY_USER ssh-keygen -t ed25519 -C "github_actions_deploy" -f /home/$DEPLOY_USER/.ssh/id_ed25519 -N "" -q
sudo cp /home/$DEPLOY_USER/.ssh/id_ed25519.pub /home/$DEPLOY_USER/.ssh/authorized_keys
sudo chmod 600 /home/$DEPLOY_USER/.ssh/authorized_keys
sudo chown $DEPLOY_USER:$DEPLOY_USER /home/$DEPLOY_USER/.ssh/authorized_keys
# 4. 配置目录权限
echo "步骤 4: 配置目录权限..."
sudo chgrp -R $DEPLOY_GROUP $DEPLOY_PATH
sudo chmod -R 775 $DEPLOY_PATH
sudo find $DEPLOY_PATH -type d -exec chmod g+s {} \;
# 5. 配置 Sudo
echo "步骤 5: 配置 Sudo..."
echo "# GitHub Actions 部署用户权限" | sudo tee -a /etc/sudoers.d/$DEPLOY_USER
echo "$DEPLOY_USER ALL=(ALL) NOPASSWD: /usr/bin/pm2 restart leisure-article" | sudo tee -a /etc/sudoers.d/$DEPLOY_USER
echo "$DEPLOY_USER ALL=(ALL) NOPASSWD: /usr/bin/pm2 start leisure-article" | sudo tee -a /etc/sudoers.d/$DEPLOY_USER
echo "$DEPLOY_USER ALL=(ALL) NOPASSWD: /usr/bin/pm2 reload leisure-article" | sudo tee -a /etc/sudoers.d/$DEPLOY_USER
sudo chmod 440 /etc/sudoers.d/$DEPLOY_USER
echo "=== 配置完成 ==="
echo ""
echo "私钥内容(复制到 GitHub Secrets > DEPLOY_SSH_KEY):"
echo "----------------------------------------"
sudo cat /home/$DEPLOY_USER/.ssh/id_ed25519
echo "----------------------------------------"
echo ""
echo "请在 GitHub 配置以下 Secrets:"
echo " DEPLOY_HOST: 服务器地址"
echo " DEPLOY_USER: $DEPLOY_USER"
echo " DEPLOY_SSH_KEY: (上面的私钥内容)"
echo " DEPLOY_PORT: 22"
echo " DEPLOY_PATH: /www/wwwroot/leisure-article"
执行脚本:
bash
chmod +x setup-deploy-user.sh
sudo ./setup-deploy-user.sh
十、参考信息
目录权限说明
| 权限 | 数值 | 说明 |
|---|---|---|
| rwxrwxr-x | 775 | 所有者读写执行,组读写执行,其他读执行 |
| drwxrwsr-x | 2775 | 同上,且设置 setgid(新文件继承组) |
Sudo 权限级别
| 级别 | 配置 | 安全性 |
|---|---|---|
| 低 | ALL=(ALL) NOPASSWD: ALL |
不安全 |
| 中 | ALL=(ALL) ALL |
需要密码 |
| 高 | command-specific |
仅限特定命令(推荐) |