繁体   English   中英

Linux文件权限(深入)-数字到字符串的表示法,反之亦然; 额外的文件权限

[英]Linux file permissions(in-depth) - numeric to string notation, and vice versa; additional file-permissions

我想出了如何将符号rwx部分读取/转换为421个八进制部分的方法,这非常简单。 但是当涉及到特殊字符时,我感到非常困惑。 我们知道-r-xr --- wx转换为0543,但是-r-sr --- wt或-r-xr --- wt转换为什么?

我相信在用户执行许可下有x,s,S。对于组执行许可也有x,s,S。然后所有其他用户执行许可有x,t,T。这些是什么意思,以及它们如何转换八进制表示法。 我猜它与0421中的0位置有关吗?

从我的课堂笔记中可以看出,5543转换为-r-sr --- wt。 然后,示例问题-rS-wsrw-转换为6536,除了它希望我们固定第二个位置(5)以便将其正确转换。

我搜索并搜索了很多内容,但令人惊讶的是这些特殊字符找不到任何东西。

在Web上进行深入搜索之后,我找到了有关了解Linux文件权限的链接该链接对其进行了详细描述:

s-这表示 setuid / setgid权限。 未设置为显示在权限显示的特殊权限部分,而是按所有者或组权限的读取部分表示。

t-这表示粘性位权限。 未设置在权限显示的特殊权限部分中显示,而是在所有用户权限的可执行部分中表示。

Setuid / Setgid的特殊权限

--- setuid / setguid权限用于告诉系统以具有所有者权限的所有者身份运行可执行文件。

---请谨慎使用权限中的setuid / setgid位。 如果您错误地将setuid / setgid位置1,将权限分配给root拥有的文件,则可以打开系统进行入侵。

---您只能通过显式定义权限来分配setuid / setgid位。 setuid / setguid位的字符为s。

粘性位特殊权限

---黏贴位在共享环境中非常有用,因为当将其分配给目录的权限时,它会对其进行设置,因此只有文件所有者才能重命名或删除该文件。

---您只能通过显式定义权限来分配粘性位。 粘性位的字符是t。

从数值(1/2/4421)转换为符号(rwx / s / t)的逻辑:


编辑:

第一个数字表示所有者权限; 第二个代表组权限; 最后一个数字代表所有其他用户的权限。 这些数字是rwx字符串的二进制表示形式。

r = 4
w = 2
x = 1

--->粘性位可以使用chmod命令进行设置,并且可以使用八进制模式1000或符号t(setuid位已使用s)进行设置。 例如,要在目录/ usr / local / tmp上添加该位,可以键入chmod 1777 /usr/local/tmp

---> setuid和setgid位通常是通过chmod命令设置的,方法是将setuid的高阶八进制数字设置为4将setgid设置 2 chmod 6711 file将同时设置setuid和setgid位(4 + 2 = 6),从而使文件对所有者(7)可读/写/可执行,并可由组(前1个)和其他用户(第二个1)执行。

注意 :

s  ---  The setuid bit when found in the user triad; the setgid bit when found in the group 
        triad; it is not found in the others triad; it also implies that x is set.
S  ---  Same as s, but x is not set; rare on regular files, and useless on folders.
t  ---  The sticky bit; it can only be found in the others triad; it also implies that x is
        set.
T  ---  Same as t, but x is not set; rare on regular files, and useless on folders.

s,S,t和T值始终附加在“其他用户组”权限符号之前。 因此,符号的第一个字母表示附加到字符串的s,S,t或T值。 接下来的3个字母是通常的权限。

您有关文件权限的问题/示例:

1. -r-sr---wt   = 5543, first 5(s set for user = 4 + t set for others = 1),
                  second 5(r=4,s=1), third 4(r = 4), and last, fourth 3(w=2, t = 1).


2. -r-S-wsrw-   = 6436, first 6(S set for user = 4 + s set for group = 2),
                  second 5(r=4, x=0, since S don't account for x), 
                  third 3(w = 2, s results in x = 1), and last, fourth 6(r=4,w=2).

如果您需要实际的位,可以在stat.2手册页上找到它们(格式设置为代码,以便于阅读):

   The following mask values are defined for the file type of the
   st_mode field:

       S_IFMT     0170000   bit mask for the file type bit field

       S_IFSOCK   0140000   socket
       S_IFLNK    0120000   symbolic link
       S_IFREG    0100000   regular file
       S_IFBLK    0060000   block device
       S_IFDIR    0040000   directory
       S_IFCHR    0020000   character device
       S_IFIFO    0010000   FIFO

   ...

   The following mask values are defined for the file mode component of
   the st_mode field:

       S_ISUID     04000   set-user-ID bit
       S_ISGID     02000   set-group-ID bit (see below)
       S_ISVTX     01000   sticky bit (see below)

       S_IRWXU     00700   owner has read, write, and execute permission
       S_IRUSR     00400   owner has read permission
       S_IWUSR     00200   owner has write permission
       S_IXUSR     00100   owner has execute permission

       S_IRWXG     00070   group has read, write, and execute permission
       S_IRGRP     00040   group has read permission
       S_IWGRP     00020   group has write permission
       S_IXGRP     00010   group has execute permission

       S_IRWXO     00007   others (not in group) have read, write, and
                           execute permission
       S_IROTH     00004   others have read permission
       S_IWOTH     00002   others have write permission
       S_IXOTH     00001   others have execute permission

这些位在/usr/include/uapi/linux/stat.h头文件中定义:

#ifndef _UAPI_LINUX_STAT_H
#define _UAPI_LINUX_STAT_H


#if defined(__KERNEL__) || !defined(__GLIBC__) || (__GLIBC__ < 2)

#define S_IFMT  00170000
#define S_IFSOCK 0140000
#define S_IFLNK  0120000
#define S_IFREG  0100000
#define S_IFBLK  0060000
#define S_IFDIR  0040000
#define S_IFCHR  0020000
#define S_IFIFO  0010000
#define S_ISUID  0004000
#define S_ISGID  0002000
#define S_ISVTX  0001000

#define S_ISLNK(m)  (((m) & S_IFMT) == S_IFLNK)
#define S_ISREG(m)  (((m) & S_IFMT) == S_IFREG)
#define S_ISDIR(m)  (((m) & S_IFMT) == S_IFDIR)
#define S_ISCHR(m)  (((m) & S_IFMT) == S_IFCHR)
#define S_ISBLK(m)  (((m) & S_IFMT) == S_IFBLK)
#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
#define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)

#define S_IRWXU 00700
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100

#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010

#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001

#endif


#endif /* _UAPI_LINUX_STAT_H */

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM