Sockets “/tmp”是UNIX域套接字文件的正确位置吗?
使用UNIX套接字进行IPC通信时,必须在文件系统中创建文件。互联网上的许多例子都认为Sockets “/tmp”是UNIX域套接字文件的正确位置吗?,sockets,unix,Sockets,Unix,使用UNIX套接字进行IPC通信时,必须在文件系统中创建文件。互联网上的许多例子都认为/tmp是放置套接字的好地方。一些人建议使用具有700权限的/tmp/specific_文件夹,以保持/tmp清洁,并防止其他用户访问插座 然而,这可靠吗?操作系统不能随时删除其中的任何文件吗?让我们假设这些文件将在那里保存数天或数月,有些文件可能在长时间内(即:天)不用于通信 此外,将这些文件存储在本地用户文件夹(例如,在/.hidden_sockets/中)的缺点是在系统崩溃等情况下无法清理这些文件。即使在
/tmp
是放置套接字的好地方。一些人建议使用具有700
权限的/tmp/specific_文件夹
,以保持/tmp
清洁,并防止其他用户访问插座
然而,这可靠吗?操作系统不能随时删除其中的任何文件吗?让我们假设这些文件将在那里保存数天或数月,有些文件可能在长时间内(即:天)不用于通信
此外,将这些文件存储在本地用户文件夹(例如,在/.hidden_sockets/
中)的缺点是在系统崩溃等情况下无法清理这些文件。即使在系统重新启动后也不行
你会把那些文件放在哪里?是否有标准/首选方法?如果在文件夹上设置了粘性位,则只有文件所有者和root用户才能从该目录中删除该文件,并且默认情况下/tmp目录受粘性位保护。 这就是为什么建议将套接字文件放在/tmp文件夹中,以便只有所有者和root用户有权删除它,但每个人都可以访问它
drwxrwxrwt. 25 root root 720 Mar 30 23:36 /tmp/
---------^--
this t indicate sticky bit ###this 1 in "chmod 1777 dir" used to set sticky bit
有关完整的故事,请检查以下分步命令-
考虑到我们有3个用户root,A和B.
[root@localhost ~]# mkdir /test
[root@localhost ~]# chmod 777 /test/
[root@localhost ~]# su - a
[a@localhost ~]$ cd /test/
[a@localhost test]$ touch a.txt
[a@localhost test]$ ls -ltr a.txt
-rw-rw-r--. 1 a a 0 Mar 30 17:25 a.txt
[a@localhost test]$ su - b
[b@localhost ~]$ cd /test/
[b@localhost test]$ touch b.txt
[b@localhost test]$ rm a.txt
rm: remove write-protected regular empty file 'a.txt'? y
[b@localhost test]$ su - a
[a@localhost ~]$ cd /test/
[a@localhost test]$ ll
-rw-rw-r--. 1 b b 0 Mar 30 17:25 b.txt
[a@localhost test]$ rm b.txt
rm: remove write-protected regular empty file 'b.txt'? y
[a@localhost test]$ su - root
[root@localhost ~]# chmod 1777 /test/ ####setting sticky bit
[root@localhost ~]# cd /test/
[root@localhost test]# su a
[a@localhost test]$ pwd
/test
[a@localhost test]$ touch a.txt
[a@localhost test]$ ll
-rw-rw-r--. 1 a a 0 Mar 30 17:27 a.txt
[root@localhost test]# su b
[b@localhost test]$ touch b.txt
[b@localhost test]$ ll
-rw-rw-r--. 1 a a 0 Mar 30 17:27 a.txt
-rw-rw-r--. 1 b b 0 Mar 30 17:27 b.txt
[b@localhost test]$ rm a.txt
rm: remove write-protected regular empty file 'a.txt'? y
rm: cannot remove 'a.txt': Operation not permitted
(b)用户无法删除该文件,因为我们已设置了粘性
位(在目录测试中)
如果在文件夹上设置了粘性位,则只有文件所有者和root用户才能从该目录中删除该文件,并且默认情况下/tmp目录受粘性位保护。 这就是为什么建议将套接字文件放在/tmp文件夹中,以便只有所有者和root用户有权删除它,但每个人都可以访问它
drwxrwxrwt. 25 root root 720 Mar 30 23:36 /tmp/
---------^--
this t indicate sticky bit ###this 1 in "chmod 1777 dir" used to set sticky bit
有关完整的故事,请检查以下分步命令-
考虑到我们有3个用户root,A和B.
[root@localhost ~]# mkdir /test
[root@localhost ~]# chmod 777 /test/
[root@localhost ~]# su - a
[a@localhost ~]$ cd /test/
[a@localhost test]$ touch a.txt
[a@localhost test]$ ls -ltr a.txt
-rw-rw-r--. 1 a a 0 Mar 30 17:25 a.txt
[a@localhost test]$ su - b
[b@localhost ~]$ cd /test/
[b@localhost test]$ touch b.txt
[b@localhost test]$ rm a.txt
rm: remove write-protected regular empty file 'a.txt'? y
[b@localhost test]$ su - a
[a@localhost ~]$ cd /test/
[a@localhost test]$ ll
-rw-rw-r--. 1 b b 0 Mar 30 17:25 b.txt
[a@localhost test]$ rm b.txt
rm: remove write-protected regular empty file 'b.txt'? y
[a@localhost test]$ su - root
[root@localhost ~]# chmod 1777 /test/ ####setting sticky bit
[root@localhost ~]# cd /test/
[root@localhost test]# su a
[a@localhost test]$ pwd
/test
[a@localhost test]$ touch a.txt
[a@localhost test]$ ll
-rw-rw-r--. 1 a a 0 Mar 30 17:27 a.txt
[root@localhost test]# su b
[b@localhost test]$ touch b.txt
[b@localhost test]$ ll
-rw-rw-r--. 1 a a 0 Mar 30 17:27 a.txt
-rw-rw-r--. 1 b b 0 Mar 30 17:27 b.txt
[b@localhost test]$ rm a.txt
rm: remove write-protected regular empty file 'a.txt'? y
rm: cannot remove 'a.txt': Operation not permitted
(b)用户无法删除该文件,因为我们已设置了粘性
位(在目录测试中)
最好的地方可能是下面的子目录,在该目录中,未经授权的用户软件可以存储运行时数据,例如通信原语。这类似于/run,但适用于用户应用程序。它是一个用户私有名称空间,因此使用起来非常安全。它在注销时自动清除。粘性位防止文件通过“老化”被时间清理 来源
- *
- *
/tmp
目录时,我在您的回答中遗漏了一些东西:[……]这可靠吗?操作系统不能随时删除其中的任何文件吗?[…]基本上,是否100%保证文件在使用过程中不会被删除?(即使套接字长时间不发送消息)感谢您的回答。然而,当提到/tmp
目录时,我在您的回答中遗漏了一些东西:[……]这可靠吗?操作系统不能随时删除其中的任何文件吗?[…]基本上,是否100%保证文件在使用过程中不会被删除?(即使套接字长时间不发送消息)