.ssh 文件夹中私钥的权限?

516 linux permissions file-permissions macos

我更改了.ssh文件夹中的权限,现在当我使用使用我的私钥的软件时,我每次都必须输入密码。id_rsa每次使用使用它的应用程序时,我对文件的权限应该是什么?

目前我的权限设置为:

-rw-------@ 1 Jody  staff   114 Nov  4 23:29 config
-rw-------  1 Jody  staff  1743 Oct 21  2009 id_rsa
-rw-------@ 1 Jody  staff   397 Oct 21  2009 id_rsa.pub 
-rw-------@ 1 Jody  staff  3855 Sep 13 22:35 known_hosts
Run Code Online (Sandbox Code Playgroud)

小智 845

通常,您希望权限为:

  • .ssh 目录: 700 (drwx------)
  • 公钥(.pub文件):644 (-rw-r--r--)
  • 私钥(id_rsa):600 (-rw-------)
  • 最后,您的主目录不应由组或其他人(最多755 (drwxr-xr-x))写入。

我假设您的意思是每次都必须输入系统/用户密码,而以前您不必这样做。cdhowie 的响应是假设您在生成密钥时设置了密码/密码,如果您按照他所说的那样设置,则除非您使用 ssh 代理,否则每次都必须输入密码。

  • 我现在已经回到这个帖子大约 30 次了。我不敢相信我想不起来了。 (178认同)
  • 我在其他地方发现,如果使用 authorized_keys 文件,它应该被修改为 640,即 -rw-r----- 。 (20认同)
  • 我在哪里可以在手册页中找到此信息? (10认同)
  • 唯一重要的事情是 .ssh 中的任何内容都不能写入任何其他人,并且其他任何人都无法读取任何密钥。 (10认同)
  • @Cerin 对目录的执行权限授予列出该目录的直接子文件/目录的能力,文件夹内的文件不会“继承”其父文件夹的执行位。 (6认同)
  • `~/.ssh/config` 的正确权限是 `600`。 (4认同)
  • 为什么是700?为什么你需要在 `~/.ssh` 下的任何东西是可执行的? (3认同)
  • 您还可以安全地将 `.ssh` 目录权限保留在 `755 (drwxr-xr-x)`,以及将 `id_*.pub` 和 `authorized_keys` 权限保留在 `644 (-rw-r--r --)`。否则其他用户无法查找您的公钥,但如果他们需要知道您的公钥以授予您访问某些服务的权限,那么能够这样做通常很方便。 (2认同)
  • 至少某些版本的 OpenSSH 需要主目录权限,以便只有用户可以写入主目录。否则,`~/.ssh` 可以被链接替换,并替换为任意的 `authorized_keys` 文件。 (2认同)
  • 我在 Debian Buster 上收到“/home/user/.ssh/id_rsa.pub”的“权限 0644 太开放了。”。 (2认同)

Ale*_*x W 112

