Chown and Chmod commands for managing file permissions in Linux system

Time:2020-5-20

chown
Usage: chown [options]… [owner] [: [group]] file
Or: chown [options]… — Reference = reference file
Change the owner and / or group of each file.
When using the — referenbce parameter, change the owner and group of the file to be the same as the specified reference file.

– C, — changes is similar to verbose, but only displays results when there are changes
— reference is affected by the object indicated by the symbolic link, not the symbolic link itself
– H, – no reference affects the symbolic link itself, not the destination indicated by the symbolic link
(this option is useful when the system supports changing the owner of a symbolic link)
— from = current owner: current group
Change the
There are people and groups. One of them can be omitted, and the omitted attribute will not
Need to match the original properties.
— no preserve root does not treat “/” (default value)
— preserve root does not allow recursive operation on “/”
– F, — silent, — quiet removes most error messages
— Reference = reference uses the group to which the reference belongs, not the specified value
– R, – recursive handles all files and subdirectories recursively
– V, – verbose displays diagnostic information for all files processed

The following options are used to set how to traverse the directory architecture when the – R option is specified.
If you specify more than one option, only the last one will take effect.

– h if the command line argument is a symbolic link to the directory, traverse the symbolic link
– L traverses every symbolic link to the directory encountered
– P does not traverse any symbolic links (default)

— help display this help message and exit
— version displays version information and exits

If no owner is specified, it does not change. If the group is not specified, it will not be changed. However, if you add
Group changes to the primary group of the specified owner when:. Owners and groups can be numbers or names.

Example 1:

Copy code

The code is as follows:

[ [email protected] ~]# chown zhangy:zhangy nginx.conf #Will nginx.conf Change the users and groups to Zhangy, Zhangy
[[email protected] ~]# ls -al |grep nginx.conf
-Rw-r — R — 1 May 3 15:21 nginx.conf
[ [email protected] ~]# chown -R zhangy:zhangy www #Change the WWW directory, users and groups to Zhangy, Zhangy
[[email protected] ~]# ls -al |grep ww
Drwxr-xr-x 2 Zhangy 4096 May 3 15:20 www – change the WWW directory to root
[[email protected] ~]# chown root nginx.conf
[[email protected] ~]# ls -al |grep nginx.conf
-Rw-r — R — 1 root Zhangy 0 may 3 15:21 nginx.conf

Example 2:

Copy code

The code is as follows:

[[email protected] database]# ll
Total dosage 4592
-Rw-r — R — 1 root 2466 July 23 18:02 1.html
-Rw-r — R –. 1 tank tank 4099771 May 28 14:42 28 toplearning.sql
-Rw-r — R –. 1 tank tank 596069 May 29 00:07 toplearning.tar.gz
Copy code

The code is as follows:

[ [email protected] Database] chown. Tank 1.html change group only
[[email protected] database]# ll
Total dosage 4592
-Rw-r — R — 1 root tank 2466 July 23 18:02 1. HTML group changed
-Rw-r — R –. 1 tank tank 4099771 May 28 14:42 28 toplearning.sql
-Rw-r — R –. 1 tank tank 596069 May 29 00:07 toplearning.tar.gz

chmod
Usage: Chmod [options]… Mode [, mode]… File
Or: Chmod [options]… Octal mode file
Or: Chmod [options]… — Reference = reference file
Change the mode of each file to the specified value.

– C, — changes is similar to — verbose, but only displays results when there are changes
— no preserve root does not treat root directory specially (default)
— preserve root forbids recursive operation on the root directory
– F, — silent, — quiet removes most error messages
– V, – verbose displays diagnostic information for all files processed
— Reference = reference file uses the mode of specified reference file instead of the mode of self specified permission
– R, – recursive changes all files and subdirectories recursively
— help display this help message and exit
— version displays version information and exits

Each mode should belong to this type of form “[ugoa] * ([- +] ([rwxxst] * | [Ugo]) +”.

Operation alignment

U file ownership
G permissions of users in the same group
O other user rights
A all users (including the above three)

Permission setting

+ add permissions
– cancel permission
= unique setting permission

Permission category

R read permission
W write permission
X execution permission
X indicates that only when the file is a subdirectory or has been set to be executable.
S file owner and group ID
L lock the file so that other users cannot access it

   r–>4
   w–>2
   x–>1

Example 1:

Copy code

The code is as follows:

[ [email protected] ~]# chmod ugo+r nginx_ bak.conf #Accessible to all
[ [email protected] ~]# chmod a+r nginx_ bak.conf #Accessible to all
[ [email protected] ~]# chmod ug+w,o-w nginx_ bak.conf #If it is set as the owner of the file, it can be written by the same group as the owner, but it cannot be written by other people
[ [email protected] ~]# chmod u+x nginx_ bak.conf #Creator has execution permission
[ [email protected] ~]#Chmod – r a + R. / www / ා make all files and subdirectories under www accessible to anyone
[ [email protected] ~]# chmod a-x nginx_ bak.conf #Retrieve all users’ nginx_ bak.conf Execution permission of
[ [email protected] ~]# chmod 777 nginx_ bak.conf #Everyone can read, write, execute

Example 2:

Copy code

The code is as follows:

# chmod a+s test.ppt #CHMOD G + s, set an alternate group ID for a file
# ls -al |grep test.ppt
-Rwsr SR –. 1 tank tank 2320384 November 18 19:29 test.ppt

The first s indicates that the user override ID (suid) has been set.
The second s indicates that the substitution group ID (sgid) has been set.

In this way, each user running the program will be given the same valid user ID as the program owner and the same valid group ID as the user’s group.