Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of the Linux basic command chmod

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the usage of the Linux basic command chmod". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the usage of the Linux basic command chmod".

Chmod

To change the permissions of a file or directory, you can use numbers or letters to identify permissions. In digital mode: 0, no permissions; 1, executable; 2, readable; 4, writable; multiple permissions can be added. In character mode: X, for execution; r, for read; w, for write; g, for group permissions; o, for other user permissions in the group; u, for user rights, the permissions seen in the ls command are: user rights, group permissions, and other user rights in the group.

The scope of this command: RedHat, RHEL, Ubuntu, CentOS, SUSE, openSUSE, Fedora.

1. Grammar

Chmod [options] mode file

Chmod [option] Octal mode file

Chmod [options]-reference=RFILE file

2. List of options

Option

Description

-- version

Display command version information

-- help

Show help documentation

-c |-- changes

Same as verbose, but only show details when a change occurs

-- preserve-root

Recursive hype of the root directory is prohibited

-- no-preserve-root

No special treatment for root directory, default value

-f |-- silent |-- quiet

Ignore some error messages

-- reference=file

Use the group to which the associated file belongs

-v |-- verbose

Show details

-R |-- recursive

Change all directories and subdirectories recursively

3. Description

Chmod changes the file mode bits of each given file according to the pattern, which can be a symbolic representation of the change to be made or an octal number that represents the bit pattern of the new mode bits. The format of the symbol pattern is [ugoa...] [[+-=] [perms...]...], where perms is zero or more letters of the collection rwxXst, or a single letter from the associated ugo. A variety of symbol patterns can be given, separated by commas.

The letter ugoa controls which users' permissions to access the file will be changed: (u) the user who owns the file, (g) other users in the filegroup, (o) other users who do not belong to the filegroup, or all users (A). If none of this is given, the effect is as if "a" is given, but the bits set in umask are not affected.

The operator'+ 'causes the selected file mode bits to be added to the existing file mode bits of each file;'-'causes them to be deleted;' = 'causes them to be added and unmentioned bits are deleted unless the unmentioned setting user and group ID bits of the directory are not affected.

The letter "rwxXst" selects file mode bits for the affected user: (r) read, (w) write, (x) execute (or search directory), (X) only if the file is a directory or already has execute permissions for certain users, (s) set user or group ID at execution time, (t) restrict deletion flag or sticky bit / search. You can specify one or more letters of "ugo": (u) permissions granted to the user who owns the file, (g) permissions granted to other users belonging to the filegroup, and (o) permissions granted to either of the above two categories.

The number pattern is 1 to 4 octal digits (0-7), obtained by adding the bits of the values 4, 2, and 1. The omitted number is assumed to be a leading zero. The first numeric selection sets the user ID (4) and the setting group ID (2), and restricts the deletion or paste of the (1) attribute. The second digit is the user selection permission for the file: read (4), write (2), and execute (1); the third digit selects the permissions of other users in the filegroup with the same value; the fourth digit selects permissions with the same value for other users who do not belong to the filegroup.

Chmod never changes the permissions of symbolic links; chmod system calls cannot change their permissions. This is not a problem because symbolic link permissions have never been used. However, for each symbolic link listed on the command line, chmod changes the permissions to the file. In contrast, chmod ignores symbolic links encountered during recursive directory traversal.

4. Set uid and gid

If the group ID of the file does not match the user's valid group ID or the user's auxiliary group ID, chmod clears the "set-group-ID" bit from the regular file unless the user has the appropriate permissions. Additional restrictions may cause the "set-user-ID" and "set-group-ID" bits of MODE or RFILE to be ignored. This behavior depends on the policies and functions of the underlying chmod system calls. When in doubt, check the underlying system behavior.

Chmod retains the "set-user-ID" and "set-group-ID" bits of the directory, unless you explicitly specify otherwise. You can set or clear bits using symbolic modes such as "Usings" and "gmurs", or you can set (but not clear) these bits in numeric mode.

5. Restrict deletion of flags or sticky bits

The restricted delete flag or sticky bit is a bit, and its interpretation depends on the file type. For directories, it prevents unprivileged users from deleting or renaming files in the directory unless they own the file or directory; this is called the restricted delete flag for directories and is usually found in writable directories such as / tmp. For some regular files on older systems, this one saves the text image of the program on the swap device so that it can be loaded faster at run time; this is called sticky bits.

6. Examples

1) modify the permission of the file to the maximum

[root@localhost weijie] # chmod 777 1.c

You have new mail in / var/spool/mail/root

[root@localhost weijie] # ls-l 1.c

-rwxrwxrwx 1 root weijie 0 September 7 09:11 1.c

2) set groups and other users to read-only, using character mode settings

[root@localhost weijie] # chmod 777 1.c / / changed to maximum permission

[root@localhost weijie] # ls-l 1.c

-rwxrwxrwx 1 root weijie 0 September 7 09:11 1.c

[root@localhost weijie] # chmod go+r 1.c / / modify group, other user rights

[root@localhost weijie] # ls-l 1.c

-rwxrwxrwx 1 root weijie 0 September 7 09:11 1.c / / there is no effect here

[root@localhost weijie] # chmod 7001.c / / Group, other user permissions are

[root@localhost weijie] # chmod go+r 1.c / / groups and other users increase read permissions

[root@localhost weijie] # ls-l 1.c

-rwxr--r-- 1 root weijie 0 September 7 09:11 1.c / / successful. You can see that when using character mode, permissions are an algorithm of or, not a direct assignment

Thank you for your reading, the above is the content of "the usage of the Linux basic command chmod". After the study of this article, I believe you have a deeper understanding of the usage of the Linux basic command chmod, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report