Introduction
In this article and associated video, we will cover Linux file and directory ownership and permissions.
Every file and directory on a Linux system is associated with an owner and a group. This is how the system identifies who has control over the files and directories and who is part of the group that can access it.
-
- Owner : The user who owns a file or directory. Customarily the owner has the ability to add/edit/delete files/directories and modify their permissions.
- Group : A set of users who share the same permissions for a file or a directory.
- Others : Everyone else who is not the owner or a member of the group.
1) Symbolic Notation : when permissions and users are represented by letters.
Permissions:
-
- “r” : Read
- “w” : Write
- “x” : Execute
- “+” : Add a permission
- “–” : Remove a permission
- “=” : Set exact permissions
Ownership:
-
- Owner : u
- Group : g
- Others : o
Examples:
Add execute permission for the owner: $ chmod u+x file.txt
Remove write permission for others: $ chmod o-w file.txt
2) Numeric Notation
Each permission corresponds to a number:
-
- “r = 4” : read
- “w = 2” : write
- “x = 1” : execute
The permissions for each user (owner, group, others) are represented as a three-digit number.
-
- “7” = “rwx” (read, write, execute)
- “6” = “rw-” (read, write)
- “5” = “r-x” (read, execute)
- “4” = “r–” (read)
- “3” = “-wx” (write, execute)
- “2” = “–w–” (read)
- “1” = “–x” (execute)
- “0” = “–––” (no permissions)
To set permissions numerically, you add the values for owner, group, and others. For example:
File : $ chmod 644 file.txt
-
- “6” (owner) = `rw-` (read, write)
- “4” (group) = `r–` (read)
- “4” (others) = `r–` (read)
Directory : $ chmod 755 <directory>
-
- “7” (owner) = `rwx` (read, write, execute)
- “5” (group) = `r-x` (read, execute)
- “5” (others) = `r-x` (read, execute)
3) File Ownership
-
- Owner : The user who owns a file.
- Group : A set of users who share the same permissions for a file.
- Others : Everyone else who is not the owner or a member of the group.
4) File Permissions
-
- Read (r) : Permission to open and read the contents of a file.
- Write (w) : Permission to modify or delete the contents of a file.
- Execute (x) : Permission to execute a file (i.e., run it as a program).
5) Directory Ownership
Owner (User): Each directory in Linux is associated with an owner, which is typically a user. The owner has specific permissions to read, write, and execute files within that directory, depending on the permissions set. The owner is usually the user who created the directory, but ownership can be changed.
Group : In addition to the owner, a directory is associated with a group. The group defines a set of users who share common access rights to the directory. Group members can have different permission levels compared to the owner or other users.
Permissions
Linux uses three main types of permissions for directories:
-
- Read (
r
) : The user can list the contents of the directory. - Write (
w
) : The user can create, delete, or rename files in the directory. - Execute (
x
) : The user can navigate into the directory and access files or subdirectories.
- Read (
These permissions are assigned to three categories of users:
-
- Owner: The user who owns the directory.
- Group: Users who are members of the group assigned to the directory.
- Others: All other users on the system.
6) Directory Permissions
-
- Read (r) : for directories, this allows listing the files inside the directory.
- Write (w) : for directories, this allows adding, deleting, or renaming files within the directory.
- Execute (x): for directories, this allows accessing the directory, meaning the ability to enter the directory and work with its file.
7) Changing Ownership
To change the owner and group of a file or directory, you use the `chown` command.
Example : $ sudo chown <newuser>:<newgroup> file.txt (could be a directory).
8) Changing Permissions
To modify the permissions, you use the “chmod” command. Permissions can be set using symbolic notation (letters) or numeric notation (numbers).
-
- Read (“r”) : Allows listing the contents.
- Write (“w”) : Allows modifying the directory’s contents (adding/removing files).
- Execute (“x”) : Allows accessing files inside the directory (entering the directory).
Example: sudo chmod 755 <directory> (could be a file)
9) Listing Permissions
File: One the command line issue the command “vdir”.
Directories: One the command line issue the command “vdir -id”.
Conclusion
This article and associated video cover Linux file and directory ownership and permissions and how to change them.
Changing file ownership and permissions can be completed by one of two approaches – Symbolic Notation or Numeric Notation.
To change file or directory ownership we use the chown command.
To change file or directory permissions we use the chmod command.