Understand the chown and chmod commands

These two are easy to get confused for beginners. Here, ch is actually a shorthand for change, and their meanings are:
chown-》 change own Change owner
chmod-》 change mod (mod does not know whether it is shorthand) to change file permissions


1. The
purpose of the chown command : change the owner or group of the file. The command is composed of the words change owner.
Examples of use:
1. Change the owner of the file: 
chown jim
program.c The owner of the file program.c is changed to jim. As the owner, jim can use the chmod command to allow or deny other users access to program.c.
2. Change the owner of the directory:
chown -R   john: build / tmp / src
Change the owner and group of all files in the directory / tmp / src to the user john and group build
-R Recursively change the specified directory and the following The owner of all subdirectories and files. 
-v Show the work done by the chown command.

(Eg.chown –R root: root rootfs changes the permissions and groups of the
rootfs folder and its subdirectories to root chown –R liufan: liufan-desktop rootfs changes the owner and group of the directory rootfs folder and subdirectories to User
liufan and group liufan-desktop)

2. Chmod command
Purpose: to change the access rights of files or directories.
This command has two usages:
one is the text setting method containing letters and operator expressions; the other is the number setting method containing numbers.
1. Text setting method
chmod [who] [+ |-| =] [mode] File name
The meaning of each option in the command is: the
operation object who can be any one of the following letters or a combination of them:
      u means "user (User) "is the owner of the file or directory.
      g means "group users", that is, all users with the same group ID as the file owner.
      o means "other (others) user".
      a means "all (all) users". It is the system default.

The operation symbol can be:
      + Add a certain permission.
      -Cancel a permission.
      = Give the given permission and cancel all other permissions (if any).

The permissions indicated by setting mode can be any combination of the following letters:
      r readable.
      w is writable.
      x executable.
      X The x attribute is appended only if the object file is executable by some users or the object file is a directory.
      s Set the process owner or group ID as the file owner of the file when the file is executed. The mode "u + s" sets the user ID bit of the file, and "g + s" sets the group ID bit.
      t Save the text of the program on the exchange device.
      u Has the same permissions as the owner of the file.
      g has the same permissions as the user in the same group as the owner of the file.
      o Has the same permissions as other users.

File name: A list of files whose permissions are to be changed separated by spaces. Wildcards are supported.
Multiple permissions can be given in one command line, separated by commas. For example: chmod g + r, o + r example
enables the same group and other users to have read permission to the file example.
2. Number setting method
We must first understand the meaning of the attributes represented by numbers : 0 means no authority, 1 means executable authority, 2 means writeable authority, 4 means readable authority, and then add them up. Therefore , the format of the numeric attribute should be 3 octal numbers from 0 to 7 , in the order of (u) (g) (o). 

For example, if you want the owner of a file to have "read / write" permissions, you need to set 4 (read) + 2 (write) = 6 (read / write).
The general form of the number setting method is:
chmod [mode] file name

There is another way to calculate the meaning of the digital representation is to take the remainder of 2 and find the difference : (u) (g) (o) corresponding to rwx rwx rx ==》 111 1111 101 ==》 775

Reprinted from: https://www.cnblogs.com/snowbook/p/5663007.html

Published 27 original articles · 25 praises · 120,000 views

Guess you like

Origin blog.csdn.net/weixin_38293850/article/details/89839120