Linux File Permissions and the chmod Command
Overview of Permissions in Linux File System
- The
chmodcommand is used to change permissions of files and directories in Linux. - Understanding permissions is vital for file security and access control.
Viewing Permissions
- Use
ls -lhato view detailed file listings -l: long listing format-h: human-readable file sizes-a: show all files, including hidden files- Hidden files begin with a period (
.)
Permission String Breakdown
- The permission string consists of four sections:
- File Type:
-: signifies a normal filed: signifies a directory
- User Permissions: Three characters indicating the permissions for the file owner. (e.g.,
rwx) - Group Permissions: Three characters for the group that owns the file.
- Other (World) Permissions: Three characters for all other users.
Permission Characters and Their Meanings
r: readw: writex: execute- A hyphen
-indicates that a permission is not granted.
Example Permissions Structure
- For a permission string like
-rwxr-xr--: rwxfor the user (read, write, execute)r-xfor the group (read, execute)r--for others (read only)
Understanding Ownership
- Ownership of files: User (often
root), Group. - Example:
rootownsansiblebootstrap.sh.
Changing Permissions with chmod
- Basic Command Structure:
chmod [options] [permissions] [filename] - Adding Permissions: Use
+
- Example:
chmod g+w ansiblebootstrap.sh(adds write permission for group)
- Removing Permissions: Use
-
- Example:
chmod g-w ansiblebootstrap.sh(removes write permission for group)
- Copying Permissions: Use
=to set permissions equal to another category.
- Example:
chmod g=u ansiblebootstrap.sh(group permissions set equal to user permissions)
- Combining Commands: Multiple operations can be applied in one command.
- Example:
chmod g-w,u-x,o-r ansiblebootstrap.sh
Numeric Representation of Permissions
- Permissions can also be set using a three-digit numerical system:
- Values:
r= 4w= 2x= 1
- Example:
chmod 644 ansiblebootstrap.sh - First digit for user:
6(4+2) =rw- - Second digit for group:
4=r-- - Third digit for others:
4=r--
File vs. Directory Permissions
Files:
r: View contents, read the file.w: Modify contents (write).x: Execute the file if it's a script or program.Directories:
r: List contents of the directory.w: Add or remove files in the directory.x: Navigate into the directory (usingcd). Without it, you get a "Permission denied" error.
Summary
- Understanding and modifying file permissions is essential for effective file system management in Linux.
- The
chmodcommand, using both symbolic and numeric methods, allows for flexible permission settings, promoting security and access management.