Skip to content

SSH私钥权限问题排查与解决 + 服务器连接信息归档

SSH 私钥权限问题排查与解决

问题现象

  • 尝试 SSH 连接服务器 47.105.83.78(别名 aliyun)时,报错:
    Load key "/c/Users/zhuonian/.ssh/id_ed25519": Permission denied
    root@47.105.83.78: Permission denied (publickey,password).
  • 同时 ~/.ssh/config 文件也无法打开,提示"没有权限"

根本原因

Windows 文件的 ACL(访问控制列表)配置不正确。

  • id_ed25519 私钥文件权限过于开放(Unix 视图显示 rw-r--r-- 即 644)
  • SSH 客户端要求私钥必须为 600(仅所有者可读),否则拒绝使用
  • config 文件同样 ACL 异常,导致无法打开

解决方法(在 Windows 本机执行)

步骤1:以管理员身份打开 PowerShell

步骤2:修复 SSH 私钥文件权限

powershell
# 重置 ACL 为默认值
icacls "C:\Users\zhuonian\.ssh\id_ed25519" /reset

# 移除继承,仅授予当前用户读取权限
icacls "C:\Users\zhuonian\.ssh\id_ed25519" /inheritance:r
icacls "C:\Users\zhuonian\.ssh\id_ed25519" /grant:r "%username%:R"

步骤3:修复 config 文件权限(同样方法)

powershell
icacls "C:\Users\zhuonian\.ssh\config" /reset
icacls "C:\Users\zhuonian\.ssh\config" /inheritance:r
icacls "C:\Users\zhuonian\.ssh\config" /grant:r "%username%:R"

步骤4:验证

powershell
icacls "C:\Users\zhuonian\.ssh\id_ed25519"
icacls "C:\Users\zhuonian\.ssh\config"

关键知识点

  • Windows 的权限核心是 ACL,不是 Unix chmod 权限位
  • Cygwin/Git Bash 里 ls -la 显示的权限位不一定反映真实 ACL
  • icacls 是 Windows 原生命令,修改 ACL 最可靠
  • /reset 重置 ACL;/inheritance:r 移除继承;/grant:r 授予权限(R=读取,F=完全控制)

结果

修复后成功通过 SSH 密钥认证登录 47.105.83.78(root@aliyun)。


SSH 连接信息归档

服务器:aliyun(47.105.83.78)

  • 主机名iZm5e676h3wpqf6og0p87yZ
  • 系统:Ubuntu 6.8.0-106-generic,x86_64
  • SSH 配置(本地 ~/.ssh/config):
    Host aliyun
        HostName 47.105.83.78
        User root
        IdentityFile ~/.ssh/id_ed25519
        ServerAliveInterval 60
        ServerAliveCountMax 3
        LocalForward 2017 127.0.0.1:2017
        LocalForward 18789 127.0.0.1:18789
  • 服务器上 SSH 文件
    • ~/.ssh/authorized_keys - 已授权公钥(我的 ed25519 公钥在此)
    • ~/.ssh/config - root 用户 SSH 客户端配置
    • ~/.ssh/github-ssh/ - GitHub SSH 密钥目录
    • /etc/ssh/sshd_config - SSH 服务配置
    • SSH 服务状态:running(active)