答案:该错误因SSH密钥未配置或无效导致,需生成密钥并添加公钥到GitHub。1. 检查或生成SSH密钥;2. 将公钥添加至GitHub账户;3. 运行ssh -T测试连接;4. 可选使用HTTPS协议;5. 确保SSH agent运行且权限正确。

出现 "git@github.com: Permission denied (publickey)" 错误,是因为 Composer 尝试通过 SSH 协议从 GitHub 拉取代码时,系统无法找到有效的 SSH 私钥或未正确配置密钥认证。以下是解决方法:
1. 检查是否已生成 SSH 密钥
打开终端(Linux/macOS)或 Git Bash(Windows),运行以下命令查看是否存在 SSH 密钥:ls -al ~/.ssh/
id_rsa 和 id_rsa.pub(或 id_ed25519 等),说明已有密钥。如果没有,执行以下命令生成:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
2. 将 SSH 公钥添加到 GitHub 账户
将公钥内容复制到剪贴板:cat ~/.ssh/id_rsa.pub
- Settings → SSH and GPG keys → New SSH key
- 标题自定义(如 "My Laptop")
- 粘贴公钥内容
- 点击 "Add SSH key"
3. 测试 SSH 连接
在终端运行:ssh -T git@github.com
Hi username! You've successfully authenticated...,说明配置成功。
4. 使用 HTTPS 替代 SSH(可选临时方案)
如果你不想处理 SSH,可以让 Composer 使用 HTTPS 协议拉取包:composer config -g github-protocols https
"config": { "github-protocols": ["https"] }
5. 确保 SSH agent 正在运行(Windows 常见问题)
在 Git Bash 中启动 agent 并添加密钥:eval $(ssh-agent -s)ssh-add ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsachmod 644 ~/.ssh/id_rsa.pub










