Linux file permission analysis: from entry to practice

Linux file permissions are a very important part of the Linux system. It controls the permissions (read, write, execute) and ownership (users, groups) of files and directories. Users must understand the concept of file permissions when using the Linux system. , master how to modify file permissions and prevent accidental access. This article will explain in depth the basics and practical skills of Linux file permissions.

The Basics: File Permissions and File Ownership
In a Linux system, each file and directory has an owner and a set of permissions. There are three types of permission control: read, write, and execute. Owners have the right to control the read, write, and execute permissions of any file or directory under their name, and also have the right to control the ownership of the file or directory. Therefore, in the Linux system, the permissions and access control of file operations depend entirely on the ownership and permissions of the file or directory.

Permission type:
- Read permission: Can view file content, directory listing, file permissions, but cannot modify files.
-Write permissions: Allows editing file content and creating, deleting and renaming directories or files.
- Execution permission: Access files in the directory, which is the permission of executable files. Files and directories without this permission cannot be run.

File Ownership:
The ownership of files in Linux is very important because it is the key factor affecting the access of files. The file owner is the only user who can access, modify and perform any operations. In addition, there are group owners and nonfree users. Sets the owner and group members of a file to have read, write, and execute permissions. However, ordinary users cannot open or edit the file without read and write permissions.

Practical skills:
1. How to check file permissions?
Run the command: ls -l /pathtofile

This will display the file's name, owner, group owner, permissions for all users, creation date, and other details. Here is a concrete example:
-rw-r–r-- 1 root root 8065 Mar 23 2022 pythonfile.py

  • The first column - rw-wxr-x: This is the permission string, composed of r, w, x
    r - means read permission
    w - means write permission
    x - means execute permission
  • The second column - 2 stores the number of links to the file
  • Column 3 - Owner's name
  • Fourth column - Name of the owner group
  • Fifth column - the size of the file in bytes
  • Column 6 - Date and time the file was created
  • Column Seven - Name of the file

2. How to modify file permissions?
Run the command: chmod mode file

For example, you can give other users read permissions:
chmod o+r file
You can also set executable permissions for all users at the same time:
chmod a+x file

3. How to modify file ownership?
Run the command: chown owner:group file

For example, assign the file file to the www-data user:
chown www-data:www-data file

4. How do I change permissions for a folder and all its subfiles?
Run the command: chmod -R permissions folder_path

For example, to change the ownership of the my_folder folder and its subfolders:
chmod -R 755 my_folder

Summary:
Mastering Linux file permissions is one of the foundations of using the Linux system. You should learn how to view, modify and change the permissions of Linux files and directories. Set different access permissions for different users to protect your files and directories as much as possible. Familiarizing yourself with common permissions, examples, names of changes, and changing file ownership would be a good place to start.

Guess you like

Origin blog.csdn.net/canshanyin/article/details/130911239