Home Learn Linux Linux Permissions Demystified: SUID, SGID & Sticky Bit

Linux Permissions Demystified: SUID, SGID & Sticky Bit

We will dive deep into these special permissions, exploring what they are, why they are essential, and how to implement them, ensuring that you harness their capabilities while maintaining system security.

by Divya Kiran Kumar
exploring suid, sgid, and sticky bit in linux

In this article, I’ll be diving into a topic that is a must-read for all Linux users: Linux file permissions. Having spent countless hours setting up servers and debugging permission-related issues, I have a certain fondness for understanding the nitty-gritty of permissions in Linux. It’s like finding the perfect combination to a safe – get it right and everything works seamlessly, get it wrong and you could be left scratching your head.

So, let’s delve into the wonderful world of SUID, SGID, and the sticky bit.

What are File Permissions?

Every file and directory in Linux has a set of permissions that determine who can access them and how they can be accessed. You can view these permissions with the ls -l command.

Let’s take a look at a sample output:

-rw-r--r-- 1 owner group 23 Aug 10 14:34 fosslinux_sample.txt

From left to right:

  1. -: This indicates the type of file. A dash - means it’s a regular file, while d indicates a directory.
  2. rw-: This represents the permissions for the file’s owner.
  3. r--: This represents the permissions for the file’s group.
  4. r--: This represents the permissions for everyone else.

But did you know that besides these, there are some special permissions? Enter SUID, SGID, and the sticky bit.

SUID (Set User ID)

The SUID bit, when set on a file, allows the file to run with the permissions of its owner rather than the permissions of the person running it. It’s symbolized by an s in the user’s permission spot.

Example:

-rwsr-xr-x 1 owner group 2048 Aug 10 14:34 fosslinux_sample

This means when any user executes fosslinux_sample, it will run with the permissions of the owner. One classic example of a file with SUID permission is the /usr/bin/passwd command, which lets regular users change their passwords, editing the /etc/shadow file which they normally wouldn’t have access to.

However, a word of caution: Incorrect use of SUID can pose security risks. If a malicious user can exploit a program with the SUID bit set, they might gain unauthorized permissions.

SGID (Set Group ID)

SGID is similar to SUID but instead of user permissions, it deals with group permissions. When SGID is set on a file, it runs with the permissions of the group that owns the file. On directories, however, it has a different function. Any files or directories created within a directory with the SGID bit set will inherit the group of the parent directory.

Example:

-rwxr-sr-x 1 owner group 2048 Aug 10 14:34 fosslinux_sample_dir

This has always been a personal favorite of mine, especially when setting up shared folders on a server. It ensures that all files, irrespective of who creates them, belong to a certain group, making collaboration a breeze. “2048” represents a hypothetical file size in bytes for the example files I mentioned.

Sticky Bit

Now, the sticky bit is an interesting one. When set on a directory, it ensures that only the owner of a file can delete or modify it, regardless of the directory’s permissions. This is especially useful in directories like /tmp, where users can create files but shouldn’t be able to tamper with others’ files.

Example:

drwxrwxrwt 8 root root 4096 Aug 10 14:34 fosslinux_sample_dir

Notice the t at the end. This indicates that the sticky bit is set.

Practical Application

To set these special permissions, you use the chmod command. Here’s a brief overview:

  • SUID: chmod u+s filename
  • SGID: chmod g+s filename
  • Sticky Bit: chmod o+t directoryname

Let me give a example usage case.

Use Case: Setting Up a Shared Workspace in an Organization

Imagine you’re working as a System Administrator for a mid-sized organization called “TechFlow Inc.” The Research & Development (R&D) department has approached you with a request. They want a shared directory where team members can drop their scripts, data, and tools. However, they have some specific requirements:

All files dropped in this directory should be accessible by any member of the R&D group.
Any member of the R&D group should be able to add and execute files, but they should only be able to modify or delete their own files.
They want some scripts to run with elevated permissions to access specific system resources.

Setting Up the Workspace

Step 1: Creating the Directory

First, you create the shared directory:

mkdir /shared/rd_workspace

Step 2: Setting the Group Ownership

Assign the R&D group to the directory:

chown :rd_group /shared/rd_workspace

Step 3: Implementing the SGID and Sticky Bit

This is where our knowledge of SGID and the sticky bit comes into play:

SGID ensures that any file or directory created inside will inherit the group of the parent directory.
The sticky bit ensures that members can only delete or modify their own files.

chmod g+s /shared/rd_workspace
chmod o+t /shared/rd_workspace

Now, when members from the R&D team create files in /shared/rd_workspace, the files will belong to the rd_group, and only the individual who created a file can modify or delete it.

Step 4: Setting up a Special Script with SUID

The R&D team has a script called resourceScanner, which requires elevated permissions to scan system resources.

chown admin_user /shared/rd_workspace/resourceScanner
chmod u+s /shared/rd_workspace/resourceScanner

By setting the SUID, whenever any R&D team member runs resourceScanner, it will execute with the permissions of admin_user.

After a day, you get a thankful message from the R&D department. Their shared workspace is functioning exactly as they wanted. They can collaborate without accidentally overwriting each other’s work, and the resourceScanner tool works seamlessly.

Through this use case, you can see how SGID, SUID, and the sticky bit can be instrumental in real-world scenarios, providing both functionality and security in an organizational setup. Properly configured permissions can solve real challenges, ensuring smooth collaboration while preserving individual responsibilities.

Pro Tips

  1. Audit Regularly: Periodically check for unwanted SUID and SGID bits in your system with the find command. For instance, find / -perm -4000 will search for files with the SUID bit set.
  2. Use Sparingly: Don’t set the SUID or SGID bit unless absolutely necessary. Unwanted or misconfigured permissions can lead to security breaches.
  3. Document: Whenever you change special permissions, make a note of it. It aids debugging later and ensures that other team members are aware of the changes.

As much as I adore the flexibility and control Linux permissions offer, I’ve had my fair share of facepalm moments. There was this one time I mistakenly set the SUID bit on a custom script. It took me hours to figure out why users were getting elevated permissions!

However, every mistake was a learning opportunity. Now, I approach permissions with a mix of respect and caution. And as for the sticky bit, it remains my unsung hero, preventing many potential file-deletion disasters in shared environments.

Wrapping Up

Linux permissions, especially the SUID, SGID, and sticky bit, are like the intricate gears of a clock. When set up correctly, they ensure the system runs smoothly. I hope this guide demystified these special permissions for you. With great power comes great responsibility. Use them wisely!

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.