ThisCalculator

Everyday

Linux File Permissions and Ownership: How to Change Them

Updated · Standard formula, shown below

Linux File Permissions and Ownership: How to Change Them

Changing Linux file permissions controls who can read, modify, or run a file; changing ownership assigns it to a user and group. You’ll use commands such as chmod and chown in a terminal. Before changing anything, identify the exact path and make sure you have the required account privileges.

What You’ll Need

  • Access to a Linux terminal, either on the computer or through a remote shell.
  • The path to the file or directory you want to change. Use pwd and ls -l to check your current location and inspect files.
  • Your account password or administrator access if the changes require sudo.
  • A clear idea of which users need access. Avoid broad permissions unless you understand the consequences.

Step-by-Step

  1. Open a terminal and locate the target. Run pwd to print the current directory. List its contents with ls -l, or inspect a specific path with ls -l /path/to/file. Replace the example path in the commands below with the real path. If the name contains spaces, put the path in quotes, such as "/home/sam/Project Notes.txt".
  2. Read the current permissions and owner. In the output of ls -l, a permissions string such as -rw-r----- appears before the link count, owner, group, size, and modification date. The first character indicates the file type; for a directory it is d. The next three characters are the owner’s permissions, the next three the group’s, and the final three permissions for other users. The owner and group names appear after the permissions.
  3. Choose the narrowest permissions that work. The letters mean read (r), write (w), and execute (x). For a regular file, execute allows it to run as a program or script. For a directory, read lets a user list its entries, write allows creating or removing entries, and execute allows entering or traversing it. Decide which account categories need each permission before editing.
  4. Change permissions with symbolic notation. Use chmod with categories and an operation. For example, chmod u+x script.sh adds execute permission for the owner. chmod g-w report.txt removes write permission from the group. Categories include u (owner), g (group), o (others), and a (all); operations are + to add, - to remove, or = to set exactly. For example, chmod u=rw,go=r report.txt sets the owner to read and write and group and others to read only.
  5. Alternatively, set permissions with an octal mode. Each permission has a value: read is 4, write is 2, and execute is 1. Add the values for each category and write owner, group, then others. For example, chmod 640 report.txt gives the owner read/write (6), the group read (4), and others no permissions (0). chmod 755 script.sh gives the owner full permissions and gives group and others read/execute. These are examples, not universal settings; choose based on the file’s purpose and required access.
  6. Change the owner or group when needed. Use chown username /path/to/file to change the owner. To set both owner and group, use chown username:groupname /path/to/file. To change only the group, use chgrp groupname /path/to/file. Replacing an existing owner or group generally requires administrator privileges, so use sudo only when appropriate, for example sudo chown sam:editors /srv/shared/notes.txt.
  7. Apply changes to a directory tree cautiously. Add -R for a recursive owner or permission change, such as sudo chown -R sam:editors /srv/project. This affects everything inside the directory, so verify the path first. Avoid blindly applying one permission mode recursively: directories need execute permission for traversal, while ordinary files often should not be executable. If you need different settings for files and directories, handle them separately and inspect the results.
  8. Verify the result. Run ls -l /path/to/file again to check the owner, group, and displayed permissions. For more detail, use stat /path/to/file. Test access as the intended user when practical; seeing the expected mode does not guarantee that other access controls, such as directory permissions or ACLs, allow the operation.

Common Mistakes

  • Using chmod 777 as a quick fix: It grants read, write, and execute permissions to everyone. That can expose or alter files unnecessarily. Grant only the access required.
  • Confusing a file’s permissions with its parent directory’s: A user may need execute permission on every directory in a path to reach the file. Check the whole path, not just the final file.
  • Mixing up chmod and chown: chmod changes access bits; chown changes the owner or group. One does not automatically do the other.
  • Running recursive commands on the wrong path: A misplaced -R can change many unrelated files. Confirm the target with pwd and ls before proceeding.
  • Assuming execute means the same thing everywhere: On a file it permits execution; on a directory it permits traversal. Removing directory execute permission can make its contents inaccessible even if those contents remain readable.

Tips

  • Use symbolic modes when you want to adjust one permission without replacing the others, such as chmod o-r file.
  • Use octal modes when you want to specify all three permission groups at once. Remember the order is owner, group, others.
  • Quote paths containing spaces or shell-special characters. Alternatively, type the beginning of a path and use Tab completion.
  • For shared directories, consider setting an appropriate group and group permissions rather than granting access to all users. Ask the system administrator which group is intended.
  • Linux filesystems may also use access control lists (ACLs), and mount options or security policies can affect access. If the displayed permissions look correct but access still fails, check for these controls.

Troubleshooting

“Permission denied” when changing ownership: You may not own the file or have administrator rights. Check with ls -l, then use sudo only if you are authorized. On a managed system, ask its administrator.

The command says the file does not exist: Check spelling, capitalization, and the current directory. Linux paths are case-sensitive. Use an absolute path or quote a path containing spaces.

Permissions look right but access still fails: Check each parent directory with ls -ld /path /path/to /path/to/file. Also check ACLs with getfacl /path/to/file if that command is installed, and verify that you are using the expected account and group.

A program still will not run: Confirm the file has execute permission for your user and that its parent directories are traversable. A script may also need a valid interpreter line and suitable line endings; permissions alone cannot fix a malformed script.

You changed too much: There is no universal command that restores previous permissions. If you recorded the original settings, reapply them. Otherwise, stop making changes and consult a system administrator or restore from a backup; guessing can leave files exposed or unusable.

Frequently Asked Questions

How do I give the file owner execute permission?
Run chmod u+x filename. This adds execute permission for the owner without changing the other permission bits.

What does chmod 644 mean?
The owner can read and write; the group and others can read. It is a common pattern for non-executable files that should be publicly readable, but it is not suitable for every file.

How do I change the owner of a file?
Run sudo chown username /path/to/file if you have authorization. Check the result with ls -l.

How can I change a directory and everything inside it?
Use chown -R or chmod -R with the correct target. Review the path carefully first, because recursive changes affect all nested files and directories.

Can I use chmod to change who owns a file?
No. Use chown to change the owner or group; use chmod to change read, write, and execute permissions.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.