SSH文件权限设置 | chmod 600 ~/.ssh/* && chmod 644 ~/.ssh/*.pub && chmod 700 ~/.ssh

SSH文件权限问题

如果.ssh目录的权限配置错误,将会导致以下不利影响:

  • 无法使用公钥登录:如果.ssh目录的权限不正确,如目录权限过于宽松(例如设置为777),则该目录下的文件和文件夹可能会被其他用户访问和修改,这将导致无法使用公钥登录进入该服务器。如使用公钥登陆时,提示如Permission denied (publickey,password).
  • 安全问题:.ssh目录包含用户的私钥和公钥文件,如果目录的权限配置错误,可能会导致这些敏感文件被非法访问和篡改,从而造成安全问题。

为了正确配置.ssh目录及其内涵的文件权限,可以按照以下步骤操作:

  1. 将.ssh目录的权限设置为700:运行以下命令:chmod 700 ~/.ssh
  2. 将私钥文件的权限设置为600:运行以下命令:chmod 600 ~/.ssh/id_rsa
  3. 将公钥文件的权限设置为644:运行以下命令:chmod 644 ~/.ssh/id_rsa.pub

这样做的目的是,只允许当前用户读写.ssh目录和私钥文件,而其他用户只能读取公钥文件,从而保证.ssh目录和密钥文件的安全性。
当然,你可以使用如下命令批量化的处理.ssh权限问题:

chmod 600 ~/.ssh/* && chmod 644 ~/.ssh/*.pub && chmod 700 ~/.ssh

SSH公钥权限究竟是644还是600?

chmod 600 ~/.ssh/id_rsa; chmod 600 ~/.ssh/id_rsa.pub (i.e. chmod u=rw,go= ~/.ssh/id_rsa ~/.ssh/id_rsa.pub) are correct.
chmod 644 ~/.ssh/id_rsa.pub (i.e. chmod a=r,u+w ~/.ssh/id_rsa.pub) would also be correct, but chmod 644 ~/.ssh/id_rsa (i.e. chmod a=r,u+w ~/.ssh/id_rsa) would not be. Your public key can be public, what matters is that your private key is private.
Also your .ssh directory itself must be writable only by you: chmod 700 ~/.ssh or chmod u=rwx,go= ~/.ssh. You of course need to be able to read it and access files in it (execute permission). It isn’t directly harmful if others can read it, but it isn’t useful either.
You don’t need sudo. Don’t use sudo to manipulate your own files, that can only lead to mistakes.
refer to: SSH Key Permissions chmod settings?

chmod 600 ~/.ssh/id_rsachmod 600 ~/.ssh/id_rsa.pub是正确的,这表示只有用户可以读写这两个文件,其他用户无法访问。chmod 644 ~/.ssh/id_rsa.pub也是正确的,这表示任何用户都可以读取这个公钥文件,这并不会影响安全性,关键的是私钥文件必须是私有的
此外,.ssh目录本身必须只能被用户写入:chmod 700 ~/.sshchmod u=rwx,go= ~/.ssh。当然,用户需要能够读取它和访问其中的文件(执行权限)。如果其他用户可以读取它,那么它并不会直接造成危害,但也没有任何用处。
不需要使用sudo。不要使用sudo操作自己的文件,这样只会导致错误。

猜你喜欢

转载自blog.csdn.net/bigbaojian/article/details/130255339