Linux file Permissions Command chmod learning

Source: Internet
Author: User

LinuxEach file and directory in the system has access permission 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-lWhen the command displays 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. RRepresents read-only, WRepresentative Write, XRepresents an executable. Note There are a total of 10A location. 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's D,, which indicates that it is a directory.

For example:

-rw-r--r--

Normal file File Master Group Users Other users

is File sobsrc.tgzAccess rights, which represent sobsrc.tgzis an ordinary document; sobsrc.tgzThe owner has read and write permission; with sobsrc.tgzUsers in the same group are read only, and other users have Read permissions.

Once the access to a file is determined, the user can take advantage of the LinuxSystem-provided chmodcommand to reset the different access permissions. You can also use Chowncommand to change the owner of a file or directory. Using CHGRPcommand to change the user group for a file or directory.

These commands are described separately below.

chmodCommand

chmodCommands are very 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 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 whoHowever, any of the following letters or their combinations:

USaidUsers (user)”, which is the owner of the file or directory.

GSaidSame set (groupUser, which has the same group ID as the owner of the fileFor all users.

OSaidOther (OthersUser。

ASaidAll (AllUser。 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.

Set modeThe permission represented can be any combination of the following letters:

RReadable.

Wcan be written.

XExecutable.

XAppend x only if the destination file is executable to some users, or if the target file is a directoryProperty.

SThe owner or group ID of the process when the file is executedThe file in which the file is placed belongs to the owner. Way "U+s "Set the user ID of the fileBit, "G+s "Set the group ID-bit

TSave the program's text to the swap device.

UHas the same permissions as the owner of the file.

GHas the same permissions as the user who owns the same group as the file owner.

OHave 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

Example files to the same group and other usershave Read permission.

2.Digital Setting method

We must first understand the meaning of the attributes represented by numbers: 0Indicates no permissions, 1Indicates executable permissions, 2Represents writable permissions, 4Represents a readable permission and then adds it. So the format of the numeric attribute should be 3A from 0to 7Octal number, whose order is (U) (g) (O)。

For example, if you want a file owner to have "ReadWriteTwo kinds of permissions, you need to put 4(readable) +2(Can write) =6ReadWrite).

The general form of the digital setting method is:

chmod[Mode] File name ¼

Example:

(1) Text Setting Method:

Example 1: $ chmod a+x Sort

That is, set the file sortThe properties are:

File owner (U) Increase execution permissions

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

Other users (O) Increase execution permissions

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

That is, set the file textThe properties are:

File owner (U) Add Write permission

Same group of users as the file owner (g) Add Write permission

Other users (O) Remove Execute Permissions

Example 3: $ chmod u+s a.out

Assuming execution chmodAfter a.outThe permission is (can be used ls–l a.outcommand to see):

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

And this execution file will use a text file shiyan1.c, and its file access rights are "–RW-------", that is, the file has read and write access only to its owner.

When other users execute a.outThis program, his identity as a result of this program temporarily become Inin(Due to chmodThe command used the Soption), so he was able to read shiyan1.cThis file (although this file is set to other people without any permissions), this is SThe function.

Therefore, in the entire system, in particular rootitself, it is best not to set this type of file too much (unless necessary) so as to ensure the security of the system, avoid some program bugsAnd the system is compromised.

Example 4: $ chmod a–x mm.txt

$ chmod–x Mm.txt

$ chmod ugo–x Mm.txt

All of these three commands are mm.txt filesThe Execute permission is removed, and it sets the object to all users.

(2) Digital Setting Method:

Example 1: $ chmod 644 Mm.txt

$ ls–l

That is, set file Mm.txtThe properties are:

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

File owner (U) IninHave read and Write permissions

With the owner of the document is the same group of users (g) Have Read permission

Other people (O) Have Read permission

Example 2: $ chmod wch.txt

$ ls–l

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

That is set WchtxtThe properties of this file are:

File Master Himself (U) IninReadable/Can write/The right to execute

Same group as the file owner (g) Readable/The right to execute

Other people (O) Does not have any permissions

ChgrpCommand

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 groupCan be a user group IDOr it could be/etc/group.The group name of the user group in the 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:

, 3Recursively changes the genus of the specified directory and all subdirectories and files under it.

Example 1: $ chgrp-r Book/opt/local/book

Change/opt/local/book/The group of all files under the directory and its subdirectories is book。

ChownCommand

Function: Change a file or directory's owner and owner group. This command is also very common. such as RootThe user copies one of their own files to the user Xu, in order to let the user XuAbility to access this file, rootThe user should set the owner of this file as Xu, otherwise, the user XuThis file cannot be accessed.

Syntax: ChownOptions user or group file

Description: Chown

-R Recursively alters the owner of the specified directory and all subdirectories and files under it.

-v shows the work done by the Chown command.


$ chown Wang Shiyan.c

: Put the directory/his

Linux file Permissions command chmod learning

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.