Common Linux commands (22) and common linux commands

Source: Internet
Author: User

Common Linux commands (22) and common linux commands

The chmod command is used to change the access permissions of linux system files or directories. Use it to control access to files or directories. This command can be used in two ways. One is the text setting method that contains letters and operator expressions, and the other is the number setting method that contains numbers.


In Linux, each file and directory has access permission to determine who can access and operate files and directories.


File or directory access permissions are divided into read-only, write-only and executable. Taking a file as an example, the read-only permission indicates that only the content of the file can be read, and any changes to the file are prohibited. The executable permission indicates that the file can be executed as a program. When a file is created, the file owner automatically has the read, write, and executable permissions for the file to facilitate reading and modifying the file. You can also set the access permission to any combination as needed.


There are three different types of users who can access files or directories: file owners, users in the same group and other users. The owner is generally the creator of the file. The owner can allow users in the same group to access files and grant the file access permission to other users in the system. In this case, every user in the system can access the files or directories owned by this user.


Each file or directory has three groups of access permissions. Each group is represented by three digits, which are the read, write, and execution permissions of the file owner; the read, write, and execution permissions of users in the same group as the owner; the read, write, and execution permissions of other users in the system. When you use the ls-l command to display detailed information about a file or directory, the leftmost column lists the file access permissions. For example:

[root@localhost test]# ll -al-rwxrwxrwx 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.log

Take log2012.log as an example:

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

The first column has 10 locations and the first character specifies the file type. In general, a directory is also a file. If the first character is a horizontal line, it indicates a non-directory file. If it is d, it indicates a directory. From the second character to the tenth character, there are 9 Characters in total, and a group of 3 characters represents the permissions of the three groups of users on files or directories. The permission character uses a horizontal line to indicate an empty permit. r indicates read-only, w indicates write, and x indicates executable.

For example:-Rw-r --

Indicates that log2012.log is a common file. The owner of log2012.log has the read and write permissions. users in the same group as log2012.log have only the read permission. Other users have only the read permission. After determining the access permission for a file, you can use the chmod command provided by Linux to reset different access permissions. 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 the access permissions of files or directories. You can 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] mode file


2. command functions:

Used to change the access permission of a file or directory, and use it to control the access permission of a file or directory.


3. command parameters:

A. required parameters:

-C report processing information when a change occurs

-F the error message is not output.

-R processes the specified directory and all files in its subdirectories.

-V displays detailed processing information during running


B. Select parameters:
-- Reference = <directory or File> is set to have the same permissions for the specified directory or file.
-- Version: displays version information.
<Permission range> + <permission settings> grant specified permissions to directories or files within the permission range.
<Permission range>-<permission settings> Delete the specified permission for a directory or file in the permission range
<Permission range> = <permission settings> set the permission for directories or files within the permission range to the specified value.


C. Permission scope:

U: the current user of the directory or file

G: current group of directories or files

O: users or groups except the current users or groups of directories or files

A: All users and groups


D. Permission code:
R: Read Permission, represented by number 4
W: Write Permission, represented by number 2
X: Execution permission, represented by number 1
-: Delete permission, represented by a number 0
S: special permissions

This command can be used in two ways. One is the text setting method that contains letters and operator expressions, and the other is the number setting method that contains numbers.

1). text setting method:

Chmod [who] [+ |-| =] [mode] File Name


2). Number setting method

First, we must understand the meaning of the property represented by numbers: 0 indicates no permission, 1 indicates executable permission, 2 indicates writable permission, 4 indicates readable permission, and then add it. Therefore, the format of the numeric attribute should be three Octal numbers from 0 to 7. The order is (u) (g) (o ).

For example, if you want the owner of a file to have the "read/write" permission, you need to set 4 (readable) + 2 (writable) to 6 (read/write ).

The general format of the number setting method is: chmod [mode] File Name

The correspondence between numbers and characters is as follows:

R = 4, w = 2, x = 1

If you want the rwx attribute, 4 + 2 + 1 = 7

If the rw-attribute is required, 4 + 2 = 6;

If you want the r-x attribute, 4 + 1 = 7.


4. Command instance:

Instance 1.Add executable permissions for all file user groups

Command: chmod a + x log2012.log

Note: The execution permission is added to the file owner (u), and the execution permission is added to the user (g) in the same group as the file owner, and to other users (o ).

[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]#

Instance 2. Modify different user permissions at the same time

Command: chmod ug + w, o-x log2012.log

Note: The write permission is added to the file owner (u), and the write permission is added to the users (g) in the same group as the file owner. Other users (o) delete the execution permission.

[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

Instance 3
. Delete File Permission

Command: chmod a-x log2012.log

Delete the executable permissions of all users

[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

Instance 4. Use "=" To Set permissions

Command: chmod u = x log2012.log

Revoke all permissions of the user and grant the owner executable permissions.

[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

Instance 5. Add permissions to all files in a directory and Its subdirectories.

Command: chmod-R u + x test4

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

[root@localhost test]# cd test4[root@localhost test4]# ls -aldrwxr-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 -aldrwxr-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.log

Instance 6. Other examples

1. chmod 751 file assigns read, write, and execute permissions to the file owner, read and execute permissions to the group where the file is located, and execution permissions to other users.

2. chmod u = rwx, g = rx, o = x file functions the same as (1)

3. chmod = r file assigns read permissions to all users

4. the chmod 444 file function is the same as that of (3)

5. chmod a-wx, a + r file functions are the same (3)

Original article: http://www.cnblogs.com/peida/archive/2012/11/29/2794010.html

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.