Linux: Summary of chmod and chown usage

 

chmod

The chmod command changes the read, write and execute permissions of a file.

File permissions can be viewed through ls -aor ll, there are 10 characters before each file, the first is da folder, otherwise it is a stroke. The latter three groups rwxare read, write and execute permissions; the three groups are respectively It is the corresponding authority of the user himself, the same group and others rwx. Without execution authority, scripts and programs cannot run directly; without write authority, files cannot be generated and saved; without read authority, even access is difficult. General file permissions Yes 755, it will be introduced below.

 

There are two ways of expressing permissions, the rwxway and the number. The r permission represents 1, the w permission represents 2, the x permission represents 4, and the no permission represents 0. rwxThe value of the permission number is accumulated, which is the corresponding permission of a user/group/other people , for example 775on behalf of users and groups with rwxpermissions, while others only have rxpermissions and no write permissions.

 

Command format: chmod (option) (permission mode file)

 

Options

  • -c或--changes: The effect is similar to the "-v" parameter, but only the changed part is reported;
  • -f或--quiet或--silent: Forced execution, no error message is displayed;
  • -R或--recursive: Recursive processing, processing all files and subdirectories in the command directory together;
  • -v或—-verbose: Display the instruction execution process;
  • --reference=<参考文件或目录>: Set all the groups to which the specified file or directory belongs to the same group as the reference file or directory;

 

Privilege Mode

  • <权限范围>+<权限设置>: Enable the permission settings of this option for files or directories within the permission scope;
  • <权限范围>-<权限设置>: Turn off the permission setting of this option for the file or directory of the permission scope;
  • <权限范围>=<权限设置>: The option permission setting for the file or directory that specifies the permission scope;

 

The scope of authority is:

  • u: User, the owner of the file or directory;
  • g: Group, that is, the group to which the file or directory belongs;
  • o: Other, except for the file or directory owner or group, other users belong to this scope;
  • a: All, that is, all users, including the owner, the group they belong to, and other users;

 

where permissions are set to:

  • r: read permission, the number code is "4";
  • w: Write permission, the digital code is "2";
  • x: Execute or switch permissions, the digital code is "1";
  • -: does not have any authority, the number code is "0";
  • s: Special function description: Change the permissions of files or directories.
  • If the permission scope is not specified, the default is All.
  • Two modes:
    1. 权限范围+/-/=权限设置, for example u+x, the user increases the execution authority; different groups are set to use ,separation, such as u+wx,g+w,o-wxthis ; can also ug+wxwrite. o=rThat is, only read authority (r-); +xthat is, all three groups increase execution authority.
    2. A three-digit pattern, such as 755 means user has rwx, group and others have rx.

 

The more important knowledge points are: how to know permissions, how to change permissions, what does 755 stand for, and -R is used for folders.

chmod u+x,g+w f01 #Set the permissions for file f01 that can be executed by itself and can be written by group members
chmod u=rwx,g=rw,o=r f01  #rwxrw-r--
chmod 764 f01 #rwx-wx--x permissions
chmod a+x f01 #Add executable attributes to u, g, o of file f01
chmod -R +x DirName #Add execute permission to the entire folder and its contents

 

chown

The chown command is to change the ownership of the file, which user it belongs to, and what the user group is. The correspondence will affect the effect in chmod rwx. The
user can be user or user D, and the user group can be group name or group id. The filename can be a list of files separated by spaces, and wildcards can be included in the filename. After the -R option, you can operate on the entire folder. Only the file owner (change your own files) and superusers (change other people's files) can use this command. Use with caution by non-root administrators.

 

Command format: chown option user:group file

  • -c或--changes: The effect is similar to the "-v" parameter, but only the changed part is reported;
  • -f或--quite或--silent: Forced execution, no error message is displayed;
  • -h或--no-dereference: Only modify the file linked by the symbol, without changing any other related files;
  • -R或--recursive: Recursive processing, processing all files and subdirectories in the specified directory together;
  • -v或--version: Display the instruction execution process;
  • --dereference: The effect is the same as the "-h" parameter;
  • --help: online help;
  • --reference=<参考文件或目录>: Set the owner of the specified file or directory and the group to which it belongs to the same group as the owner of the referenced file or directory;
  • --version: Display version information.

 

The attribution can only change the user 用户 文件.
In addition to 用户:组名the writing method, the general linux can also 用户.组名(MacOS can not...).

Remember -R is for folders, user:group is enough. It's relatively simple.

 

 

More references:

Linux: Summary of System File Permissions

Linux: Detailed System Structure

l inux: batch modify the name of the folder and the file under the folder

 

This article is transferred from: Linux: Summary of chmod and chown usage

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326507523&siteId=291194637