Linux command: Change access to files or directories

  
Linux commands: Change access to files or directories Each file and directory in the Linux system has access permissions, which can be used to determine who can use files and directories. Access and operation. Access rights to files or directories are divided into read-only, write-only, and executable. In the case of a file, read-only permission means that only its contents are allowed to be read, and any changes are prohibited. Executable permissions indicate that the file is allowed to execute as a program. When a file is created, the file owner automatically has read, write, and executable permissions on the file to facilitate reading and modifying the file. Users can also set access rights to any combination they need, as needed. There are three different types of users that can access files or directories: file owners, groups of users, and other users. The owner is usually the creator of the file. The owner can allow the same group of users to have access to the file, and can also give access to the file to other users in the system. In this case, every user in the system can access the files or directories owned by the user. Each file or directory has three groups of access rights, each group is represented by three digits, which are the read, write and execute permissions of the file owner; the read, write and execute permissions of the user in the same group; other in the system User read, write, and execute permissions. When using the ls -l command to display the details of a file or directory, the leftmost column is the file's access rights. For example: $ ls -l sobsrc. tgz -rw-r--r-- 1 root root 483997 Ju1 l5 17:3l sobsrc. tgz The horizontal line represents the empty license. r stands for read-only, w stands for write, and x stands for executable. Note that there are 10 locations here. The first character specifies the file type. In the usual sense, a directory is also a file. If the first character is a horizontal line, it means a non-directory file. If it is d, it means a directory. For example: - rw- r-- r-- ordinary file file primary group user other users are the access rights of the file sobsrc.tgz, indicating that sobsrc.tgz is a normal file; the owner of sobsrc.tgz has read and write permissions; and sobsrc .tgz belongs to the same group of users only read permissions; other users only have read permissions. After determining the access rights of a file, the user can use the chmod command provided by the Linux system to reset different access rights. You can also use the chown command to change the owner of a file or directory. Use the chgrp command to change the user group of a file or directory. These commands are described separately below.
chmod Command The chmod command is very important for changing the access rights of a file or directory. The user uses it to control access to files or directories. There are two uses for this command. One is a text setting method that contains letters and operator expressions; the other is a number setting method that includes numbers. 1. Text setting method chmod [who] [+ |  - |  =] [mode] File name? The meaning of each option in the command is: 2. Number setting method We must first understand the meaning of the attribute represented by number: 0 means no permission, 1 means executable permission, 2 means Shang Cixi蓿? indicates readable permissions and then adds them together. So the format of the numeric attribute should be three 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 have 4 (readable) + 2 (writable) = 6 (read/write). The general form of the number setting method is: chmod [mode] File name? chgrp command Function: Change the group to which the file or directory belongs. Syntax: chgrp [options] group filename? This command changes the user group to which the specified file belongs. The group can be a user group ID or a group name of a user group in the /etc/group file. File names are separated by spaces. The list of files to be changed is a wildcard. If the user is not the owner or superuser of the file, the group of the file cannot be changed. The options for this command mean: - R Recursively changes the specified directory and all subdirectories and files under it. Chown command Function: Change the owner and group of a file or directory. This command is also very common. For example, the root user copies a file of his own to the user xu. In order for the user xu to access the file, the root user should set the owner of the file to xu. Otherwise, the user xu cannot access the file. Syntax: chown [options] User or group File Description: chown changes the owner of the specified file to the specified user or group. The user can be a username or a user ID. A group can be a group name or a group ID. A file is a list of files whose permissions are to be changed, separated by spaces, and supports wildcards. The options for this command have the following meaning: - R Recursively changes the owner of the specified directory and all subdirectories and files under it. -v Displays the work done by the chown command. The operation object who can be any one of the following letters or a combination thereof: u means "user", that is, the owner of the file or directory. g means "group user", that is, all users who have the same group ID as the file owner. o means "others". a means "all (all) users". It is the system default. The action symbol can be: + Add a permission. - Cancel a permission. = Give given permissions and cancel all other permissions (if any). The permissions indicated by setting mode can be any combination of the following letters: r Readable. w can be written. x executable. X Appends the x attribute only if the target file is executable to some users or if the target file is a directory. s Set the owner or group ID of the process to 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 to the switching 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 file owner. o Have the same permissions as other users. File name: A list of files whose permissions are to be changed, separated by spaces, to support wildcards. Multiple permission modes can be given on one command line, separated by commas. For example: chmod g+r, o+r example allows the same group and other users to have read access to the file example.

Copyright © Windows knowledge All Rights Reserved