Linux 文件权限管理

柔情只为你懂 2022-02-23 16:17 387阅读 0赞

前言

之前在现场部署的时候, 某些用户没有root用户的权限. 由此学习了一波Linux权限相关的知识. 最近有机会, 将其总结如下:

  • 用户与用户组
  • chrown/chmod/ 命令
  • -R/755/777 的含义
  • 注意事项

正文

用户与用户组

用户与用户组是多对多的关系. .一个用户可以在多个用户组内, 一个用户组也可以包含多个用户. 我们通常可以通过/etc/passwd文件查看相关的信息.

  1. localhost:~ Sean$ cat /etc/passwd
  2. ##
  3. # User Database
  4. #
  5. # Note that this file is consulted directly only when the system is running
  6. # in single-user mode. At other times this information is provided by
  7. # Open Directory.
  8. #
  9. # See the opendirectoryd(8) man page for additional information about
  10. # Open Directory.
  11. ##
  12. nobody:*:-2:-2:Unprivileged User:/var/empty:/usr/bin/false
  13. root:*:0:0:System Administrator:/var/root:/bin/sh
  14. daemon:*:1:1:System Services:/var/root:/usr/bin/false

用户组的相关信息一般存储在/etc/group文件内:

  1. localhost:~ Sean$ cat /etc/group
  2. ##
  3. # Group Database
  4. #
  5. # Note that this file is consulted directly only when the system is running
  6. # in single-user mode. At other times this information is provided by
  7. # Open Directory.
  8. #
  9. # See the opendirectoryd(8) man page for additional information about
  10. # Open Directory.
  11. ##
  12. nobody:*:-2:
  13. nogroup:*:-1:
  14. wheel:*:0:root
  15. daemon:*:1:root

其他相关操作: Linux 用户和用户组管理

chmod/chown

我们对于文件的赋权通常包括三个部分<所有者权限><同组内用户权限><其他用户权限>. 对于权限, 我们一般有两种表示方式: 字符形式 与 数字形式.

字符形式, 主要是方便读. 数字形式主要是方便授权.

我们在使用命令授权的时候经常使用chmod 777 /tmp/hello/ 而不会写成 chmod u+x /tmp/hello/. 主要是因为使用方便. 但是读取的时候, 还是使用字符的形式, 更加方便. 如:

  1. localhost:Software Sean$ ls -lrt
  2. total 238016
  3. drwxr-xr-x@ 3 Sean staff 96 Feb 13 2012 Yummy FTP.app
  4. drwxr-xr-x 4 Sean staff 128 Oct 8 2016 Axure

其中:

  • 第一位d表示为文件目录;
  • 后面的 rwx中, 对于文件拥有者, r表示可读, w表示可写,x表示可以执行(即./start.sh执行脚本的方式).
  • 再后面的r-x表示,对于文件所在的组, r表示可读, -表示无写权限, x表示可执行;
  • 再后面的r-x表示, 其他用户的权限, 同上.
  • 注: r(读权限):4 / w(写权限):2 / x(执行权限):1. 所以rwx可就是常说的权限7; r-x是权限5.

我们在使用chmod命令时候, 可以使用chmod 755 /xx/xx.file赋予<所有者读写执行><同组读执行><其他用户读执行>这样的权限.

linux如何修改文件或目录的权限(chmod)
linux drwxr-xr-x 什么意思

此外, 我们有时还会遇到chmod u+x /xx/xx.file表示增加权限.
文件权限中 chmod、u+x、u、r、w、x分别代表什么
linux 权限 chmod u+x


其他注意实现

在创建文件时候, 有时系统默认设置权限的. 这时有一个umask, 可以查看当前的默认权限.

  1. localhost:Software Sean$ umask
  2. 0022

当前用户权限掩码为0022. 所以, 用户创建文件夹的权限为777-022, 也就是755, 即rwx r-x r-x. 我们可以创建一个文件夹查看下:

  1. localhost:Software Sean$ mkdir tm
  2. localhost:Software Sean$ ls -lrt | grep "tm"
  3. drwxr-xr-x 3 Sean staff 96 Apr 8 16:13 tm

另外, 默认创建文件的权限为666. 此时创建文件的默认权限为666-022, 可就是644, 即 rw- r-- r--. 默认的文件是没有执行权限的. 我们可以同样创建一个文件来查看一下:

  1. localhost:tm Sean$ touch 123.sh
  2. localhost:tm Sean$ ls -lrt
  3. total 0
  4. -rw-r--r-- 1 Sean staff 0 Apr 8 16:13 123.sh

Linux里新建文件/目录的默认权限


Reference

[1]. linux如何修改文件或目录的权限(chmod)
[2]. linux drwxr-xr-x 什么意思
[3]. 文件权限中 chmod、u+x、u、r、w、x分别代表什么
[4]. linux 权限 chmod u+x
[5]. Linux里新建文件/目录的默认权限

发表评论

表情:
评论列表 (有 0 条评论,387人围观)

还没有评论,来说两句吧...

相关阅读

    相关 07Linux基础-文件权限管理

    1 文件的基本权限 1.1 权限的作用 通过对文件设定权限可以达到以下三种访问限制权限: 只允许用户自己访问; 允许一个预先指定的用户组中的用户访问; 允许系