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操作自己的文件,这样只会导致错误。

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值