To manage file permissions and ownership in Linux take a bit of practice and patience. This is why after brewing a nice cup of specialty coffee from Bolivia at home I decided to put together tutorial together.

To start, we should go into the basics of managing file permissions and ownership in Linux.

Basic Linux Permissions (r, w, x)

Create a file and set permissions. Run the below commands in your Terminal:

touch toma.txt
ls -l toma.txt
chmod 640 toma.txt
ls -l toma.txt

What the chmod options mean:
6 = read/write for owner
4 = read for group
0 = no access for others

SUID(Set User ID)in Linux

Run program as file owner instead of user running it. For example:

ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 54256 …

s in place of x means SUID → normal users can run passwd as root to change their password.

Another example is to add SUID to a script:

chmod u+s usemyscript.sh

SGID (Set Group ID)

Files created in a directory inherit the group of the directory, not the user’s primary group. For example:

mkdir /shared
chgrp developers /shared
chmod 2775 /shared
ls -ld /shared

Now files in /shared always belong to group developers.

Sticky Bit in Linux

In a shared directory, only file owners can delete their files (not other users). For example:

mkdir /public
chmod 1777 /public
ls -ld /public

The above a common file permissions for the /tmp folder in Linux.

umask (File Creation Mask)

Defines default permissions when files/directories are create. If you want to see the current setting:

umask

Example: Set umask 027 will make new files = 640, new dirs = 750.

umask 027
touch newfile
mkdir newdir
ls -l

Nice formula to remember is:

New File = 666 - umask
New Dir  = 777 - umask

Then you subtract the umask you set:

umask 022   # files=644, dirs=755 (default on most systems)
umask 027   # files=640, dirs=750 (more secure)

For example:

666 – 022 = 644

777 – 027 = 750

Group-based Access in Linux

Add a user to a group:

sudo usermod -aG developers ali

Change file group ownership:

chgrp developers project.txt
chmod 640 project.txt

Now user ali (in developers group) can read the file.

A brief summary File Permissions and Ownership in Linux

  • Sticky bit protects /tmp from one user deleting another’s files.
  • SGID ensures project files in /shared all belong to the developers group.
  • umask ensures new config files don’t get created world-readable (security).
  • SUID lets non-root users run system tools (like passwd).

Common Octal Values:

ModeOctalMeaning
644rw-r–r–Files readable by all, writable only by owner
600rw——-Private file
755rwxr-xr-xCommon for executables/scripts
700rwx——Private dir or script
770rwxrwx—Shared group dir
1777drwxrwxrwtWorld-writable but sticky (/tmp)
4755-rwsr-xr-xSUID program
2755drwxr-sr-xSGID dir
6755-rwsr-sr-xBoth SUID and SGID

Special bits (setuid, setgid, sticky)

They are represented by an extra digit in front of the normal octal permissions:

  • 4xxx → setuid
  • 2xxx → setgid
  • 1xxx → sticky bit

Normal octal permissions

rwxr-xr-x → 755
rw-r--r-- → 644

That’s 3 digits:

  • 1st → user
  • 2nd → group
  • 3rd → others

Special bits add a 4th digit

The special permissions are encoded as an extra octal digit before those three:

  • 4xxx → setuid
  • 2xxx → setgid
  • 1xxx → sticky

So for example:

Special BitOctal DigitExample
setuid44755 = rwsr-xr-x
setgid22755 = rwxr-sr-x
sticky11777 = rwxrwxrwt

To manage file permissions and ownership in Linux might seem confusing. But, once you practice over and over you will master it. Always contact me if you have any questions. Consider supporting my work by shopping one of my T-Shirt and coffee mugs designs from my shop. You can see some of them below. Thanks!

Recommended products

Leave a comment

Your email address will not be published. Required fields are marked *