• 今天linux学习创建新用户和设置读写权的时候遇到了一个问题,在root用户下创建一个新用户后(wyf1),用su 切换用户,连基础的ls 命令都报错了,问题还原如下:

    • 创建新用户 wyf1  (此处指令省略)

    • [root@wyflinux ~]# id wyf1   # 新用户信息,创建成功
      uid=1000(wyf1) gid=1000(wyf1) groups=1000(wyf1)

    • [root@wyflinux ~]# chmod 777 test1.txt 
      [root@wyflinux ~]# ls -l test1.txt
      -rwxrwxrwx 1 root root 14 Aug 31 04:11 test1.txt  # 配置 test1.txt文件权限为全部用户可读写
      [root@wyflinux ~]# su wyf1  # 切换到 wyf1 用户
      [wyf1@wyflinux root]$ ls -l test1.txt
      ls: cannot access 'test1.txt': Permission denied # ls 命令报错

    • 其实这个问题的原因在于,其它用户是不能在root目录下操作的,

    • [wyf1@wyflinux root]$ ls -l test1.txt # 因为在还是在 root 的目录下 所以报错,这个时候要先cd返回主目录,才能操作

    • [wyf1@wyflinux root]$ cd
      [wyf1@wyflinux ~]$ ls  
      [wyf1@wyflinux ~]$ ls -l  # 返回主目录后,ls 就可以执行
      total 0                      

    • 同理,因为其它用户无法在root下操作,所以即使前文中对“test1.txt”这个文件做了权限配置,wyf1 用户也无法做任何读写,因为此文件还在根目录下

    • [root@wyflinux ~]# chmod 777 test1.txt  #此时的文件仍在根目录下 
      [root@wyflinux ~]# ls -l test1.txt
      -rwxrwxrwx 1 root root 14 Aug 31 04:11 test1.txt  # 配置 test1.txt文件权限为全部用户可读写
  • 要解决这个问题,首先要把test1.txt这个文件移动到其它目录下

  • [root@wyflinux ~]# mv test1.txt /tmp/  # 把文件移动到 /tmp/ 目录下
    [root@wyflinux ~]# cd /tmp/  
    [root@wyflinux tmp]# ls 
    AliyunAssistClientSingleLock.lock  systemd-private-a63b0f54ab64483da7ecfa94742378dd-chronyd.service-6eItYg  test1.txt  

  • [root@wyflinux tmp]# su wyf1 # 切换至 wyf1 ,注意,此时是在tmp目录下

  • [wyf1@wyflinux tmp]$ ls # 此时ls 命令可以使用了 并且能够看到下方的test1.txt 文件

  • AliyunAssistClientSingleLock.lock  systemd-private-a63b0f54ab64483da7ecfa94742378dd-chronyd.service-6eItYg  test1.txt
    [wyf1@wyflinux tmp]$ ls -l

  • # 检查wyf1 对于test1 的权限,注:因为自己中间对权限做了重新配置,所以下面 test1.txt 的权限不再是777全体读写

  • total 4

  • -rw------- 1 root root  0 Aug 30 11:39 AliyunAssistClientSingleLock.lock
    drwx------ 3 root root 17 Aug 30 11:47 systemd-private-a63b0f54ab64483da7ecfa94742378dd-chronyd.service-6eItYg
    -rwxrw-rw- 1 root root 30 Aug 31 11:53 test1.txt
    [wyf1@wyflinux tmp]$ vi test1.txt # 以wyf1用户身份,对test1 进行编辑
    www.wyf1.net
    2022/8/30/22:52

    ~
    ~

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