当前位置: 首页>>技术问答>>正文


“ls -ld/tmp”输出中的“t”字母是什么?

, ,

问题描述

运行命令ls -ld /tmp时,输出将是:

drwxrwxrwt 30 root root 20480 Mar 11 14:17 /tmp

所以我有两个主要问题:

  • 什么是t后的权限?

  • 据我所知/tmp用于创建与系统中不同用户相关的临时文件,那么如何获得许可rwxrwxrwx(777)?

这对我来说似乎是错误的。请我需要你的帮助来理解这里会发生什么。

最佳解决方案

那么粘性是什么?

粘滞位是在目录上设置的权限位,该目录仅允许该目录中的文件的所有者,目录的所有者或根用户删除或重命名该文件。没有其他用户具有删除其他用户创建的文件所需的权限。

这是一种安全措施,可避免删除关键文件夹及其内容(sub-directories和文件),尽管其他用户具有完全权限。

为什么/tmp具有t粘性位?

不同的Linux用户可以使用/tmp目录创建临时文件。现在,如果用户删除/重命名由该目录中的某个其他用户创建的文件,该怎么办?

那么,为了避免这些问题,我们使用了粘性位的概念。因此,为了这个777给予,但保留粘性位不是一个坏主意。

我如何设置目录的粘性位?

我将在桌面上的一个名为test的目录上设置一个粘性位。

符号方式(t代表粘性位):

chmod o+t ~/Desktop/test

要么

chmod +t ~/Desktop/test

数值/八进制方式(1,位于第一位的值为1的粘性位)

chmod 1757 ~/Desktop/test

现在让我们测试结果:

ls -li ~/Desktop/test

1551793 drwxrwxrwt 45 hadi hadi 20485 Mar 11 14:35 ~/Desktop/test

删除/删除粘贴位

chmod o-t ~/Desktop/test

现在让我们测试结果:

ls -li ~/Desktop/test

1551793 drwxrwxrwx 45 hadi hadi 20485 Mar 11 14:35 ~/Desktop/test

来源:“What is a sticky Bit and how to set it in Linux?” at The Linux Juggernaut

次佳解决方案

A Sticky bit is a permission bit that is set on a file or a directory that lets only the owner of the file/directory or the root user to delete or rename the file. No other user is given privileges to delete the file created by some other user.

有时会发生这样的情况:您需要Linux系统的所有用户都可以使用的Linux目录来创建文件。用户可以根据他们在此目录中的便利性创建,删除或重命名文件。

Now, what if an user accidentally or deliberately deletes (or rename) a file created by some other user in this directory?

Well, to avoid these kind of issues, the concept of sticky bit is used. Since /tmp is used for this purpose. So to avoid the above scenario, /tmp use sticky bit.

例如:

mkdir demo
chmod 777 demo

我还在该文件夹中创建了两个具有不同用户权限的文件,其中有权限777

ls -ld demo
drwxrwxrwx 2 guru guru 4096 Mar 11 18:17 demo

ls -l demo
-rwxrwxrwx 1 abhi abhi    0 Mar 11 17:11 file1
-rwxrwxrwx 1 anshu anshu   0 Mar 11 18:15 file2

现在打开这个粘性位

 chmod +t demo/
 ls -ld demo
 drwxrwxrwt 2 guru guru 4096 Mar 11 18:17 demo

现在如果一个用户(abhi)想要重命名第二个用户(anshu)会发生什么情况

mv /home/guru/demo/file2  /home/guru/demo/file3
mv: cannot move '/home/guru/demo/file2' to  '/home/guru/demo/file3': Operation not   permitted  

粘性位的起源

在Linux上,粘性位仅在目录上具有上述用途。从历史上看,它在常规文件中用于完全不同的东西,这就是名称的来源。

When a program is executed, it takes time to load the program into memory before the user can actually start using it. If a program, for example an editor is used frequently by users the the start-up time delay was an overhead back then.

To improve this time delay, the sticky bit was introduced. The OS checked that if sticky bit on an executable is ON, then the text segment of the executable was kept in the swap space. This made it easy to load back the executable into RAM when the program was run again thus minimizing the time delay.

像Linux这样的现代系统会自动管理其可执行文件和其他文件的缓存,并且不需要那些粘性位。

来源:“Linux Sticky Bit Concept Explained with Examples” at The Geek Stuff

第三种解决方案

stickybit是一种避免意外删除共享目录的解决方法。当一个目录有一个stickybit时,只有所有者或根可以删除它,即使每个用户都可以获得完整的其他权限。

/tmp是进程和用户之间最共享的目录,因此它包含stickybit以确保没有用户可以删除该目录,即使该权限是777,也必须这样才能让用户和进程能够使用该目录在权限中没有冲突。

参考资料

本文由Ubuntu问答整理, 博文地址: https://ubuntuqa.com/article/954.html,未经允许,请勿转载。