SuperAPI AI 工具集

ssh-key-management

部署与运维

查看项目

更新于 10-15 08:17

技能介绍

SSH 密钥管理(ssh-key-management)

一、用途

该技能用于为 Acquia Cloud 访问添加、生成、列出或删除 SSH 密钥,也适用于因缺少密钥而导致 SSH 访问被拒绝的情况。SSH 密钥可提供对 Acquia Cloud 资源的安全认证,无需输入密码。

使用场景包括:

  • 为 Acquia Cloud 添加或生成 SSH 密钥
  • 列出或删除 SSH 密钥
  • 为 IDE 或 CI/CD 流水线设置 SSH 访问

SSH 密钥允许你:无需密码安全访问 IDE、推送代码到仓库、在服务器上执行远程命令、在脚本或 CI/CD 流水线中自动化部署。

二、主要能力与命令

列出密钥:

acli ssh-key:list

按 IDE 列出密钥:

acli ide:ssh-key:list

交互式生成新 SSH 密钥:

acli ssh-key:create

该命令会提示输入标签(例如 "My Laptop"、"CI Server"),生成公钥/私钥对,将公钥存储到 Acquia 账户,并将私钥保存在本地(通常为 ~/.ssh/id_rsa)。示例输出包含公钥添加成功、私钥保存路径和指纹信息。注意:标签提示是密钥生成并永久添加到账户前的最后一步,按回车前请确认标签正确。

上传已有 SSH 密钥:

acli ssh-key:upload --filepath ~/.ssh/id_rsa.pub --label "Existing Key"

或交互式执行:

acli ssh-key:upload

上传时可跳过等待传播:

acli ssh-key:upload --filepath ~/.ssh/id_rsa.pub --label "My Key" --no-wait

一步生成并上传:

acli ssh-key:create-upload

可带选项:

acli ssh-key:create-upload
--filename=my_key
--password=""
--label="My New Key"
--no-wait

按指纹查看密钥详情:

acli ssh-key:info --fingerprint="ab:cd:ef:12:34:56:78:90"

删除密钥(交互式):

acli ssh-key:delete

非交互式删除:

acli ssh-key:delete --public-key "ab:cd:ef:12:34:56:78:90"

三、在 IDE 中使用 SSH 密钥

设置 SSH 密钥后,可连接 IDE(注意使用 IDE 的 ides-hostname,而不是 Web URL):

ssh -i ~/.ssh/id_rsa user@ide-12345.ides.acquia.com

在 IDE 中为 git 设置 SSH(替代需要密码认证的 HTTPS):

git remote set-url origin git@github.com:yourorg/yourrepo.git git push origin main

导出公钥用于其他系统:

cat ~/.ssh/id_rsa.pub

将公钥复制到 GitLab、GitHub、内部系统等。

四、最佳实践

  1. 每台设备一个密钥:为个人笔记本、工作台式机和 CI/CD 服务器分别创建密钥,例如:

acli ssh-key:create --label "Personal MacBook" acli ssh-key:create --label "Work Desktop" acli ssh-key:create --label "Jenkins CI Server"

  1. 使用描述性标签:推荐 "2024 MacBook Pro - Home"、"GitHub Actions CI";避免 "Key 1" 之类。

  2. 撤销不再使用的旧密钥:acli ssh-key:delete

  3. 保护私钥:权限应为 600(仅所有者可读写):chmod 600 ~/.ssh/id_rsa

  4. 使用 SSH 代理。macOS:在 ~/.ssh/config 中添加 Host *、AddKeysToAgent yes、UseKeychain yes。Linux:eval "$(ssh-agent -s)" 然后 ssh-add ~/.ssh/id_rsa。

五、高级:用于 CI/CD

为 CI/CD 流水线生成密钥:

acli ssh-key:create --label "GitHub Actions CI"

然后复制私钥,添加到 CI/CD 提供方的密钥管理(如 GitHub Secrets、GitLab CI/CD Variables 等),并在 CI 脚本中使用。GitHub Actions 示例:

  • name: Deploy to Acquia uses: actions/checkout@v2 env: SSH_KEY: ${{ secrets.ACQUIA_SSH_KEY }} run: | mkdir -p ~/.ssh echo "$SSH_KEY" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa acli api:environments:code-switch <env-id> main

MEO (V3) 订阅:使用 acli api:v3:environments:create-deployment <environmentId> true refs/heads/main 替代 api:environments:code-switch(参见 MEO Deployments)。

六、适用场景与限制

适用场景:本地开发机、工作台式机、CI/CD 服务器需要访问 Acquia Cloud;IDE 免密访问;SSH 访问因缺少密钥被拒绝时。

限制:以上命令依赖 acli 工具;具体平台支持范围、收费或价格信息资料未提供;文档未保证特定使用效果。