Linux commands-chmod, chown, and CHGRP

Source: Internet
Author: User
Tags readable



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 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 root 483997 Ju1 L5 17:3l sobsrc. tgz

The horizontal line represents an empty license. R stands for read-only, W stands for write, and x stands for executable. Note there are 10 locations. 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.

For example:

-rw-r--r--

Normal file File main group user other users

is the access rights of the file Sobsrc.tgz, indicating that sobsrc.tgz is an ordinary file, the owner of the SOBSRC.TGZ has read and write permission, and the user of the same group as the SOBSRC.TGZ owner only reads, 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.

These commands are described separately below.

chmod Command

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.

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.

1. Text Setting Method

chmod [who] [+ |-| =] [mode] file name ¼

the meanings of the options in the command are:

Action object who is either or a combination of the following letters:

u means "user", which is the owner of the file or directory.

g means "same group user", that is, all users who have the same group ID as the file owner.

o means "other (others) users".

a means "all users". It is the system default value.

The operation symbols can be:

+ Add a permission.

-Cancels a permission.

= gives the given permission and cancels all other permissions, if any.

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

R is readable.

W writable.

x executable.

x Append the x attribute only if the destination file is executable to some users, or if the target file is a directory.

S is the owner of the file in which the owner or group ID of the process is placed when the file is executed. The way "U+s" sets the user ID bit of the file, "G+s" sets the group ID bit.

T Save the program's text to the swap device.

You have the same permissions as the owner of the file.

g has the same permissions as a user with the same group as the file owner.

o have the same permissions as other users.

file name: A list of files separated by spaces to change permissions, and wildcard characters are supported.

multiple permission methods can be given in one command line, separated by commas. Example: chmod g+r,o+r Example

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

2. Digital Setting Method

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 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] file name ¼

Example:

(1) Text setting method:

example 1:$ chmod a+x sort

the properties of the set file sort are:

file Owner (u) Increased execution Permissions

increase execution rights with the owner of the file in the same group as the user (g)

additional users (o) Increased execution permissions

example 2:$ chmod ug+w,o-x text

the property of the set file text is:

file Owner (u) Add Write permission

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

other users (o) Remove Execute permissions

example 3:$ chmod u+s a.out

assume that the permissions for a.out after executing chmod are (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 execution 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 other users execute a.out this program, his identity is temporarily inin because of this program (because the chmod command uses the S option), so he can read shiyan1.c this file (although this file is set to other people do not have any permissions), this is the function of S.

therefore, in the whole system, especially the root itself, it is best not to set too much of this type of file (unless necessary) to ensure the security of the system, to avoid the bug of some programs to make the system compromised.

example 4:$ chmod a–x mm.txt

$ chmod–x Mm.txt

$ chmod ugo–x mm.txt

All of the above three commands delete the execution permission of the file Mm.txt, and it sets the object to be all the 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

User with file owner (g) has Read access

other People (O) have read access

Example 2: $ chmod wch.txt

$ ls–l

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

that is, set wchtxt the properties of this file are:

file Owner (u) inin readable/writable/executable rights

(g) readable/enforceable rights with the main group of files

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 belonging to the group, support wildcard characters. If the user is not the owner or superuser of the file, you cannot change the group of the file.

The options for the command mean:

-R recursively changes the genus of the specified directory and all subdirectories and files under it.

example 1:$ chgrp-r book/opt/local/book

change the genus of all files under/opt/local/book/and its subdirectories as book.

chown Command

function: Change a file or directory's owner and owner group. 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 be 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 either 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 to change permissions, and 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 shows the work done by the Chown command.

Example 1: Change the owner of the file shiyan.c to Wang.

$ chown Wang Shiyan.c

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

$ chown-r Wang.users/his

Linux commands-chmod, chown, and CHGRP

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