ssh authorized_keys 文件位置和权限

ssh authorized_keys 文件位置和权限

我对 ssh 在处理自定义时所需的权限感到困惑授权密钥文件。

例如,假设我们有一个服务器,文件中有以下行/etc/ssh/sshd_config

AuthorizedKeysFile .ssh/authorized_keys /etc/ssh/authorized_keys

这意味着理论上我们可以使用插入这两个文件中的所有密钥来访问服务器,考虑到.ssh/授权密钥是每个用户的文件(这意味着我们可以使用文件中的密钥以 root 用户身份登录授权密钥/home/root/authorized_keys)。

现在,这对于非 root 用户来说如何工作授权密钥,如果使用默认模式时StrictMode yes不允许使用授权密钥文件,除非父文件夹具有权限 0700 并由用户拥有(这里不是这种情况)并且该文件由用户拥有并具有权限 0600(我们可以假设是这种情况)?

Ssh 不会接受密钥授权密钥对于非 root 用户,因为该用户的权限不正确(所有者/etc/ssh)。

或者我遗漏了什么?

答案1

@dave_thompson_085 对这个问题的评论确实切中要害,因此对此进行了扩展,以免这个问题得不到正式的答案:

misc.c,函数safe_path()状态,截至 2023-06:

/*
 * Check a given path for security. This is defined as all components
 * of the path to the file must be owned by either the owner of
 * of the file or root and no directories must be group or world writable.
 *
 * XXX Should any specific check be done for sym links ?
 *
 * Takes a file name, its stat information (preferably from fstat() to
 * avoid races), the uid of the expected owner, their home directory and an
 * error buffer plus max size as arguments.
 *
 * Returns 0 on success and -1 on failure
 */
int
safe_path(const char *name, struct stat *stp, const char *pw_dir,
    uid_t uid, char *err, size_t errlen)
{
    ...
    if ((!platform_sys_dir_uid(stp->st_uid) && stp->st_uid != uid) ||
        (stp->st_mode & 022) != 0) {
        snprintf(err, errlen, "bad ownership or modes for file %s",
            buf);
        return -1;
    }
    ...
    (similarly to each path component)
}

该函数由包装器调用safe_path_fd(),如果同时考虑两者,它们的用法authorized_keys似乎是auth_openfile()auth2-pubkeyfile.c

static FILE *
auth_openfile(const char *file, struct passwd *pw, int strict_modes,
    int log_missing, char *file_type)
{
...
    if (strict_modes &&
        safe_path_fd(fileno(f), file, pw, line, sizeof(line)) != 0) {
        fclose(f);
        logit("Authentication refused: %s", line);
        auth_debug_add("Ignored %s: %s", file_type, line);
        return NULL;
    }
...
}

从哪里来strict_modesStrictModes选择sshd_config

最后,回答你的问题:

现在,如果 [...] 不允许使用 [...],除非父文件夹具有权限 0700 并由用户 [...] 拥有,并且文件由用户拥有并具有权限 0600,那么非 root 用户如何才能使用 /etc/ssh/authorized_keys?还是我遗漏了什么?

你误解了sshd documentation

~/.ssh/authorized_keys
        Lists the public keys (DSA, ECDSA, Ed25519, RSA) that can
        be used for logging in as this user.  The format of this
        file is described above.  The content of the file is not
        highly sensitive, but the recommended permissions are
        read/write for the user, and not accessible by others.

        If this file, the ~/.ssh directory, or the user's home
        directory are writable by other users, then the file could
        be modified or replaced by unauthorized users.  In this
        case, sshd will not allow it to be used unless the
        StrictModes option has been set to “no”.

注意永远不会需要该文件严格为 0600 authorized_keys,它只是建议它。对于目录,它只需要每个组件 不是世界(或群体)可写,所以它们可以是 0755。这就是为什么代码只检查022 面具

并且,查看代码,文件系统和用户root都始终允许作为所有者,因此甚至可以(记住 sshd 进程sshdauthorized_keys0000 ---------- root:root运行以 root 身份登录)。不幸的是,手册中从未明确提到这一点,但另一方面,它也没有说过文件所有者和登录用户必须匹配(代码是匹配的)。

相关内容