Modify Linux File Permissions command: chmod command detailed _linux

Source: Internet
Author: User
Tags chmod numeric readable file permissions

Each file and directory in a Linux system has access permission to determine who can access and manipulate files and directories in any way.

File or directory access is divided into read-only, write-only and executable three kinds. In the case of a file, read-only permission means that only its contents are allowed, and no changes to it are prohibited. Executable permission indicates 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 reading and modifying the file. Users can also set access permissions to any combination they need, 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 generally the creator of the file. The owner can allow the same group of users to have access to the file, and to give the file access to other users on the system. In this case, each user in the system has access to the files or directories owned by the user.

Each file or directory has three sets of access rights, each with three-bit representation, read, write, and execute permissions for the owner of the file, as well as read, write, and execute permissions for users of the same group, read, write, and execute permissions for other users in the system. When you use 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 483997 Ju1 L5 17:3l. sobsrc

The horizontal line represents the empty license. R stands for read-only, W for write, X 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 represents a file that is not a directory. If it is D, the representation is a directory.

For example:


Normal file file main group user other user

is a file sobsrc.tgz access, indicating that the sobsrc.tgz is a common file, the owner of the SOBSRC.TGZ has read and write permissions, and the sobsrc.tgz owner of the same group of users only read access, and other users have only Read permissions.

After determining the access rights for a file, users can use the chmod commands 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.

These commands are described separately below.

chmod command

The chmod command is important to change the access rights of a file or directory. Users use it to control access to files or directories.

There are two ways to use this command. One is the text-setting method that contains the letters and operator expressions, and the other is the numeric setting that contains the numbers.

1. Text Setting method

chmod [who] [+ | - | =] [mode] filename ¼

The meanings of the options in the command are:

Action object who is one of the following letters or their combination:

    • U represents "User", which is the owner of a file or directory.
    • G means "same group of users", that is, all users who have the same group ID as the owner of the file.
    • O Represents "other (others) users".
    • A represents "All users". It is the system default value.

The action symbol can be:

    • + Add a permission.
    • -Cancels a permission.
    • = gives the given permission and cancels all other permissions, if any.

The permissions represented by mode set can be any combination of the following letters:

    • R readable.
    • W can be written.
    • X executable.
    • x attributes are appended only if the target file is executable for some users or if the destination file is a directory.
    • s the owner or group ID of the process to the file owner of the file when the file is executed. Mode "U+s" sets the user ID bit of the file, and "G+s" sets the group ID bit.
    • T saves the text of the program to the switching device.
    • You have the same permissions as the owner of the file.
    • G has the same permissions as the user of the same group as the file owner.
    • o have the same permissions as other users.

File name: A space-separated list of files to change permissions to support wildcard characters.

Multiple permissions can be given in one command line, separated by commas. For example: chmod g+r,o+r Example

Make the same group and other users have read access to the file example.

2. Digital Setting method

We must first understand the meaning of a number-represented attribute: 0 means no permission, 1 for executable, 2 for writable, 4 for readable, and then adds. Therefore, the format of the numeric attribute should be 3 octal 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 two permissions, you need to have 4 (readable) +2 (writable) =6 (read/write).

The general form of the digital setting method is:

chmod [mode] filename ¼


(1) Text setting method:

Example 1:

$ chmod a+x Sort

The property of the set file sort is:

File owner (u) Increase execution permissions

Increase execution permissions with the same group of users as the file owner (g)

Other users (O) Increase execution permissions

Example 2:

$ chmod ug+w,o-x Text

The property of the set file text is:

File owner (u) Increase Write permissions

Add write permissions to the same group of users as the file owner (g)

Other users (O) Delete execution permissions

Example 3:

$ chmod u+s a.out

Let's assume that the permissions for a.out after the execution of chmod are (as can be seen with the Ls–l a.out command):

–rws--x--x 1 inin users 7192 Nov 4 14:22 a.out

And this executable file to use a text file shiyan1.c, its file access permission is "–RW-------", that is, the file only its owner has read and write permissions.

When another user executes a.out this program, his identity is temporarily inin by the program (because the S option is used in the chmod command), so he can read the shiyan1.c file (although this file is set to someone else does not have any permissions), this is the function of S.

Therefore, in the entire system, especially root itself, it is best not to set this type of file too much (unless necessary) to ensure that the system security, to avoid some program bugs and the system is compromised.

Example 4:

$ chmod a–x mm.txt

$ chmod–x mm.txt

$ chmod ugo–x mm.txt

All three of these commands delete the execution permissions of the file Mm.txt, and it sets the object to all users.

(2) Digital setting Method:

Example 1:

$ chmod 644 Mm.txt

$ ls–l

The properties of the set file Mm.txt are:

-rw-r--r--1 Inin users 1155 Nov 5 11:22 Mm.txt

File owner (U) inin has read and write permissions

Has read access to the same group of users as the owner of the file (g)

Other person (o) has Read permission

Example 2:

$ chmod 750 wch.txt

$ ls–l

-rwxr-x---1 inin users 44137 Nov 9:22

Set Wchtxt The properties of this file are:

File owner (U) inin readable/writable/executable power

(g) Read/execute rights to the same group as the owner of the file

Other people (O) do not have any permissions

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. Where group can be the user group ID or the group name of the user group in the/etc/group file. The file name is separated by a space to change the list of files in the group, supporting wildcard characters. 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 group of the specified directory and all subdirectories and files under it.

Example 1:

$ chgrp-r Book/opt/local/book

Change the group of all files under/opt/local/book/and its subdirectories to book.

Chown command

Function: Change the owner and group of a file or directory. This command is also very common. For example, root users copy their own files to the user Xu, in order to allow users Xu access to this file, the root should be the owner of the file to Xu, otherwise, the user Xu can not access this 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 user name or a user ID. A group can be either a group name or a group ID. The file is a space-separated list of files that you want to change permissions for, wildcard characters are supported.

The options for this command have the following meanings:

-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.

Example 1: Change the owner of the document SHIYAN.C to Wang.

$ chown Wang Shiyan.c

Example 2: Change the owner of the directory/his and all the files and subdirectories below to Wang, and change the group to users.

$ chown-r Wang.users/his

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

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: 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.