file permissions
- What are File Permissions?
- File permissions determine who can read, write, or execute a file or directory.
- Every file has three permission types:
- Read (r): Ability to view the contents of a file or list a directory’s contents.
- Write (w): Ability to modify or delete a file or its contents.
- Execute (x): Ability to run a file as a program or access a directory.
-
Viewing Permissions
Usels -l
to see permissions for files and directories:$ ls -l
Example output:
-rwxr-xr-- 1 user group 4096 Oct 11 10:15 script.sh
- Permission Breakdown:
-rwxr-xr--
- The first character:
-
(file) ord
(directory). - Owner:
rwx
(read, write, execute for the file owner). - Group:
r-x
(read, execute for users in the file's group). - Others:
r--
(read-only for all others).
- The first character:
- Permission Breakdown:
-
Basic Syntax:
chmod <permissions> <file>
-
1. Using Numeric Mode
- Permissions can be represented by numbers:
4
= Read (r)2
= Write (w)1
= Execute (x)
- Combine these for different roles:
- Owner | Group | Others: 777 (rwx for all), 644 (rw- for owner, r-- for group and others)
Example:
chmod 755 script.sh
This gives the owner full access (rwx), while group and others get read and execute access (r-x).
- Permissions can be represented by numbers:
-
2. Using Symbolic Mode
- Permissions can also be changed with symbols:
u
= user/ownerg
= groupo
= othersa
= all (user, group, others)
Example:
chmod u+x script.sh
This adds execute permission to the owner.
Example:
chmod g-w file.txt
This removes write permission for the group.
- Permissions can also be changed with symbols:
-
Give execute permission to all users:
chmod a+x script.sh
-
Remove write permission for others:
chmod o-w file.txt
-
Set full permissions for the owner, read-only for group and others:
chmod 744 file.txt
-
chown
allows you to change the owner and group of a file.chown <owner>:<group> <file>
-
Example: Change the owner to
user1
and group tostaff
:chown user1:staff file.txt
-
Change ownership of a directory recursively:
chown -R user1:staff /path/to/directory
- Use
stat
to view detailed file permissions and other metadata:This will show the numeric mode (e.g., 0644) and other attributes of the file.stat file.txt
-
Setuid (Set User ID): Allows a file to be executed with the permissions of its owner.
chmod u+s file.sh
-
Setgid (Set Group ID): Ensures new files created in a directory inherit the group of the directory.
chmod g+s directory/
-
Sticky Bit: Prevents users from deleting files in a directory unless they own them.
chmod +t /shared_directory
This expanded section provides an in-depth understanding of file permissions, chmod
, chown
, and special permission settings in Linux.