Linux Command learning Note: Linux chmod command

Source: Internet
Author: User

the chmod command is used to change access rights for Linux system files or directories. Use it to control access to files or directories. There are two ways to use this command. One is a text-setting method that contains letters and operator expressions, and the other is a digital setting method that contains numbers.  Each file and directory in a Linux system has access permissions, which are used to determine who can access and manipulate files and directories. Access to a file or directory is divided into read-only, write-only, and executable three types. As an example of a file, a read-only permission means that only the content is allowed to be read, and any changes to it are forbidden. Executable permission means that the file is allowed to be executed as a program. When a file is created, the file owner automatically has read, write, and execute permissions on the file to facilitate the reading and modification of the file.  Users can also set access rights to any combination they want, as needed. There are three different types of users who can access files or directories: The file owner, the same group of users, and other users. The owner is typically the creator of the file. The owner can allow the same group of users access to the file, as well as the access rights of the file to other users on the system.  In this case, every user in the system can access the files or directories that the user owns. Each file or directory has three groups of access rights, each group is represented by three bits, respectively, the read, write, and execute permissions of the file owner, the read, write, and execute permissions of the user belonging to the primary group, and the read, write, and execute permissions of other users in the system. When using LS-The l command displays the details of a file or directory, and the leftmost column is the file's access rights. For example: command:ls-al output: [[email protected] test]# ll-Al Total 316lrwxrwxrwx1Root root One  One- A  .: -Linklog.logLog2012.log-rw-r--r--1Root root302108  One- -  .:GenevaLog2012.log-rw-r--r--1Root root A  One- -  .:GenevaLog2013.log-rw-r--r--1Root root0  One- -  .:GenevaLog2014.log-rw-r--r--1Root root0  One- -  .: .Log2015.log-rw-r--r--1Root root0  One- -  -: ALog2016.log-rw-r--r--1Root root0  One- -  -: +Log2017.log We take Log2012.log as an example:-rw-r--r--1Root root 296K One- -  .:GenevaLog2012.log There are 10 locations in the first column, and 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 represents a non-directory file. If it is D, the representation is a directory. From the second character to the tenth, a total of 9 characters, a group of 3 characters, respectively, representing 3 groups of users of the file or directory permissions. The permission character is represented by a horizontal line for an empty license, R for Read only, W for write, and X for executable. For example:-rw-r--r--indicates that the Log2012.log is an ordinary file, the owner of the Log2012.log has read and write permissions, and the Log2012.log owner is the same group as the user only, and the other user only has Read permission. After determining the access rights of a file, users 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 for a file or directory. The chmod command is very important for changing the access rights of a file or directory. Users use it to control access to files or directories. The details of the chmod command are as follows. 1. Command format:chmod[-CFVR] [--help] [--version] Modefile   2. Command function: Used to change the access rights of a file or directory, using it to control access to a file or directory. 3. Command parameters: Required Parameters:-C When a change occurs, the report processes the information-f error message not output-R handles all files in the specified directory and its subdirectories-v Run-time display detailed processing information selection parameters:--reference=< directory or File >set to have the same permissions as the specified directory or file--version displays release information< permission range >+< permission settings >make a directory or file within a permission scope have the specified permissions< permission range >-< permission settings >Delete a permission range of a directory or a file's specified permissions< permission range >=< permission settings >set permissions for a directory or file within a permission range for the specified value permission range: U: Current user of directory or file G: Directory or current group of files O: User or Group A: All users and groups except the directory or the current user or group of files: R: Read access, denoted by the number 4W: Write permission, denoted by the number 2 x: Execute permission, denoted by the number 1-: Delete permission, denoted by number 0 S: Special permissions The command has two uses.  One is a text-setting method that contains letters and operator expressions, and the other is a digital setting method that contains numbers. 1). Text Setting Method:chmod[W.H.O.] [+ | - | =] [mode] File name2). Digital Setup We must first understand the meaning of the attributes represented by numbers: 0 means no permissions, 1 means executable permissions, 2 is writable, 4 is read, and then it is added.  So 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 a file owner to have a "read/write "Two kinds of permissions, need to put 4 (readable) +2(writable) =6Readwrite). The general form of the digital setting method is:chmodthe [mode] filename number corresponds to the character as follows: R=4,W=2, x=1to rwx the property, 4+2+1=7to RW-Attribute is 4 +2=6; to RThe-X property is 4 +1=7. 4. Use instance: Instance 1: Add File All user groups Execute Permissions command:chmodA +x log2012.log output: [[email protected] test]#ls-Al Log2012.log-rw-r--r--1Root root302108  One- -  .:GenevaLog2012.log[[email protected] test]#chmodA +x log2012.log [[email protected] test]#ls-Al Log2012.log-rwxr-xr-x1Root root302108  One- -  .:GenevaLog2012.log[[email protected] test] #说明: That is, the property of the set file Log2012.log is: the file owner (U) to increase the execution permissions, with the file owner of the same group of users (g) To increase the execution of permissions, other users (O) To increase the execution permissions. Example 2: Modify different user rights commands at the same time:chmodug+W, Ox log2012.log output: [[email Protected]ocalhost test]#ls-Al Log2012.log-rwxr-xr-x1Root root302108  One- -  .:GenevaLog2012.log[[email protected] test]#chmodug+W, Ox log2012.log [[email protected] test]#ls-Al Log2012.log-rwxrwxr--1Root root302108  One- -  .:GenevaLog2012.log Description: That is, the property of the text of the set file is: the file owner (U) to increase the Write permission, and the file belongs to the same group of users (g) To increase the Write permission; other users (o) Remove Execute permissions Instance 3: Delete file permissions command:chmodA-x log2012.log output: [[email protected] test]#ls-Al Log2012.log-rwxrwxr--1Root root302108  One- -  .:GenevaLog2012.log[[email protected] test]#chmodA-x log2012.log [[email protected] test]#ls-Al Log2012.log-rw-rw-r--1Root root302108  One- -  .:Genevalog2012.log Description: Remove executable permissions for all users Instance 4: Use "=the Set permissions command:chmodu=x log2012.log output: [[email protected] test]#ls-Al Log2012.log-rw-rw-r--1Root root302108  One- -  .:GenevaLog2012.log[[email protected] test]#chmodu=x log2012.log [[email protected] test]#ls-Al Log2012.log---xrw-r--1Root root302108  One- -  .:Genevalog2012.log Description: Revoke all of the original permissions, and then have the owner have the Read permission instance 5: Add permission command to all files of a directory and its subdirectories:chmod-R u+x test4 output: [[email protected] test]# CD Test4[[email protected] test4]#ls-Al Total 312drwxrwxr-X2Root root4096  One- -  to: -. DRWXR-xr-x5Root root4096  One- A  .: - ..-rw-r--r--1Root root302108  One- A  A: WuLog2012.log-rw-r--r--1Root root A  One- A  A: WuLog2013.log-rw-r--r--1Root root0  One- A  A: WuLog2014.log[[email protected] test4]# CD. [Email protected] test]#chmod-R u+x test4[[email protected] test]# CD Test4[[email protected] test4]#ls-Al Total 312drwxrwxr-X2Root root4096  One- -  to: -. DRWXR-xr-x5Root root4096  One- A  .: - ..-rwxr--r--1Root root302108  One- A  A: WuLog2012.log-rwxr--r--1Root root A  One- A  A: WuLog2013.log-rwxr--r--1Root root0  One- A  A: WuLog2014.log Description: Recursively assigns permissions to all files and subdirectories in the Test4 directory to other instances:1). Command:chmod 751 fileDescription: Read, write, execute for the owner of file (7), assign read and execute to the group where the file is located (5), assigning execution to other users (1) of Permissions2). Command:chmodU=rwx,g=rx,o=xfileDescription: Another form of the above example3). Commandchmod=rfileDescription: Assign Read permissions to all users3). Command:chmod 444 fileDescription: above example4). Command:chmodA-wx,a+rfileDescription: above example

Linux Command learning Note: Linux chmod command

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.