Linux modifies the file owner and file group,,

chgrp username filename -R

chown username filename -R

-R means recursively all files in the directory

The above part is verified

1. Modify the group to which the file belongs—chgrp
is very simple to modify the group to which the file belongs—the chgrp command is the abbreviation of change group (we can use these to remember commands)
Syntax: chgrp group file name/directory
Example:
[root@redhat ~]# groupadd groupa
[root@redhat ~]# groupadd groupb
[root@redhat ~]# useradd -g groupa zgz
[root@redhat ~]# su - zgz
[zgz@redhat ~]$ touch filea
[zgz@redhat ~] ]$ touch fileb
[zgz@redhat ~]$ ls -l
total 8
-rw-r–r-- 1 zgz groupa 0 Sep 26 05:48 filea
-rw-r–r-- 1 zgz groupa 0 Sep 26 05: 50 fileb

[root@redhat zgz]# chgrp groupb filea --Change the group to which filea belongs
[root@redhat zgz]# ls -l
total 8
-rw-r–r-- 1 zgz groupb 0 Sep 26 05:48 filea
-rw-r–r-- 1 zgz groupa 0 Sep 26 05:50 fileb

2. Modify the file owner-chown
The command to modify the group is chgrp, that is, change group, then the command to modify the file owner is naturally chown, that is, change owner. There are many functions of chown. It can not only change the file owner, but also modify the group to which the file belongs. If you need to change the owner of all files in a certain directory, you can use the -R parameter.
The syntax is as follows:
chown [-R] account name file/directory
chown [-R] account name: group file/directory
Example:
[root@redhat zgz]# ls -l
total 20
-rw-r–r-- 1 zgz groupb 0 Sep 26 05:48 filea
-rw-r–r-- 1 zgz groupa 3 Sep 26 05:59 fileb
drwxr-xr-x 2 zgz groupa 4096 Sep 26 06:07 zgzdir
[root@redhat zgz]# chown myy fileb -- modify the owner of fileb to myy
[root@redhat zgz]# ls -l
total 20
-rw-r–r-- 1 zgz groupb 0 Sep 26 05:48 filea
-rw-r–r-- 1 myy groupa 3 Sep 26 05:59 fileb
drwxr-xr-x 2 zgz groupa 4096 Sep 26 06:07 zgzdir
[root@redhat zgz]# chown myy:groupa filea --Modify the owner of filea to myy, and
change the group to groupa when [root@redhat zgz]# ls -l
total 20
-rw-r–r-- 1 myy groupa 0 Sep 26 05:48 filea
-rw-r–r-- 1 myy groupa 3 Sep 26 05:59 fileb
drwxr-xr-x 2 zgz groupa 4096 Sep 26 06:07 zgzdir
[root@redhat zgz]# chown -R myy zgzdir Change the owner of all files under it at the same time
total 20
-rw-r–r-- 1 myy groupa 0 Sep 26 05:48 filea
-rw-r–r-- 1 myy groupa 3 Sep 26 05:59 fileb
drwxr-xr-x 2 myy groupa 4096 Sep 26 06:07 zgzdir
[root@redhat zgz]# cd zgzdir/
[root@redhat zgzdir]# ls -l
total 8
-rw-r–r-- 1 myy groupa 0 Sep 26 06:07 filec
-rw-r–r-- 1 myy groupa 0 Sep 26 06:07 filed

3. Change file permissions—chmod
1. Use numbers to change file permissions
We have already understood the meaning of -rw-r–r--, Linux assigns a fixed number to each permission:
r: 4 (read permission)
w: 2 (write permission)
x: 1 (execute permission)
We add these numbers together to get the permission value of each group, for example
-rw-r–r-- 1 myy groupa 0 Sep 26 06:07 filed
The first group (user): rw- = 4+2+0 = 6
The second group (group): r-- = 4+0+0 = 4
The third group (others): r-- = 4+0+ 0 = 4
, then 644 is the digital representation value of the fileb permission.
If we want to change the permissions of a certain file, we first need to convert the permissions into a combination of numbers. For example, if we want to get -rwxrw-r–, then we should get a combination of numbers: [4+2+1][4+2+0][ 4+0+0]=764, and then use the chmod command to modify the
chmod syntax:
chmod xyz file/directory
Example:
[root@redhat zgzdir]# ls -l
total 8
-rw-r–r-- 1 myy groupa 0 Sep 26 06:07 filec
-rw-r–r-- 1 myy groupa 0 Sep 26 06:07 filed
[root@redhat zgzdir]# chmod 777 filec–change filec permissions to 777
[ root@redhat zgzdir]# ls -l
total 8
-rwxrwxrwx 1 myy groupa 0 Sep 26 06:07 filec
-rw-r–r-- 1 myy groupa 0 Sep 26 06:07 filed
[root@redhat zgzdir]# chmod 750 filed–Internally filedwithinstall 750
[root@redhat zgzdir]# ls -l
total 8
-rwxrwxrwx 1 myy groupa 0 Sep 26 06:07 filec
-rwxr-x— 1 myy groupa 0 Sep 26 06:07 filed

