Skip to main content

Command Palette

Search for a command to run...

How to Change File Permissions in Linux

Updated
7 min read

Changing file permissions in Linux is something you’ll often need to do. Whether you want to protect your files or allow others to access them, understanding how permissions work is key. You might feel a bit lost if you’re new to Linux, but don’t worry—I’ll guide you through the process step-by-step. By the end, you’ll know how to check and modify permissions easily.

We’ll cover the basics of Linux permissions, how to use commands like chmod, chown, and chgrp, and explain what each permission means. You’ll also see examples that make it simple to apply what you learn. Let’s dive in and make your Linux files secure and accessible exactly how you want.

Understanding Linux File Permissions

Linux file permissions control who can read, write, or execute a file or directory. These permissions help keep your system secure by limiting access. Every file and folder has three types of permissions for three groups of users:

  • Owner: The user who owns the file.
  • Group: Other users in the file’s group.
  • Others: Everyone else.

Each group can have these permissions:

  • Read (r): Allows viewing the file’s contents or listing a directory.
  • Write (w): Allows modifying the file or adding/removing files in a directory.
  • Execute (x): Allows running the file as a program or entering a directory.

You can see these permissions when you list files with the ls -l command. For example:

-rwxr-xr-- 1 alice staff 1024 Apr 10 12:00 script.sh

This means:

  • Owner (alice) has read, write, and execute permissions.
  • Group (staff) has read and execute permissions.
  • Others have read permission only.

Understanding this structure is the first step to changing permissions effectively.

How to Check File Permissions in Linux

Before changing permissions, you need to know the current settings. Use the ls -l command to list files with detailed info:

ls -l filename

This shows permissions, owner, group, size, and modification date. For example:

-rw-r--r-- 1 bob users 2048 May 1 09:30 notes.txt

You can also check permissions for multiple files or directories:

ls -l /path/to/directory

If you want to see permissions in numeric form (octal), use:

stat -c "%a %n" filename

This outputs something like:

644 notes.txt

Here, 644 is the numeric permission code, which is useful when using the chmod command.

Changing Permissions with chmod Command

The chmod command is the main tool to change file permissions in Linux. You can use it in two ways: symbolic mode and numeric mode.

Symbolic Mode

In symbolic mode, you specify which user group to change (u for owner, g for group, o for others, a for all) and what permission to add (+), remove (-), or set (=).

Examples:

  • Add execute permission for the owner:

    chmod u+x script.sh
    
  • Remove write permission for group:

    chmod g-w file.txt
    
  • Set read and write for owner, read for group and others:

    chmod u=rw,go=r document.txt
    

Numeric Mode

In numeric mode, you use a three-digit number representing permissions for owner, group, and others. Each digit is a sum of:

  • Read = 4
  • Write = 2
  • Execute = 1

For example:

  • 7 (4+2+1) means read, write, execute.
  • 6 (4+2) means read and write.
  • 5 (4+1) means read and execute.
  • 4 means read only.

To set permissions, run:

chmod 755 script.sh

This means:

  • Owner: read, write, execute (7)
  • Group: read, execute (5)
  • Others: read, execute (5)

Numeric mode is quick and precise once you understand the values.

Changing Ownership with chown and chgrp

Sometimes, you need to change the owner or group of a file, not just the permissions. This is where chown and chgrp come in.

chown: Change Owner

To change the owner of a file:

sudo chown newowner filename

Example:

sudo chown alice report.txt

This makes alice the owner of report.txt.

You can also change both owner and group at once:

sudo chown alice:staff report.txt

chgrp: Change Group

To change only the group:

sudo chgrp newgroup filename

Example:

sudo chgrp staff report.txt

This changes the group to staff.

Changing ownership is important for managing access in multi-user environments.

Using Recursive Options to Change Permissions and Ownership

If you want to change permissions or ownership for all files and directories inside a folder, use the recursive option -R.

For example, to give execute permission to all files in a directory:

chmod -R u+x /path/to/directory

To change ownership of all files and folders inside a directory:

sudo chown -R alice:staff /path/to/directory

Be careful with recursive changes, as they affect everything inside the directory.

Special Permissions: Setuid, Setgid, and Sticky Bit

Linux also has special permissions that control how files and directories behave.

  • Setuid (s): When set on an executable file, it runs with the file owner’s permissions. Useful for programs needing higher privileges.
  • Setgid (s): When set on a file, it runs with the group’s permissions. When set on a directory, new files inherit the directory’s group.
  • Sticky bit (t): When set on a directory, only the file owner can delete or rename files inside, even if others have write permission.

You can set these using numeric mode:

  • Setuid = 4 (thousands place)
  • Setgid = 2 (thousands place)
  • Sticky bit = 1 (thousands place)

Example to set sticky bit on /tmp directory:

chmod 1777 /tmp

This means:

  • Owner, group, others have read, write, execute.
  • Sticky bit is set to protect files.

Practical Examples of Changing File Permissions

Here are some common scenarios you might face:

  • Make a script executable by the owner only:

    chmod u+x script.sh
    
  • Allow everyone to read a file but only owner can write:

    chmod 644 file.txt
    
  • Allow group members to write to a directory:

    chmod g+w /shared/folder
    
  • Set a directory so new files inherit group ownership:

    chmod g+s /project
    
  • Make a directory writable by everyone but protect files with sticky bit:

    chmod 1777 /public
    

These examples help you control access based on your needs.

Troubleshooting Permission Issues

If you can’t access or modify a file, check:

  • Are you the owner or in the group?
  • What are the current permissions (ls -l)?
  • Is the file on a mounted filesystem with restrictions?
  • Are special permissions like setuid or sticky bit affecting behavior?

Use sudo if you need temporary root access to change permissions or ownership. Always be cautious when changing permissions on system files to avoid security risks.

Conclusion

Changing file permissions in Linux is a skill that gives you control over your files and system security. By understanding the basics of read, write, and execute permissions, and how they apply to owners, groups, and others, you can manage access effectively. Using commands like chmod, chown, and chgrp lets you customize permissions to fit your needs.

Remember to check current permissions before making changes and use recursive options carefully. Special permissions like setuid, setgid, and sticky bit add extra control for advanced use cases. With practice, you’ll find managing Linux file permissions straightforward and empowering.

FAQs

How do I make a file executable in Linux?

Use the command chmod +x filename to add execute permission for all user categories. You can also specify owner only with chmod u+x filename.

What does the permission code 755 mean?

It means the owner has read, write, and execute permissions (7), while group and others have read and execute permissions (5 each).

How can I change the owner of a file?

Use sudo chown newowner filename to change the file’s owner. You can also change group with sudo chown newowner:newgroup filename.

What is the difference between chmod symbolic and numeric modes?

Symbolic mode uses letters and symbols (e.g., u+x) to add or remove permissions, while numeric mode uses numbers (e.g., 755) to set exact permissions.

How do I apply permission changes to all files in a directory?

Use the recursive option -R with chmod or chown, like chmod -R 755 /path/to/directory, to apply changes to all files and subdirectories.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts