Welcome to github.com/hsfxuebao/j… , hope to help you, if you feel ok, please click on the Star

The chmod command is used to change the access permission of a file or directory in the Linux system. Use it to control access to files or directories. This command can be used in two ways. One is a literal setting method that contains letters and operator expressions. The other is the number setting method with numbers.

Every file and directory in Linux has access permissions that determine who can access and manipulate files and directories and how. The access permission for a file or directory is read-only, write only, and executable. Take a file as an example, the read-only permission means that only the contents of the file can be read, but no modification is allowed. Executable permission allows the file to be executed as a program. When a file is created, the file owner automatically has the read, write, and execute permissions on the file for easy reading and modification. Users can also set access to any combination they want as needed. There are three different types of users who can access files or directories: file owners, group users, and other users. The owner is typically the creator of the file. The owner can grant the same group of users access to the file, and can grant access to the file to other users in the system. In this case, every user on the system can access files or directories owned by that user. Each file or directory has three groups of access permissions. Each group is the read, write, and execute permissions of the file owner. Read, write, and execute permissions of users in the same group as the owner; Read, write, and execute permissions of other users in the system. When the ls -l command is used to display details about a file or directory, the leftmost column is the access permission for the file. Such as:

Command:

ls -al

Output:

[root@localhost test]# ll-al 316lrwxrwxrwx 1 root root 11 11-22 06:58 linklog.log -> log2012.log -rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log -rw-r--r-- 1 root root 61 11-13 06:03 log2013.log -rw-r--r-- 1 root root 0 11-13 06:03 log2014.log -rw-r--r-- 1 root root 0 11-13 06:06 log2015.log -rw-r--r-- 1 root root 0 11-16 14:41 log2016.log -rw-r--r-- 1 root root 0 11-16 14:43 log2017.logCopy the code

Let’s take log2012.log as an example:

-rw-r–r– 1 root root 296K 11-13 06:03 log2012.log

There are 10 positions in the first column, with the first character specifying the file type. In a general sense, a directory is also a file. If the first character is a dash, it is a file that is not a directory. If the value is D, it is a directory. It contains nine characters starting from the second character to the tenth character. A group of three characters indicates the permissions of three groups of users on files or directories. Permission characters are underlined for empty permission, r for read-only, W for write, and x for executable.

For example, -rw-r — r– indicates that log2012.log is a common file. The log2012. Log owner has read and write permissions. Users in the same group as the log2012. Log owner have only the read permission. Other users have only read permission.

After determining the access permission for a file, you can use the chmod command provided by the Linux system to reset the access permission for different files. 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.

The chmod command is very important for changing access to files or directories. Users use it to control access to files or directories. Details about the chmod command are as follows.

1. Command format:

chmod [-cfvR] [–help] [–version] mode file

2. Function

It is used to change the access permission of a file or directory.

3. Command parameters:

Required parameters: -c Reports processing information when changes occur. -f Error information is not displayed. -r Processes all files in the specified directory and its subdirectory

Select parameters: –reference=< directory or file > set to have the same permission for the specified directory or file. –version Displays the version information. < Permission range >+< Permission Settings > Enable the specified permission for the directory or file within the permission range. Delete specified permissions for directories or files in the permission range < Permission Scope >=< Permission Settings > Sets the permission for directories or files in the permission range to the specified value

Permission scope: U: current users of a directory or file G: current groups of a directory or file O: users or groups except the current users or groups of a directory or file A: all users or groups

Permission code: r: read permission (4) W: write permission (2) X: execute permission (1) – : delete permission (0) S: special permission

This command can be used in two ways. One is a literal setting method that contains letters and operator expressions. The other is the number setting method with numbers. 1.) character set method: chmod [who] [+ | – | =] [mode] file 2). We must first understand the meaning of numeric attributes: 0 for no permission, 1 for executable permission, 2 for write permission, and 4 for read permission, and then add them up. So the numeric attribute should be in the format of three octal numbers from 0 to 7, in the order (u) (g) (o). For example, if you want the owner of a file to have read/write permissions, set 4 (read) +2 (write) = 6 (read/write). The general format of the numeric setting method is chmod [mode] file name