2. Use characters to change file permissions
There is another way to change permissions. We have learned that file permissions are divided into three groups, namely user, group, and others. Then we can use u, g, and o to represent the three groups respectively. In addition, a (all) represents all, and the permission attribute can be represented by r, w, x three characters, then please see the following syntax:
chmod u/g/o/a +(add)/-(remove)/ =(set) r/w/x file or directory

Example:
We want to get the filed file: u: readable, writable, executable
g, o: readable, executable
[root@redhat zgzdir]# ls -l
total 8
-rwxrwxrwx 1 myy groupa 0 Sep 26 06: 07 filec
-rwxr-x— 1 myy groupa 0 Sep 26 06:07 filed
[root@redhat zgzdir]# chmod u=rwx,go=rx filed – modify the file attributes of filed
[root@redhat zgzdir]# ls -l
total 8
-rwxrwxrwx 1 myy groupa 0 Sep 26 06:07 filec
-rwxr-xr-x 1 myy groupa 0 Sep 26 06:07 filed
Among them, g and o can also be set separately by ",".
Assuming that I don't know the permissions of each group at present, but just want to add "x" permissions to all groups, then we can use chmod a+x filename to achieve, for example: [root@redhat
zgz
]# ls -l
total 24
-rw -r–r-- 1 myy groupa 0 Sep 26 05:48 filea
-rw-r–r-- 1 myy groupa 3 Sep 26 05:59 fileb
-rw-r–r-- 1 zgz groupa 0 Sep 26 06:39 fileg
drwxr-xr-x 2 myy groupa 4096 Sep 26 06:07 zgzdir
[root@redhat zgz]# chmod a+x filea–modify the file of filea attribute, all groups have "x" permissions added
[root@redhat zgz]# ls -l
total 24
-rwxr-xr-x 1 myy groupa 0 Sep 26 05:48 filea
-rw-r–r-- 1 myy groupa 3 Sep 26 05:59 fileb
-rw-r–r-- 1 zgz groupa 0 Sep 26 06:39 fileg
drwxr-xr-x 2 myy groupa 4096 Sep 26 06:07 zgzdir
If you want to remove a certain permission, you can use "- ",
for example:
[root@redhat zgz]# ls -l
total 24
-rwxr-xr-x 1 myy groupa 0 Sep 26 05:48 filea
-rw-r–r-- 1 myy groupa 3 Sep 26 05: 59 fileb
-rw-r–r-- 1 zgz groupa 0 Sep 26 06:39 fileg
drwxr-xr-x 2 myy groupa 4096 Sep 26 06:07 zgzdir
[root@redhat zgz]# chmod ax filea-Modify filea file attributes and remove "x" permissions for all groups
[root@redhat zgz]# ls -l
total 24
-rw-r–r-- 1 myy groupa 0 Sep 26 05 :48 filea
-rw-r–r-- 1 myy groupa 3 Sep 26 05:59 fileb
-rw-r–r-- 1 zgz groupa 0 Sep 26 06:39 fileg
drwxr-xr-x 2 myy groupa 4096 Sep 26 06:07 zgzdir
[root@redhat zgz]#

Friendly reminder:
chgrp, chown, chmod these commands are executed by default only root has permission, you may sometimes use ordinary accounts to modify file permissions, linux will prompt you that you do not have this permission. Therefore, everyone must pay attention to the current user, for example:
[zgz@redhat ~]$ chgrp groupb filea
chgrp: changing group of `filea': Operation not permitted
– zgz does not have permission to change the group of 'filea'

Guess you like

Origin blog.csdn.net/liu511623/article/details/83933995