Linux commonly used commands chown, chgrp and chmod distinction and detailed usage

Linux commonly used commands chown, chgrp and chmod distinction and detailed usage

Before we talk about chown, chgrp, and chmod, we must first know the concepts of users, user groups, and other users, as well as the differences between file and directory permissions. You can read this article to understand. https://blog.csdn.net/weixin_45631506/article/details/114946442?spm=1001.2014.3001.5502

1. Privilege setting commands The
following operations switch to root identity, otherwise it may be restricted by permission : you can use su-to switch root identity and exit to exit root identity. (But you can switch the general user status immediately after using it!! Because the power of root is too great!!!)
Insert picture description here
Note:! ! ! ! Be sure to check the original permissions with ls -l first, don't change them directly, and change them back after the practice! ! ! !

1. chown:
(1) Modify the user who belongs to the file; (if you want to change the user who belongs to all the file directories in the directory, use -R, such as "chown -R root file or directory")

Insert picture description here

(2), chown can also change the user and user group at the same time by changing the format of "chown user: user group file or directory". (You can also use "chown user. User group file or directory", but some people may have a'.' in their username, so generally don't use this method)

Insert picture description here
Note: You can also use the ":root" or "root:" method to change the user group or user

2. chgrp: modify the user group to which the file belongs; (if you want to change the user group of all file directories in the directory, use -R, such as "chgrp -R root file or directory")

Insert picture description here

3. chmod: modify file permissions.

(1), digital type modification file permissions:
r: 4
w: 2
x: 1
-:0

For example:
user readable, writable and executable = r+w+x=7
user group readable but not writable and executable = r+x=5
others are not readable, not writable and not executable =---=0
so the command is chmod 750 Public
Insert picture description here
(2), symbol type modification file permissions:
u: user
g: user group
o: other users
a: all identities (users, user groups and other users)

+: add
-: remove
=: set

r: read
w: write
x: execute

Example 1:
User readable, writable and executable: u=rwx (if already readable and writable: u+x)
User group readable, writable and executable: g=rwx (if already readable and writable: g+x)
Note: Can be combined and written: ug=rwx (if it is already readable and writable: ug+x)
other users cannot read or write, do not execute: orwx or o-rwx

Insert picture description here
Example 2: Users, user groups and other users are all readable, writable and executable: ugo=rwx or a=rwx or a+rwx

Insert picture description here

Author's note:
You can try different combinations, but you must remember that you must use ls -l to write down the original file status before the experiment, or create a file or directory yourself and try it!

Guess you like

Origin blog.csdn.net/weixin_45631506/article/details/114906296