The mapping between numbers and characters is as follows:

R =4, w=2, x=1 4+2+1=7 4+2=6 If you want the R-x attribute, 4+1=7.

4. Example:

Example 1: Add the execute permission to all user groups of a file

Command:

chmod a+x log2012.log

Output:

[root@localhost test]# ls -al log2012.log

-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log

[root@localhost test]# chmod a+x log2012.log

[root@localhost test]# ls -al log2012.log

-rwxr-xr-x 1 root root 302108 11-13 06:03 log2012.log

[root@localhost test]#
Copy the code

Note: That is, set the properties of file log2012. Log to: Add execution permission to file owner (u); The user in the same group as the file owner (g) has the execution permission. Other users (o) Add execution permission.

Example 2: Modify different user permissions at the same time

Command:

chmod ug+w,o-x log2012.log

Output:

[root@localhost test]# ls -al log2012.log

-rwxr-xr-x 1 root root 302108 11-13 06:03 log2012.log

[root@localhost test]# chmod ug+w,o-x log2012.log

[root@localhost test]# ls -al log2012.log

-rwxrwxr-- 1 root root 302108 11-13 06:03 log2012.log
Copy the code

Add write permission to file owner (u); Add the write permission to the user in the same group as the file owner. Other users (o) delete the execution permission

Example 3: Delete file permissions

Command:

chmod a-x log2012.log

Output:

[root@localhost test]# ls -al log2012.log

-rwxrwxr-- 1 root root 302108 11-13 06:03 log2012.log

[root@localhost test]# chmod a-x log2012.log

[root@localhost test]# ls -al log2012.log

-rw-rw-r-- 1 root root 302108 11-13 06:03 log2012.log
Copy the code

Note: Delete the executable permission of all users

Example 4: Use “=” to set permissions

Command:

chmod u=x log2012.log

Output:

[root@localhost test]# ls -al log2012.log

-rw-rw-r-- 1 root root 302108 11-13 06:03 log2012.log

[root@localhost test]# chmod u=x log2012.log

[root@localhost test]# ls -al log2012.log

---xrw-r-- 1 root root 302108 11-13 06:03 log2012.log
Copy the code

Description:

Revoke all previous permissions, and then make the owner readable

Example 5: Add permissions to all files in a directory and its subdirectories

Command:

chmod -R u+x test4

Output:

[root@localhost test]# CD test4 [root@localhost test4]# ls-al 312drwxrwxr-x 2 root root 4096 11-13 05:50 drwxr-xr-x 5 root root 4096 11-22 06:58 .. -rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log -rw-r--r-- 1 root root 61 11-12 22:54 log2013.log -rw-r--r-- 1 root root 0 11-12 22:54 log2014.log [root@localhost test4]# cd .. [root@localhost test]# chmod -r u+x test4 [root@localhost test]# CD test4 [root@localhost test4]# ls-al 312drwxrwxr-x 2 root root 4096 11-13 05:50 . drwxr-xr-x 5 root root 4096 11-22 06:58 .. -rwxr--r-- 1 root root 302108 11-12 22:54 log2012.log -rwxr--r-- 1 root root 61 11-12 22:54 log2013.log -rwxr--r-- 1 root root 0 11-12 22:54 log2014.logCopy the code

Description:

Recursively assign permissions to owners of all files and subdirectories in the test4 directory

Some other examples:

1).

Command:

chmod 751 file

Description:

Assign read, write, and execute (7) permissions to the owner of file, read and execute (5) permissions to the group where file resides, and execute (1) permissions to other users

2).

Command:

chmod u=rwx,g=rx,o=x file

Description:

Another form of the above example

3).

The command

chmod =r file

Description:

Assign read permission to all users

3).

Command:

chmod 444 file

Description:

Same as above case

4).

Command:

chmod a-wx,a+r file

Description:

Same as above case

From:

One Linux command per day (27) : Linux chmod command