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

Tips for using DIV container fixed height in IE6 and IE7

There are many differences between IE6 and IE7 in ...

Summary of 6 Linux log viewing methods

As a backend programmer, you deal with Linux in m...

MySQL view principles and basic operation examples

This article uses examples to illustrate the prin...

Echarts implements switching different X-axes in one graph (example code)

Rendering If you want to achieve the effect shown...

The front end creates and modifies CAD graphics details through JavaScript

Table of contents 1. Current situation 2. Create ...

JavaScript to implement slider verification code

This article shares the specific code of JavaScri...

js to achieve simple drag effect

This article shares the specific code of js to ac...

Implementation of IP address configuration in Centos7.5

1. Before configuring the IP address, first use i...

Detailed explanation of Linux system directories sys, tmp, usr, var!

The growth path from a Linux novice to a Linux ma...

Detailed explanation of the use of various MySQL indexes

1. Slow query log 1.1 MySQL log types Logs are us...

HTML sample code for implementing tab switching

Tab switching is also a common technology in proj...

Teach you how to install mysql database on Mac

Download MySQL for Mac: https://downloads.mysql.c...

Teach you to create custom hooks in react

1. What are custom hooks Logic reuse Simply put, ...