Modify file permissions (ownership) under Linux

Modify file permissions (ownership) under Linux

Linux and Unix are multi-user operating systems, so the implementation of file permissions and ownership is very necessary; each file mainly deals with three sets of permissions, namely user, group, and other users.

User (u) is the owner of the file and usually has all file operation permissions

A user group (g) is a collection of multiple users, which may have partial access rights to files, equivalent to sharing files between users.

Other (o) refers to anyone other than the file owner and group members.

Use ls -l to display various information including file type, permissions, owner, and group in the current directory;

The first column means: -(filetype)---(user)---(group)---(other), each group of permissions corresponds to a three-digit binary number, the first bit indicates read (r) permission, the second bit indicates write (w) permission, and the third bit indicates execute (x) permission (if the file is executable); when the corresponding permission is obtained, the corresponding bit is set to 1 (otherwise 0), indicating that the current object has this permission

1: User can read, write and execute: -rwx------. Generally speaking, for data files, the user's permission is rw-, and the permission of executable files and scripts is rwx. For users, there is also a special permission called setuid, which can appear in the position of permission x. The setuid (S) permission allows the executable file to be executed with the permissions of its owner, even if the executable file is run by other users. If the permission is granted, the sequence is: -rwS------

2: User group read, write and executable: ----rwx---, For the user group, it has a special permission called setgid(S) which allows us to use any valid user group to run the file, provided that the group needs to have the same permissions as the file's required group,, ----rwS---

3: Other users: -------rwx

1: Use chmod (change mode) to set file permissions:

--x=1;-w-=2;r--=4; rx=5;rw-=6;rwx=7;

If you set permissions: rwxrw-r--

1:chomd 764 filepath

2:chmod u=rwx, g=rw, o=r filepath; chmod ugo=r, g=w filepath

To add or remove permissions for a file:

1: Add executable permissions to users and user groups (+):

chmod u+x, g+x filepath

2: Delete (-) the executable permissions of the user group:

chmod gx fielpath

chmod g=ur filepath means to set the permissions of the user group to the same permissions as the user, but delete the r permission

3: Add/remove executable permissions for all permission categories (a) (i.e. user, user group, other users)

chmod a+x filepath

2. Change Ownership

1: Use chown to change the ownership of a file. Ordinary users do not have the authority to change the owner of other people's files, nor do they have the authority to change the owner of their own files to someone else. Only the system administrator (root) has such authority: chown user:group filepath (user, group is the new user and user group)

2: Set the sticky bit for the directory

Sticky bit: Directories have a special permission called the sticky bit. If the sticky bit is set for a directory, only the user who created the directory can delete files in the directory. It appears in place of the execute permissions for the Other User Group. When the execution permission is set -------rwt, when the execution permission is not set -------rwT, chmod a+t dirname

3: Set ownership and file permissions recursively, using option -R;

chmod 764 . -R

chown user:group . -R

Supplement: File type: - ordinary file d directory (directory); c character device (char); b block device (block); l symbolic link (link); s socket (socket); p pipe (pipe)

Summarize

The above is the editor’s introduction to modifying file permissions under Linux. I hope it will be helpful to everyone!

You may also be interested in:
  • Detailed analysis of the chmod command to modify file permissions under Linux
  • Linux common commands chmod to modify file permissions 777 and 754
  • Linux server programming utime() function to modify file access time
  • How to modify the user and group of a file in Linux

<<:  Detailed explanation of MySQL covering index

>>:  Vue+js click arrow to switch pictures

Recommend

Detailed installation and configuration tutorial of MySQL 5.7 under Win10

1. Unzip MySQL 5.7 2. Create a new configuration ...

Specific use of Linux dirname command

01. Command Overview dirname - strip non-director...

How to use node to implement static file caching

Table of contents cache Cache location classifica...

Summary of English names of Chinese fonts

When using the font-family property in CSS to ref...

Detailed steps to install Nginx on Linux

1. Nginx installation steps 1.1 Official website ...

Windows DNS server exposed "worm-level" vulnerability, has existed for 17 years

Vulnerability Introduction The SigRed vulnerabili...

What can I use to save you, my table (Haiyu Blog)

Tables once played a very important role in web p...

Using loops in awk

Let's learn about different types of loops th...

How to view and close background running programs in Linux

1. Run the .sh file You can run it directly using...

JavaScript file loading and blocking issues: performance optimization case study

Let me start with a question: When writing an HTM...

Mysql NULL caused the pit

Using NULL in comparison operators mysql> sele...

Two ways to clear float in HTML

1. Clear floating method 1 Set the height of the ...

MySQL 5.7.21 installation and password configuration tutorial

MySQL5.7.21 installation and password setting tut...