The sticky bit is a special permission that is primarily used on directories. When the sticky bit is set on a directory, it restricts file deletion. Only the owner of a file, the owner of the directory, or the root user can delete or rename a file within that directory. This is commonly seen on the /tmp directory.
Example 1: Setting the sticky bit on a directory
# Create a new directory
mkdir shared_directory
# Set the sticky bit using the symbolic method
chmod +t shared_directory
# Verify the permissions
ls -ld shared_directory
Explanation: This code first creates a directory called shared_directory. The chmod +t command is then used to add the sticky bit permission to this directory. The ls -ld command will now show a 't' in the 'others' execute permission slot, indicating the sticky bit is set.
Example 2: Setting the sticky bit using octal notation
# Create another directory for this example
mkdir sticky_octal_dir
# Set read, write, and execute for everyone, plus the sticky bit
chmod 1777 sticky_octal_dir
# Check the permissions of the directory
ls -ld sticky_octal_dir
Explanation: In this example, the octal notation 1777 is used. The leading '1' represents the sticky bit. The '777' that follows grants read, write, and execute permissions to the user, group, and others.
Example 3: Verifying the functionality of the sticky bit
# As user1, create a directory and set the sticky bit
mkdir -m 1777 /tmp/test_sticky
touch /tmp/test_sticky/user1_file
# As user2, try to delete user1's file
# This command will fail
rm /tmp/test_sticky/user1_file
Explanation: This example demonstrates the practical effect of the sticky bit. After user1 creates a file in a directory with the sticky bit set, user2 is unable to delete that file, even though user2 has write permissions in the directory. The operation will result in a "Operation not permitted" error.
Example 4: Finding all directories with the sticky bit set
# Search the entire filesystem for directories with the sticky bit set
find / -type d -perm /1000
Explanation: The find command is used here to locate directories with the sticky bit. The -type d option restricts the search to only directories, and -perm /1000 looks for items where the sticky bit (octal 1000) is set.
Example 5: Removing the sticky bit from a directory
# Assume shared_directory has the sticky bit set
# Remove the sticky bit using the symbolic method
chmod -t shared_directory
# Verify the permissions have been changed
ls -ld shared_directory
Explanation: To remove the sticky bit from a directory, the chmod -t command is used. This command specifically removes the 't' permission. A subsequent ls -ld command will confirm that the sticky bit is no longer active on the shared_directory.
Example 6: The sticky bit on a file (a historical note)
# Create a file
touch sticky_file_example
# Set the sticky bit on the file
chmod +t sticky_file_example
# Check the permissions
ls -l sticky_file_example
Explanation: On modern Linux systems like RHEL 8, setting the sticky bit on a file has no effect. It is a historical artifact from older UNIX systems where it was used to keep an executable in swap memory after it finished running. While you can set the permission, the Linux kernel ignores it for files.
Example 7: Command differences in HP-UX
# On RHEL/Linux
chmod +t directory_name
# On HP-UX, the command is identical
chmod +t directory_name
# Verification might be done with slightly different ls options in very old versions
# but modern HP-UX is largely compatible with Linux in this regard.
ls -ld directory_name
Explanation: The command to set the sticky bit, chmod +t, is highly standardized and works the same way on RHEL 8 and other UNIX variants like HP-UX. The ls -ld command for verification is also standard across these systems, making this a very portable administrative action.