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.
4for read permission2for write permission1for 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 filenamesets the permissions torwxr-xr-xfor the file namedfilename.chmod u+x filenameadds execute permission for the owner of the file.chmod g-w filenameremoves write permission for the group.chmod o=r filenamesets 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-xrepresents the file's permissions.1is the number of hard links to the file.useris the file's owner.groupis the file's group.12345is the file's size in bytes.Aug 13 12:34is the file's last modification date and time.filenameis 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 filenamechanges the owner of the file namedfilenametouser.chown user:group filenamechanges the owner of the file touserand the group togroup.chown :group filenamechanges the group ownership of the file togroup, leaving the user unchanged.chown user: filenamechanges 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-xrepresents the file's permissions.1is the number of hard links to the file.useris the file's owner.groupis the file's group.12345is the file's size in bytes.Aug 13 12:34is the file's last modification date and time.filenameis 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 (ordfor a directory).rwxshows the permissions for the file's owner.r-xshows the permissions for the group associated with the file.r--shows the permissions for others.1024is the file's size in bytes.Aug 13 12:34is the file's last modification date and time.filenameis 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.