Linux, modify the file or directory permissions chown, chgrp, chmod Detailed

chgrp command

Linux chgrp command is used to change their group of files or directories.

In the UNIX system family, the control file or directory permissions to the owner and their group to manage. You can use the chgrp command to change file and directory belongs to the group, set the mode using the group name or group ID can be.

grammar

chgrp [-cfhRv] [- help] [- version] [their group] [file or directory ...] or chgrp [-cfhRv] [- help] [- reference = <reference file or directory> ] [- version] [file or directory ...]

Parameter Description

  -c or --changes similar effect "-v" argument, but returns only partially changed.

  -f or --quiet or --silent not display an error message.

  -h or --no-dereference-only changes to a file symbolic link, without any change to other relevant documents.

  -R or --recursive recursive processing, all files and subdirectories in the specified directory dealt with together.

  -v --verbose display process or the execution instruction.

  --help online help.

  --reference = <reference file or directory> belongs to a group specified file or directory is set to all of their group and the same reference files or directories.

  --version display version information.

Examples

Example 1: change the group properties file:

chgrp -v bin log2012.log

Output:

[root@localhost test]# ll
---xrw-r-- 1 root root 302108 11-13 06:03 log2012.log
[root@localhost test]# chgrp -v bin log2012.log

"Log2012.log" belongs to the group has been changed to bin

[root@localhost test]# ll
---xrw-r-- 1 root bin  302108 11-13 06:03 log2012.log

Description: log2012.log file was changed by the root group bin Groups

Example 2: Change the group attribute file according to the specified file

chgrp --reference=log2012.log log2013.log

Output:

[root@localhost test]# ll
---xrw-r-- 1 root bin  302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root     61 11-13 06:03 log2013.log
[root@localhost test]#  chgrp --reference=log2012.log log2013.log 
[root@localhost test]# ll
---xrw-r-- 1 root bin  302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root bin      61 11-13 06:03 log2013.log

Description: changing the group attribute file log2013.log such group attribute group attribute and a file reference log2012.log same log2013.log

chmod command

File permissions calling Linux / Unix is ​​divided into three levels: file owner, group, others. Chmod can use so as to control how the file is called by others.

Usage rights  : All users

grammar

chmod [-cfvR] [--help] [--version] mode file...

Parameter Description

mode: permissions string in the following format:

[ugoa...][[+-=][rwxX]...][,...]

among them:

  • u represents the owner of the file, g represents the owner of the file belonging to the same group (group) who, o represents other than human, a represents three both.
  • + Indicates increased authority - indicates remove permissions, = represents a unique set permissions.
  • r represents read, w represents the writing, x represents an executable, X is only when this file is a subdirectory or file has been set through the executable.

Other parameters:

  • -c: If the file permissions have changed indeed, it shows the change action
  • -f: If the file permissions can not be changed and do not display an error message
  • Details show permission to change: -v
  • -R: all the files and subdirectories in the current directory the same change permissions (that is handed back to the way one by one to change)
  • --help: display HELP
  • --version: show the version

Examples

Anyone can file file1.txt to read:

chmod ugo+r file1.txt

Anyone can file file1.txt to read:

chmod a+r file1.txt

The file file1.txt with file2.txt set the file owner, it belongs with a group who can write, but others can not be a person other than write:

chmod ry + w, ow file1.txt file2.txt

The ex1.py set only the file owner can execute:

chmod u+x ex1.py

Anyone under the current directory of all the files and subdirectories are set to read:

chmod -R a+r *

In addition chmod you can also use numbers to represent rights such as:

chmod 777 file

The syntax is:

chmod abc file

Wherein a, b, c are each a number, respectively permissions User, Group, and the Other.

r=4,w=2,x=1

  • To the property rwx 4 + 2 + 1 = 7;
  • To rw- property is 4 + 2 = 6;
  • To the property rx 1 = 4 + 5.
chmod a=rwx file

with

chmod 777 file

The same effect

chmod = rwx, o = x file

with

chmod 771 file

The same effect

If this program can use chmod 4755 filename have root privileges

chown command

Use chown to change the owner of the specified file specified user or group, the user may be a user name or user ID; group can be a group name or group ID; file is a list of files to change permissions separated by spaces, support wildcards. .

Generally, this command is only used by the system administrator (root), end users do not have permission to change someone else's file owner, also do not have permission to change the owner of your own files to others. Only the system administrator (root) have permission to do so.

Usage rights  : root

grammar

chown [-cfhvR] [--help] [--version] user[:group] file...

Parameters  :

  • user: user new file owner ID
  • group: new file owner's user group (group)
  • -c: Change the display information part
  • -f: ignore the error message
  • -h: Repair symbolic link
  • -v: display detailed processing information
  • All files in the specified directory and its subdirectories process: -R
  • --help: display HELP
  • --version: show the version

Examples

The owner of the file file1.txt set runoob, user groups runoobgroup:

chown runoob:runoobgroup file1.txt

The current directory of all the files and subdirectories owned by key set runoob, user groups runoobgroup:

chown -R runoob:runoobgroup *

 

Published 109 original articles · won praise 101 · views 360 000 +

Guess you like

Origin blog.csdn.net/Alen_xiaoxin/article/details/105031968