How Can We Help?
Understanding and Setting Permissions with chmod and chown
Tutorial Overview
This tutorial covers file permissions and ownership management on Linux. Using chmod
and chown
, you’ll learn to set specific access permissions and change ownership of files and directories.
Prerequisites
- Root or sudo access.
Steps
Step 1: Understand Basic Linux File Permissions
1. File permissions are represented in three groups: owner, group, and others.
2. Each group has three possible permission types:
- Read (r): Allows reading the file or listing the directory contents.
- Write (w): Allows modifying the file or creating/deleting files in the directory.
- Execute (x): Allows executing the file or accessing files within a directory.
3. Permissions are represented as a string, for example: -rw-r–r–.
- The first character (–) indicates a regular file, while d denotes a directory.
- The following characters are grouped as owner, group, and others permissions.
Step 2: Changing Permissions with chmod
1. To set permissions, use either symbolic notation (+, –, =) or numeric notation (755, 644, etc.).
2. Example of Symbolic Notation:
- To give execute permission to the owner of a file:
chmod u+x filename
- To remove write permission for group members:
chmod g-w filename
- To set permissions exactly for each group:
chmod u=rwx,g=rx,o=r filename
1. Example of Numeric Notation:
- Each permission is assigned a number: read=4, write=2, and execute=1. The sum determines each group’s permission level:
- chmod 755 filename translates to:
- Owner: 7 (4+2+1 = rwx)
- Group: 5 (4+0+1 = r-x)
- Others: 5 (4+0+1 = r-x)
Step 3: Changing Ownership with chown
1. Change the Owner of a File:
sudo chown newowner filenam
2. Change the Owner and Group of a File:
sudo chown newowner:newgroup filename
3. Change Ownership Recursively (for Directories):
- To change ownership for all files and directories within a directory:
sudo chown -R newowner:newgroup /path/to/directory
4. Verify changes with ls -l:
ls -l filename
- This command shows the updated ownership and permissions.