当前位置: 首页 > news >正文

普通用户之间免密互信时因权限问题致使配置失败

[student@master ~]$ ssh-copy-id user1@node1
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/student/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
user1@node1's password: 
Permission denied, please try again.
user1@node1's password: 
Permission denied, please try again.
user1@node1's password: 
sh: line 1: .ssh/authorized_keys: Permission denied

从提供的信息来看,问题出在 .ssh 目录的所有权和权限 上:

问题分析

  1. .ssh 目录的所有者是 root,而不是 student

    drwx------  2 root    student   6 Sep 23 18:27 .ssh
    
    • 虽然权限是 700(正确),但 所有者是 root,导致 student 用户无法写入 .ssh/authorized_keys
  2. /etc/sudoers.d/student 配置

    student ALL=(ALL) NOPASSWD:ALL
    
    • 这表示 student 用户可以无密码执行 sudo,但 不影响 .ssh 目录的所有权问题

解决方法

方法 1:修改 .ssh 目录的所有者

  1. 登录 node1 并修复所有权

    ssh root@node1  # 或者直接在 node1 上操作
    chown -R student:student /home/student/.ssh
    
    • -R 确保递归修改 .ssh 及其内容的所有者。
  2. 验证权限

    ls -ld /home/student/.ssh
    

    正确输出

    drwx------ 2 student student 6 Sep 23 18:27 /home/student/.ssh
    
  3. 重新尝试 ssh-copy-id

    ssh-copy-id student@node1
    

方法 2:手动创建 .ssh 目录(如果 chown 不可行)

如果 chown 失败(例如 /home/student 本身属于 root),可以:

  1. student 用户登录并重建 .ssh
    ssh student@node1
    rm -rf ~/.ssh          # 删除错误的目录(确保备份原有数据)
    mkdir ~/.ssh
    chmod 700 ~/.ssh
    touch ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
    exit
    
  2. 重新复制公钥
    ssh-copy-id student@node1
    

根本原因

  • 用户家目录或 .ssh 目录被错误地创建为 root 所有,可能是由于:
    • 使用 root 手动创建了 /home/student/.ssh
    • 通过 sudo 执行了某些操作,导致文件归属错误。

预防措施

  1. 避免直接使用 root 修改用户文件,优先使用 sudo -u student
    sudo -u student mkdir -p /home/student/.ssh
    sudo -u student chmod 700 /home/student/.ssh
    
  2. 检查用户家目录的默认权限,确保 useraddadduser 正确配置。

验证修复

  1. 检查 .ssh 所有权
    ssh root@node1 "ls -ld /home/student/.ssh"
    
    应显示
    drwx------ 2 student student 4096 Sep 23 18:27 /home/student/.ssh
    
  2. 测试免密登录
    ssh student@node1
    
    如果无需密码即可登录,说明问题已解决。

总结

  • 关键问题.ssh 目录属于 root,导致 student 无法写入。
  • 解决方案:使用 chown 修正所有权,或让 student 用户重建 .ssh 目录。
  • 后续建议:避免直接以 root 操作用户文件,使用 sudo -u 指定用户。
http://www.gsyq.cn/news/11081.html

相关文章:

  • 深入解析:[p2p-Magnet] docs | HTTP API与Web界面 | 搜索查询引擎
  • P10004 [集训队互测 2023] Permutation Counting 2
  • java数组拷贝主要有四种方法,浅拷贝
  • 毕赤酵母细胞工厂升级:CRISPR 技术破局传统局限,解锁多基因代谢工程新可能
  • 9月24号
  • linux系统下nginx网站ssl证书自动续签
  • C#使用Bitmap操作图像的基础方法
  • wpf 自定义输入ip地址的文本框 - 详解
  • 《密码系统设计》第三周
  • VS2022 不支持 .NET Framework 4.0 的解决方法
  • P3747 [六省联考 2017] 相逢是问候
  • 构建一个支撑10万设备的虚拟电厂IoT高效的平台
  • B1I、B1C、B2a双频北斗卫星定位芯片AT9850B-F7N-22
  • 2025 国产代码托管新局:Gitee 领跑本土生态 - 实践
  • Wi-Fi技能——网络安全
  • 完整教程:【开源】基于STM32的智能车尾灯
  • 机动数据集ESA
  • Scanner 和if
  • python自动化操作PDF
  • 完整教程:【数据结构】 ArrayList深入解析
  • 完整教程:PyCharm 在 Linux 上的安装指南
  • 分布式 ID 生成方案实战指南:从选型到落地的全场景避坑手册(二) - 指南
  • BUUCTF-babyheap_0ctf_2017题解(含详细过程与思路分析)
  • 【EF Core】框架底层的数据库连接管理
  • iNeuOS工业互联网操作系统,更新计量数据处理方式和在线报表,实现能源管理基础功能
  • 实用指南:Qt的数据库模块介绍,Qt访问SQLite详细示例
  • React自定义同步状态Hook - 详解
  • SpringBootMVC相关内容
  • 小柏实战学习Liunx(图文教程三十五)
  • HarmonyOS 5 通知与语音能力开发实战:从消息推送到智能语音交互