我一直在为此苦苦挣扎,终于弄清楚需要什么。将$USER所有位置替换为您要在服务器上登录的 SSH 用户名。如果您尝试登录,因为root您需要使用/root/.ssh等,而不/home/root/.ssh是非 root 用户的登录方式。

  • 服务器上的主目录不应被其他人写入: chmod go-w /home/$USER
  • 服务器上的 SSH 文件夹需要 700 权限: chmod 700 /home/$USER/.ssh
  • Authorized_keys 文件需要 644 权限: chmod 644 /home/$USER/.ssh/authorized_keys
  • 确保user拥有文件/文件夹而不是rootchown user:user authorized_keyschown user:user /home/$USER/.ssh
  • 将生成的公钥(来自ssh-keygenauthorized_keys放在服务器上的用户文件中
  • 确保用户的主目录设置为您期望的目录,并且它包含.ssh您一直在修改的正确文件夹。如果没有,请用于usermod -d /home/$USER $USER解决问题
  • 最后,重启 ssh: service ssh restart
  • 然后确保客户端在本地用户的.ssh文件夹中有公钥和私钥文件并登录:ssh user@host.com

  • +1。“Authorized_keys 文件需要 644 权限”<= 这很重要! (7认同)

Fel*_*rez 38

还要确保您的主目录不能被其他用户写入。

chmod g-w,o-w ~

  • 仅供参考,此命令假定您以用户身份而不是 root 身份登录 (8认同)

Ash*_*dal 26

我将此作为单独的答案发布,因为我希望看到手册页建议转换为权限。

基于手册页引用的摘要(链接在最后):

+------------------------+-------------------------------------+-------------+-------------+
| Directory or File      | Man Page                            | Recommended | Mandatory   |
|                        |                                     | Permissions | Permissions |
+------------------------+-------------------------------------+-------------+-------------+
| ~/.ssh/                | There is no general requirement to  | 700         |             |
|                        | keep the entire contents of this    |             |             |
|                        | directory secret, but the           |             |             |
|                        | recommended permissions are         |             |             |
|                        | read/write/execute for the user,    |             |             |
|                        | and not accessible by others.       |             |             |
+------------------------+-------------------------------------+-------------+-------------+
| ~/.ssh/authorized_keys | This file is not highly sensitive,  | 600         |             |
|                        | but the recommended permissions are |             |             |
|                        | read/write for the user, and not    |             |             |
|                        | accessible by others                |             |             |
+------------------------+-------------------------------------+-------------+-------------+
| ~/.ssh/config          | Because of the potential for abuse, |             | 600         |
|                        | this file must have strict          |             |             |
|                        | permissions: read/write for the     |             |             |
|                        | user, and not accessible by others. |             |             |
|                        | It may be group-writable provided   |             |             |
|                        | that the group in question contains |             |             |
|                        | only the user.                      |             |             |
+------------------------+-------------------------------------+-------------+-------------+
| ~/.ssh/identity        | These files contain sensitive data  |             | 600         |
| ~/.ssh/id_dsa          | and should be readable by the user  |             |             |
| ~/.ssh/id_rsa          | but not accessible by others        |             |             |
|                        | (read/write/execute)                |             |             |
+------------------------+-------------------------------------+-------------+-------------+
| ~/.ssh/identity.pub    | Contains the public key for         | 644         |             |
| ~/.ssh/id_dsa.pub      | authentication.  These files are    |             |             |
| ~/.ssh/id_rsa.pub      | not sensitive and can (but need     |             |             |
|                        | not) be readable by anyone.         |             |             |
+------------------------+-------------------------------------+-------------+-------------+
Run Code Online (Sandbox Code Playgroud)

所有手册页引用均来自http://linuxcommand.org/lc3_man_pages/ssh1.html

  • 我们不要忘记 `known_hosts` > _"~/.ssh/known_hosts 包含用户已登录的所有主机的主机密钥列表,这些主机尚未在系统范围内的已知主机密钥列表中。[...] 该文件应该只能由 root/所有者写入,并且可以但不一定是世界可读的。”_ (https://linux.die.net/man/8/sshd) (6认同)

cdh*_*wie 7

权限应该与此无关。您的私钥是用密码加密的,因此您需要输入密码才能解密和使用私钥。

您可能会考虑运行 ssh 代理,它可以缓存解密的密钥并将它们提供给需要它们的应用程序。

  • 不要低估权限。他们肯定还在发挥作用。 (9认同)
  • 如果您没有私钥的密码(自动远程调用脚本),它对您没有帮助。这里需要权限。 (3认同)

小智 5

Felipe 是正确的——包含您的 .ssh 目录的目录不能被组或其他人写入。因此chmod go-w ~,如果在运行 ssh 后仍然提示您输入密码ssh-keygen -t rsa; cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys,假设您没有在 ssh-keygen 命令中分配密码,并且您的 .ssh 目录在您的主目录中,那么下一个合乎逻辑的尝试是。