Is `/tmp` the right place for UNIX-domain socket files? Is `/tmp` the right place for UNIX-domain socket files? unix unix

Is `/tmp` the right place for UNIX-domain socket files?


Possibly the best place is a sub-directory beneath $XDG_RUNTIME_DIR, where unprivileged user software can store runtime data such as communication primitives. This is similar to /run but for user applications. It's a user private namespace, and hence very safe to use. It's cleaned up automatically at logout. Sticky bit prevents files to be cleaned-up by time via "aging".

Sources


When sticky bit is set on a folder then only the owner of the file and root can remove the file from that directory and by default /tmp directory is protected with sticky bit.That is why it is suggested to put the socket file in /tmp folder so that only the owner and root have the permission to remove it but everyone can access it.

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

For complete story check the below step by step commands - Consider we have 3 users root, a and 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 user is not able to remove the file because we have set the sticky bit on the directory test)