什么是 Linux 文件权限?
Setuid、Setgid 和 Sticky Bits 是特殊类型的 Unix/Linux 文件权限集,允许某些用户以提升的权限运行特定程序。 最终,在文件上设置的权限决定了哪些用户可以读取、写入或执行文件。 Linux 提供了更高级的文件权限,允许您对文件或目录执行更具体的操作。 通常,这些文件权限用于允许用户以提升的权限执行某些任务(允许他们执行通常不允许执行的操作)。 这是通过三个不同的权限设置完成的。 它们是 setuid、setgid 和粘性位。
本文适用于已经具备基本 Linux 文件权限系统工作知识的用户或管理员。 如果你不熟悉这个主题,我们有一个关于 Linux 文件权限的优秀教程。 让我们首先查看每个权限集以及它们如何与我们的系统交互。
符号权限表示法
--- no permission
--x execute
-w- write
-wx write and execute
r-- read
r-x read and execute
rw- read and write
rwx read, write and execute
数字权限表示法
数字符号系统使用数字 1 到 7,每个数字对应于不同的符号权限集:
0 --- no permission
1 --x execute
2 -w- write
3 -wx write and execute
4 r-- read
5 r-x read and execute
6 rw- read and write
7 rwx read, write and execute
每种类型控制什么
读:显示文件的内容。 您可能可以在 Vim 等文本编辑器中打开它,但您将无法保存对文件的任何更改,除非您也具有写入权限。
写:更改文件或文件夹/目录。
执行:执行(又名运行)一个文件。 示例包括编译的二进制文件和 shell 脚本。
什么是Setuid?
Setuid 是一个 Linux 文件权限设置,允许用户在该文件所有者的许可下执行该文件或程序。 这主要用于提升当前用户的权限。 如果一个文件是“setuid”并且由用户“root”拥有,那么有能力执行该程序的用户将作为用户 root 而不是他们自己来执行该程序。 最常见的 example Linux 中的这个是 ‘sudo’。 在这个 example,用户’test’找到了可执行文件’sudo’ 并使用 ‘ls -l’ 命令将其完整列出。
root@host [~]# id
uid=1002(test) gid=1002(test) groups=1002(test)
root@host [~]# which sudo
/usr/bin/sudo
root@host [~]# ls -l /usr/bin/sudo
-rwsr-xr-x 1 root root 136808 Jan 31 13:37 /usr/bin/sudo
root@host [~]#
如果您查看 ‘ 的权限级别sudo’ 可执行文件,您可以在用户的权限中看到“s”,通常会有一个“x”。 另外,请注意该文件归用户“root”(超级用户)所有,并且该文件可由世界执行(权限中的最后一个“x”)。 这表明当用户执行该程序时,操作系统将不以用户“test”的身份执行该文件,而是以用户“root”的身份执行该文件。 在使用 ‘sudo’ 命令,这允许普通用户执行提升的系统功能,而无需以 root 用户身份登录。
如何设置 Setuid?
设置“setuid”权限就像在 Linux 中设置任何其他权限一样简单。 使用命令修改文件所有权。 一个 example 设置它的命令如下。
root@host [~]# chmod u+s <filename>
在这个 example,我们将使用命令“touch”创建一个名为“myfile”的文件,然后我们将使用“ls -l”命令检查其权限。
root@host [~]# touch myfile
root@host [~]# ls -l myfile
-rw-rw-r-- 1 test test 0 Mar 2 17:59 myfile
root@host [~]#
请注意,该文件没有用户、组或世界的执行权限。 我们将添加 setuid 位,如下所示。
root@host [~]# chmod u+s myfile
root@host [~]# ls -l myfile
-rwSrw-r-- 1 test test 0 Mar 2 17:59 myfile
root@host [~]#
这个输出看起来与我们预期的有点不同。 我们正在寻找的小写字母“s”现在是大写字母“S”。 这表示 setuid 是 设置,但拥有该文件的用户没有执行权限。 我们可以使用 ‘chmod u+x’ 命令添加该权限。
root@host [~]# chmod u+x myfile
root@host [~]# ls -l
total 0
-rwsrw-r-- 1 test test 0 Mar 2 17:59 myfile
root@host [~]#
什么是 Setgid?
当用于文件时,Setgid 与 setuid 非常相似。 一个进程在执行时将作为 团体 拥有该文件。 一个典型的 example 使用它的文件是“crontab”命令。
root@host [~]# which crontab
/usr/bin/crontab
root@host [~]# ls -l /usr/bin/crontab
-rwxr-sr-x 1 root crontab 36080 Apr 5 2016 /usr/bin/crontab
root@host [~]#
如何设置 Setgid?
与“setuid”类似,“setgid”是用“chmod g+s”命令插入的。 让我们创建一个名为“myfile2”的新文件。
root@host [~]# touch myfile2
root@host [~]# ls -l myfile2
-rw-rw-r-- 1 test test 0 Mar 2 19:30 myfile2
root@host [~]#
现在我们将运行“chmod g+s”命令并查看结果。
root@host [~]# chmod g+s myfile2
root@host [~]# ls -l myfile2
-rw-rwSr-- 1 test test 0 Mar 2 19:30 myfile2
root@host [~]#
我们再次看到设置了大写“S”,但我们可以对其进行修改。
root@host [~]# chmod g+x myfile2
root@host [~]# ls -l
total 0
-rwsrw-r-- 1 test test 0 Mar 2 17:59 myfile
-rw-rwsr-- 1 test test 0 Mar 2 19:30 myfile2
root@host [~]#
目录上的 Setgid
对目录应用 setgid 权限具有不同的行为。 带有“setgid”的目录将导致在该目录中创建的所有文件都归该目录的组所有,而不是所有者的组。 首先,我们创建一个目录。
root@host [~]# mkdir mydir
root@host [~]# ls -ld mydir
drwxrwxr-x 2 test test 4096 Mar 2 19:36 mydir
root@host [~]#
然后我们使用“chgrp”命令更改目录的组所有权,然后我们可以像以前一样添加“setgid”权限。
root@host [~]# chgrp test2 mydir/
root@host [~]# chmod g+s mydir
root@host [~]# ls -ld mydir/
drwxrwsr-x 2 test test2 4096 Mar 2 19:36 mydir/
root@host [~]#
让我们通过在该目录中创建一个文件来测试它。 本教程中的所有其他文件都是以这种方式创建的,并且以“test”为组。 因为在目录上设置了“setgid”并且它归组“test2”所有,所以这个文件将获得“test2”作为它的组。
root@host [~]# touch mydir/myfile3
root@host [~]# ls -l mydir/myfile3
-rw-rw-r-- 1 test test2 0 Mar 2 19:59 mydir/myfile3
root@host [~]#
什么是粘性位?
最后的特殊许可是“粘性位”。 在目录上设置此项时,该目录中的文件只能由所有者删除。 一个典型的用法是’/tmp/’。 /tmp 目录可以被任何用户写入,但其他用户不能删除其他用户的文件。
root@host [~]# ls -ld /tmp
drwxrwxrwt 8 root root 4096 Mar 2 20:17 /tmp
root@host [~]#
请注意,每个人都可以写入 /tmp,但在权限列表末尾用“t”代替“x”。 这意味着它有粘性位。
如何设置粘性位?
使用 ‘chmod +t’ 命令设置粘性位。
root@host [~]# mkdir mydir2
root@host [~]# ls -ld mydir2
drwxrwxr-x 2 test test 4096 Mar 2 20:17 mydir2
root@host [~]# chmod +t mydir2
root@host [~]# ls -ld mydir2
drwxrwxr-t 2 test test 4096 Mar 2 20:17 mydir2
root@host [~]#
使用数字符号设置特殊权限
您可能记得从上面的定义中可以使用一系列三个数字来设置权限。 这些数字分别代表所有者、组和世界的权限。 要确定要设置的数字,可以使用 x=1、w=2 和 r=4。 您将这些数字相加以获得许可编号。 如果我们想拥有读、写和执行权限,我们会使用 7。读和写是 6。只是读是 4。 example 将文件设置为所有者的读取、写入和执行,组和世界的读取和执行如下所示:
root@host [~]# chmod 755 myfile
root@host [~]# ls -l myfile
-rwxr-xr-x 1 test test 0 Mar 2 17:59 myfile
root@host [~]#
对于特殊权限,您可以在这些数字前面加上另一个数字,其中 4 是 setuid,2 是 setgid,1 是粘性位。 下面的命令都是一样的(假设文件有我们上面设置的权限)。
root@host [~]# chmod 4755 myfile
root@host [~]# chmod u+s myfile
root@host [~]# ls -l myfile
-rwsr-xr-x 1 test test 0 Mar 2 17:59 myfile
root@host [~]#
root@host [~]# chmod 2755 myfile
root@host [~]# chmod g+s myfile
root@host [~]# ls -l myfile
-rwxr-sr-x 1 test test 0 Mar 2 17:59 myfile
root@host [~]#
root@host [~]# chmod 1755 mydir
root@host [~]# chmod +t mydir
root@host [~]# ls -ld mydir
drwxr-sr-t 2 test test2 4096 Mar 2 19:59 mydir
root@host [~]#
删除特殊权限
要删除特殊权限,我们可以使用带有 ‘ 的相同 chmod 命令–‘ 代替 ‘+。
root@host [~]# chmod u-s myfile
root@host [~]# chmod g-s mydir
root@host [~]# chmod -t mydir2
root@host [~]# ls -l
total 8
drwxr-xr-x 2 test test2 4096 Mar 2 19:59 mydir
drwxrwxr-x 2 test test 4096 Mar 2 20:17 mydir2
-rwxr-xr-t 1 test test 0 Mar 2 17:59 myfile
root@host [~]#
结论
总而言之,这些特殊权限对于区分用户或组读取、写入或执行文件或影响文件夹更改的能力非常有用。
立即预订您的位置!
给我们打电话 800.580.4985,或打开 聊天 或与我们联系,与我们知识渊博的解决方案或经验丰富的托管顾问之一交谈,了解您今天如何利用这些技术!