Permissions
Understanding Permissions
In Unix-like operating systems, permissions control who can read, modify, or execute files and directories. This is crucial for managing security and access on your system. The way permissions are handled in Unix is somewhat similar to Windows, but the specifics differ.
In Unix-like systems, permissions can be set for the file's owner (u
), the group associated with the file (g
), and all other users (o
). Below are various ways to modify these permissions using the chmod
command.
Basic Concepts
- Ownership: Every file and directory has an owner and a group associated with it. The operating system tracks these using numeric IDs.
- Categories: Each user falls into one of three categories for any file:
- Owner: The user who owns the file.
- Group: Users who are part of the file’s associated group.
- Others: Everyone else on the system.
Permission Types
- Read (
r
): Allows viewing the contents of the file. - Write (
w
): Allows modifying the file. - Execute (
x
): Allows running the file if it is a program or script.
- The first character indicates the type (e.g.,
-
for files,d
for directories). - The next three characters are the owner's permissions (aka the "user").
- The following three are the permissions for the group the user belongs to.
- The last three are the permissions for all other users.
Viewing Permissions
Use the ls -l
command to view detailed file permissions. The output includes a string like -rwxr-xr-x
that shows permissions.
-
For example,
- Owner: Can read, write, and execute.
- Group: Can read and execute, but not write.
- Others: Can read and execute, but not write.
-rwxr-xr-x
means:
Changing Permissions
Grant Read Permission
This command gives the file's owner read access. The resulting permissions would be-r--------
.
$ chmod u+r filename.txt
Grant Write Permission
This command adds write permission for the owner. The file's permissions will then be --w-------
.
$ chmod u+w filename.txt
Grant Execute Permission
This command allows the owner to execute the file. The permissions would be updated to ---x------
.
$ chmod u+x filename.txt
Grant Read, Write, and Execute Permissions
This command provides full access (read, write, and execute) to the file's owner. The file permissions will be -rwx------
.
$ chmod u+rwx filename.txt
Applying Permissions to the Group and Others
For the Group
This grants the group read, write, and execute permissions, resulting in ----rwx---
.
$ chmod g+rwx filename.txt
For Others
This command allows all other users to read, write, and execute the file, setting permissions to -------rwx
.
$ chmod o+rwx filename.txt
Combine Permission Changes
This updates permissions to -rwx-rw-r--
, providing different levels of access to the owner, group, and others.
$ chmod u+rwx,g+rw,o+r filename.txt
Setting Exact Permissions
To set permissions precisely, use the equal sign (=
) to define permissions explicitly. For example:
$ chmod u=rw,g=rw,o=r filename.txt
This command configures permissions to -rw-rw-r--
, ensuring read and write access for the owner and group, and read-only access for others.
Using Numerical Permission Codes
Numerical codes offer a compact way to set file permissions. Each digit represents permissions for the owner, group, and others, respectively.
Read Permission Only
$ chmod 400 filename.txt
This sets permissions to -r--------
, allowing only the owner to read the file.
Read Permission for Owner and Group
$ chmod 440 filename.txt
This results in -r--r-----
, enabling read access for both the owner and the group.
Read Permission for Everyone
$ chmod 444 filename.txt
This configuration, -r--r--r--
, allows all users to read the file. Here, the number 4
signifies read permission.
Read and Write Permissions
$ chmod 664 filename.txt
This sets permissions to -rw-rw-r--
, giving both the owner and the group read and write access.
Read, Write, and Execute Permissions
$ chmod 774 filename.txt
-rwxrwxr--
, allowing full access for the owner and group.
Full Access for Owner Only
$ chmod 700 filename.txt
This grants full permissions to the owner (-rwx------
) and none to others.
Typical Permissions for Directories
For directories, 755
results in drwxr-xr-x
, providing read, write, and execute access to the owner, and read and execute access to the group and others.
$ chmod 755 MyDir
See https://alvinalexander.com/linux-unix/linux-chmod-command-permissions-file-directories/ for more information and examples.