Files and Permissions in Linux - Notes By ShariqSP
Understanding Files and Permissions in Linux
What are Linux File Permissions?
In Linux, file permissions control who can read, write, or execute a file. Permissions are fundamental to system security and user privacy. Each file or directory has a set of permissions associated with it, determining which users or groups can access or modify the file.
Understanding File Permission Representation
Linux file permissions are represented in a symbolic or numeric format. The symbolic format uses letters to denote permissions, while the numeric format uses octal numbers. Here's how they work:
- Symbolic Format: Permissions are shown as a string of characters, such as
rwxr-xr--
. Each character represents a permission:r
(read): Allows viewing the contents of the file.w
(write): Allows modifying the file.x
(execute): Allows running the file as a program.
- Numeric Format: Permissions are represented by a three-digit octal number, such as
755
. Each digit corresponds to a set of permissions:- The first digit represents the owner’s permissions.
- The second digit represents the group’s permissions.
- The third digit represents others’ permissions.
4
for read permission2
for write permission1
for execute permission
Changing File Permissions
You can change file permissions using the chmod
command. This command allows you to modify permissions using either symbolic or numeric notation. Here are some examples:
chmod 755 filename
chmod u+x filename
chmod g-w filename
chmod o=r filename
In these examples:
chmod 755 filename
sets the permissions torwxr-xr-x
for the file namedfilename
.chmod u+x filename
adds execute permission for the owner of the file.chmod g-w filename
removes write permission for the group.chmod o=r filename
sets read-only permission for others.
Viewing File Permissions
You can view file permissions using the ls -l
command. This command lists files and directories with detailed information, including permissions. The output looks like this:
ls -l
-rwxr-xr-x 1 user group 12345 Aug 13 12:34 filename
In this output:
-rwxr-xr-x
represents the file's permissions.1
is the number of hard links to the file.user
is the file's owner.group
is the file's group.12345
is the file's size in bytes.Aug 13 12:34
is the file's last modification date and time.filename
is the name of the file.
Changing File Ownership
The chown
command is used to change the ownership of a file or directory. Ownership can be assigned to a user and/or a group. Here are some examples:
chown user filename
chown user:group filename
chown :group filename
chown user: filename
In these examples:
chown user filename
changes the owner of the file namedfilename
touser
.chown user:group filename
changes the owner of the file touser
and the group togroup
.chown :group filename
changes the group ownership of the file togroup
, leaving the user unchanged.chown user: filename
changes the owner of the file touser
, leaving the group ownership unchanged.
Note that only the superuser (root) can change the ownership of files that are not owned by the current user.
Viewing File Permissions
You can view file permissions using the ls -l
command. This command lists files and directories with detailed information, including permissions. The output looks like this:
ls -l
-rwxr-xr-x 1 user group 12345 Aug 13 12:34 filename
In this output:
-rwxr-xr-x
represents the file's permissions.1
is the number of hard links to the file.user
is the file's owner.group
is the file's group.12345
is the file's size in bytes.Aug 13 12:34
is the file's last modification date and time.filename
is the name of the file.
Differences Between chown
and chmod
The chown
and chmod
commands are both used for managing file permissions, but they serve different purposes:
Aspect | chown |
chmod |
---|---|---|
Purpose | Changes the owner and/or group of a file or directory. | Changes the permissions of a file or directory. |
Syntax | chown [user][:group] file |
chmod [permissions] file |
Usage | Used to transfer ownership to another user or group. | Used to specify who can read, write, or execute a file. |
Examples | chown john:admins file.txt |
chmod 755 file.txt |
Who Can Execute | Only root or a user with appropriate privileges. | Any user with appropriate permissions on the file. |
Understanding Permission Representation
File permissions are displayed in a format like -rwxr-xr--
, where:
-
indicates a regular file (ord
for a directory).rwx
shows the permissions for the file's owner.r-x
shows the permissions for the group associated with the file.r--
shows the permissions for others.1024
is the file's size in bytes.Aug 13 12:34
is the file's last modification date and time.filename
is the name of the file.
Conclusion
Understanding and managing file permissions is crucial for maintaining system security and ensuring that users have the appropriate access to files and directories. Mastering file permissions allows for better control over who can view, modify, or execute files in a Linux environment.