🐧 Linux 命令:每位 DevOps 初学者在部署 EC2 时学习的

发布: (2026年1月4日 GMT+8 13:23)
5 min read
原文: Dev.to

Source: Dev.to

(未提供需要翻译的正文内容。如需翻译,请提供完整的文本。)

🔐 SSH 与远程访问

ssh -i DevOps.pem ec2-user@ec2-x-x-x-x.compute-1.amazonaws.com

目的: 安全连接到 EC2 实例。

  • ssh – 安全外壳(Secure Shell)
  • -i DevOps.pem – 使用此私钥进行身份验证
  • ec2-user@host – 登录用户 + EC2 主机名

在不交互登录的情况下运行命令

ssh user@host "command"

目的: 直接在远程 EC2 实例上执行命令(在 CI/CD 流水线中很有用)。

示例

ssh ec2-user@EC2_HOST "whoami && hostname"

为非交互式 CI/CD 管理已知主机

ssh-keyscan -H EC2_HOST >> ~/.ssh/known_hosts
  • 防止出现 “Are you sure you want to continue connecting?”(是否确认继续连接?)提示。
  • 对自动化脚本是必需的。

更安全的变体(添加超时并且永不导致流水线失败):

ssh-keyscan -T 10 -H EC2_HOST >> ~/.ssh/known_hosts || true

📁 文件系统导航与检查

pwd                # 打印当前目录
ls                 # 列出文件和目录
ls -l              # 长格式(权限、所有者、大小)
ls -a              # 包含隐藏文件
cd                 # 更改目录

示例

cd DevOpsWeb

🔑 文件与目录权限(非常重要)

更改权限

chmod 755 directory
chmod 644 file
  • 7 = 读取 + 写入 + 执行(所有者)
  • 5 = 读取 + 执行(组/其他)
  • 4 = 仅读取

常见用法

  • 允许 Nginx 读取文件
  • 防止出现 403 Forbidden 错误

更改所有者

sudo chown -R ec2-user:nginx /home/ec2-user/DevOpsWeb
  • 所有者 → ec2-user
  • 组 → nginx
  • -R → 递归

以 root 身份运行命令

sudo 以管理员权限运行命令,常用于:

  • 安装软件包
  • 编辑系统配置
  • 重启服务

🌐 Nginx(Web 服务器)

sudo yum install nginx -y          # 安装
sudo systemctl start nginx        # 启动服务
sudo systemctl enable nginx        # 开机自启
sudo systemctl status nginx       # 检查状态
sudo nginx -t                     # 测试配置(在重新加载前运行)
sudo systemctl reload nginx       # 在不中断的情况下重新加载配置

🌍 网络与调试

curl http://localhost               # Test local web server response
curl http://PUBLIC_IP               # Test public access from the instance
lsof -i :80                         # See what process is using port 80

如果 curl http://localhost 正常工作,但浏览器无法访问该站点,请检查:

  • 安全组规则
  • 实例防火墙设置

📦 包管理 (Amazon Linux)

sudo yum install  -y       # Install a package non‑interactively
sudo yum remove  -y        # Remove a package

示例

sudo yum install httpd -y
sudo yum install docker -y
sudo yum remove httpd -y            # Remove Apache to avoid port conflicts

🐳 Docker 基础(至今)

docker run hello-world                     # 验证 Docker 安装
docker build -t devops-website .           # 从 Dockerfile 构建镜像
docker ps -a                               # 列出所有容器
docker exec -it <container> <command>      # 在运行中的容器内执行命令

示例

docker exec -it devopsweb ls /usr/share/nginx/html

允许 ec2-user 在不使用 sudo 的情况下运行 Docker:

sudo usermod -aG docker ec2-user
# 注销并重新登录以使更改生效

8️⃣ Rsync(CI/CD 部署)

rsync -avz --delete source/ user@EC2_HOST:/path/to/dest/
  • -a – 归档模式(保留权限、时间戳等)
  • -v – 冗长输出
  • -z – 压缩传输
  • --delete – 删除目标端已不存在于源端的文件

排除不需要的文件

--exclude='.git*'
--exclude='.github/'

强制目标端的权限

--chmod=Du=rwx,Dgo=rx,Fu=rw,Fgo=r

这些选项帮助解决了部署过程中出现的退出码 23 和权限相关的问题。

🧪 GitHub Actions / CI 命令

set -e          # Exit immediately if a command fails
set -eux        # -e: exit on error, -u: treat unset variables as error, -x: print commands (debug)
mkdir -p ~/.ssh
echo "$SECRET" > ~/.ssh/id_rsa   # Write SSH private key from GitHub Secrets

这些设置使 CI 流水线在出现错误时能够大声且清晰地失败,确保问题能够及早被捕获。

Back to Blog

相关文章

阅读更多 »

如何部署 AWS ELASTIC BEANSTALK

概述:AWS Elastic Beanstalk 是一种托管的云服务,允许您部署和运行 Web 应用程序,而无需担心底层基础设施。我...