What is the correct permission for /tmp directory in Linux
The /var/tmp directory is made available for programs that require temporary files or directories that are preserved between system reboots. Therefore, data stored in /var/tmp is more persistent than data in /tmp. Files and directories located in /var/tmp must not be deleted when the system is booted.
The directories /tmp and /var/tmp has some special permissions as well. The default permissions of these directories are as follows:
# ls -ld /tmp
drwxrwxrwt. 14 root root 4096 Feb 5 15:06 /tmp
# ls -ld /var/tmp
drwxrwxrwt. 8 root root 4096 Feb 5 15:06 /var/tmp
If you notice carefully, you can see an extra permission tsticky bit" permission. Many applications will show errors or fail if they are not able to write to /tmp with the appropriate permissions.
What is a sticky bit
When the sticky bit is set on a directory, only the root user, the owner of the directory, and the owner of a file can remove files within said directory. /var/tmp and /tmp are 2 examples of sticky bit. The t in place of the others execute permission indicates that this is a sticky bit directory. The /tmp directory is a location where all users must be able to create files. Unfortunately, the permission that allows users to create files also allows them to delete files—all files—in that directory.
How to set sticky bit on /tmp and /var/tmp directories
Change the permissions on the directory by running either the following commands:
# chmod 1777 /tmp
# chmod 1777 /var/tmp
or
# chmod a+trwx /tmp
# chmod a+trwx /var/tmp
Here,
1 – This digit controls special attribute settings. the value 1 sets the sticky bit on the object/directory.
7777 – These digits control read write and execute permissions for the file owner, the file owner’s primary group, and all other users. The value 4 enables read permission, the value 2 enables write permission, and the value 1 enables execute permission.