Skip to main content

Command Palette

Search for a command to run...

How Do You Change File Permissions in Linux

Published
6 min readView as Markdown

Changing file permissions in Linux is an essential skill for anyone using this powerful operating system. Whether you're managing your personal files or running a server, understanding how to control who can read, write, or execute files is crucial. In this article, I’ll guide you through the basics and advanced methods of changing file permissions in Linux, so you can keep your system secure and organized.

You might wonder why file permissions matter so much. Well, they protect your files from unauthorized access and help you share files safely with others. By the end of this article, you’ll know exactly how to check and modify permissions using simple commands like chmod, chown, and chgrp. Let’s dive in!

Understanding Linux File Permissions

Linux file permissions determine who can access or modify a file or directory. These permissions are divided into three categories:

  • Owner: The user who owns the file.
  • Group: A set of users who share the same permissions.
  • Others: Everyone else on the system.

Each category can have three types of permissions:

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

Permissions are displayed in a 10-character string when you list files with ls -l. For example:

-rwxr-xr--
  • The first character indicates the file type (- for file, d for directory).
  • The next three characters (rwx) are the owner’s permissions.
  • The following three (r-x) are the group’s permissions.
  • The last three (r--) are the permissions for others.

Understanding this layout helps you know who can do what with your files.

How to Check File Permissions

Before changing permissions, you need to check the current settings. Use the ls -l command in the terminal:

ls -l filename

This will show the permissions, owner, group, file size, and modification date. For example:

-rw-r--r-- 1 alice staff 1024 Apr 10 12:00 example.txt

Here, alice is the owner, staff is the group, and the permissions mean:

  • Owner can read and write (rw-).
  • Group can read only (r--).
  • Others can read only (r--).

You can also check permissions for directories the same way.

Changing Permissions with chmod

The chmod command is the most common way to change file permissions in Linux. It stands for "change mode." You can use it in two ways: symbolic mode and numeric mode.

Symbolic Mode

Symbolic mode uses letters to specify which permissions to add, remove, or set.

  • u = user (owner)
  • g = group
  • o = others
  • a = all (user, group, others)

Operators:

  • + adds a permission
  • - removes a permission
  • = sets exact permissions

Examples:

  • Add execute permission for the owner:

    chmod u+x filename
    
  • Remove write permission for the group:

    chmod g-w filename
    
  • Set read and write for owner, read for group and others:

    chmod u=rw,g=r,o=r filename
    

Numeric Mode

Numeric mode uses numbers to represent permissions:

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

Add these numbers for each category:

  • Owner’s permissions
  • Group’s permissions
  • Others’ permissions

For example, chmod 755 filename means:

  • Owner: 7 (4+2+1) = read, write, execute
  • Group: 5 (4+0+1) = read, execute
  • Others: 5 (4+0+1) = read, execute

Other common numeric permissions:

NumericPermissionsDescription
644rw-r--r--Owner read/write, others read
600rw-------Owner read/write only
777rwxrwxrwxEveryone full permissions

To change permissions using numeric mode:

chmod 644 filename

Changing Ownership with chown and chgrp

Sometimes, you need to change who owns a file or which group it belongs to. This is where chown and chgrp come in.

chown: Change Owner

The chown command changes the owner of a file or directory.

chown newowner filename

You can also change both owner and group at once:

chown newowner:newgroup filename

For example:

chown alice:staff example.txt

This sets alice as the owner and staff as the group.

chgrp: Change Group

If you only want to change the group, use chgrp:

chgrp newgroup filename

Example:

chgrp staff example.txt

This changes the group to staff without affecting the owner.

Recursive Permission Changes

When working with directories, you might want to change permissions for all files and subdirectories inside. Use the -R (recursive) option with chmod, chown, or chgrp.

Example:

chmod -R 755 /path/to/directory

This sets the permissions for the directory and everything inside it.

Be careful with recursive changes, especially on system directories, as incorrect permissions can cause problems.

Special Permissions: Setuid, Setgid, and Sticky Bit

Linux also supports special permissions that affect how files and directories behave.

Setuid (Set User ID)

When set on an executable file, the program runs with the permissions of the file owner, not the user running it.

Symbolic mode: u+s

Example:

chmod u+s /usr/bin/someprogram

Setgid (Set Group ID)

For files, it works like setuid but for the group. For directories, new files inherit the group of the directory.

Symbolic mode: g+s

Example:

chmod g+s /shared/directory

Sticky Bit

Used mainly on directories, it allows only the file owner to delete or rename files inside, even if others have write permission.

Symbolic mode: +t

Example:

chmod +t /tmp

This is why /tmp is safe for multiple users.

Practical Examples

Here are some common scenarios you might face:

  • Make a script executable:

    chmod +x script.sh
    
  • Allow group members to write to a shared folder:

    chmod 775 /shared/folder
    chgrp staff /shared/folder
    chmod g+s /shared/folder
    
  • Secure a private file:

    chmod 600 secret.txt
    
  • Change ownership of a website folder:

    chown -R www-data:www-data /var/www/html
    

Tips for Managing Permissions Safely

  • Always check current permissions before changing them.
  • Use numeric mode for quick, precise changes.
  • Avoid giving write or execute permissions to "others" unless necessary.
  • Use recursive changes carefully.
  • Understand the impact of special permissions before applying them.

Conclusion

Changing file permissions in Linux is straightforward once you understand the basics. You can control who reads, writes, or executes your files using chmod, and manage ownership with chown and chgrp. Remember to check permissions regularly to keep your system secure.

By mastering these commands, you’ll have better control over your files and directories, improving both security and collaboration. Whether you’re a beginner or an experienced user, these skills are essential for effective Linux system management.


FAQs

How do I make a file executable in Linux?

Use the command chmod +x filename. This adds execute permission for the file owner, allowing you to run the file as a program or script.

What does chmod 755 mean?

It means the owner has full permissions (read, write, execute), while the group and others have read and execute permissions only.

Can I change file ownership without root access?

No, changing ownership with chown usually requires root or sudo privileges to prevent unauthorized changes.

What is the difference between chmod and chown?

chmod changes file permissions (read, write, execute), while chown changes the file owner and group.

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

Use the recursive option -R with commands like chmod -R 755 /directory to change permissions for all files and subdirectories inside.

More from this blog

L

LinuxBloke | Linux Tips, Tricks & Troubleshooting

672 posts