Linux File Permissions Commands
Overview
File permissions in Linux control the access to files and directories. Permissions determine who can read, write, or execute files. This document covers basic commands for viewing and modifying file permissions.
Viewing Permissions
ls -l
Lists files and directories with detailed information, including permissions.
Example
ls -l
# Output: Lists files and directories with permissions, owner, group, size, and modification date
# Example output:
# -rwxr-xr-- 1 user group 1234 Aug 3 10:00 example.txt
Understanding File Permissions
File permissions are represented by a string of 10 characters:
- File Type:
-
(regular file),d
(directory),l
(symlink) - Owner Permissions:
rwx
(read, write, execute) - Group Permissions:
r-x
(read, execute) - Others Permissions:
r--
(read)
Changing Permissions
chmod
Changes the permissions of a file or directory.
Example
chmod 755 example.txt
# Sets the permissions of 'example.txt' to rwxr-xr-x (owner: read, write, execute; group and others: read, execute)
Numeric Mode
- 7: rwx (read, write, execute)
- 6: rw- (read, write)
- 5: r-x (read, execute)
- 4: r-- (read)
- 3: wx- (write, execute)
- 2: w-- (write)
- 1: --x (execute)
- 0: --- (no permissions)
Example
chmod 644 example.txt
# Sets the permissions of 'example.txt' to rw-r--r-- (owner: read, write; group and others: read)
Symbolic Mode
- u: user (owner)
- g: group
- o: others
- a: all
Use +
to add permissions, -
to remove, and =
to set exact permissions.
Example
chown
Changes the owner and/or group of a file or directory.
Example
sudo chown john:admins example.txt
# Changes the owner of 'example.txt' to 'john' and the group to 'admins'
chgrp
Changes the group ownership of a file or directory.
Example
Special Permissions
Setuid (Set User ID)
When set on an executable file, the process runs with the permissions of the file's owner.
Example
Setgid (Set Group ID)
When set on a directory, new files and directories inherit the group of the directory.
Example
Sticky Bit
When set on a directory, only the file owner can delete files within that directory.
Example
Summary
Understanding and managing file permissions is crucial for maintaining security and proper access control in Linux. These commands allow you to view and modify permissions, ownership, and special attributes of files and directories. For more detailed information on each command, refer to the Linux manual pages.