This article is organized from:
1. Basic knowledge
In Linux, when a file is created, the owner of the file is the user who created the file. The file user can modify the owner and user group of the file. Of course, the root user can modify the owner and user group of any file . In Linux, the permissions (rwx) of a file are divided into three parts, one part is the permissions owned by the owner of the file, one part is the permissions owned by the user in the user group where the file is located, and the
other part is owned by other users. permissions
. For file permissions, please refer to "
Linux chmod command
"
The permissions of files (including folders, the same below) can be completed by the chmod command in the shell. For this, please refer to
"
Linux chmod command
"
.
In the
shell
, you can use the
chown command
to change the file owner and user group, and the
chgrp command
to change the user group where the file is located.
In Linux
C programs
, you can use the
chown function
to change the file owner
and
user group.
Also,
in the shell, the current user to modify the file must have administrator root privileges. You can switch to the root user through the su command, or you can obtain root privileges through sudo.
Second, use the
chown command
to change the file owner
In the shell, the
chown command can be used
to change the file owner.
The chown command
is short for change owner. It should be noted that the
user must already exist in the system, that is, it can only be changed to the user name recorded in the /etc/passwd file
.
The chown command
has many uses, and you can also directly modify the name of the user group by the way. In addition, if you want to change the file owner at the same time with all subdirectories or files in the directory, you can directly add the
-R
parameter.
Basic syntax:
chown
[
-R
]
account name
file
or
directory
chown
[
-R
]
account name
:
user group name
file
or
directory
Parameters
:
-R
: make recursive continuous changes, that is, together with all files and directories in subdirectories
are updated to this user group. Often used when changing a directory.
Example 1
:
[root@localhost home]#
touch
testfile
//File created by root user
[root@localhost home]#
ls
testfile
–l
-rw--w--w- 1 root root 0 Jun 7 19:35 testfile
//The owner and owner level of the file are both root
[root@localhost home]#
chown
yangzongde
testfile
//modify the file owner to yangzongde
[root@localhost home]#
ls
testfile
-l
-rw--w--w- 1 yangzongde root 0 Jun 7 19:35 testfile
//Check that the owner of the file is yangzongde, but the group is still root
Example 2
:
chown
bininstall.log
ls
-l
-rw-r--r-- 1 bin users 68495 Jun 25 08:53 install.log
chown
root:
root
install.log
ls -l
-rw-r--r-- 1 root root 68495 Jun 25 08:53 install.log
3. Use the
chgrp command
to change the user group to which the file belongs
In the shell, you can use the
chgrp command
to change the user group to which the file belongs, which is short for change group. It should be noted that the name of the user group to be changed must exist in
/etc/group , otherwise an error will be displayed.
Basic syntax:
chgrp
[
-R
]
user group name
dirname
/
filename
...
parameter:
-R
: make recursive continuous changes, that is, together with all files and directories in subdirectories
are updated to this user group. Often used when changing a directory.
Example 3
[root@localhost home]#
ls
testfile
-l
-rw--w--w- 1 yangzongde root 0 Jun 7 19:35 testfile
//Check that the file owner is yangzongde, but the group is root
[root@localhost home]#
chgrp
yangzongde testfile //Modify the owner group to yangzongde
[root@localhost home]#
ls
testfile
-l
-rw--w--w- 1 yangzongde yangzongde 0 Jun 7 19:35 testfile
[root@localhost home]#
chown
root:
root
testfile
// Use chown to modify the owner and group at one time
[root@localhost home]#
ls
testfile
-l
-rw--w--w- 1 root root 0 Jun 7 19:35 testfile
Example 4
[root@linux ~]#
chgrp
users
install.log
[root @ linux ~] #
ls
-l
-rw-r--r-- 1 root users 68495 Jun 25 08:53 install.log
Example 5
Change to a user group that does not exist in /etc/group
[root@linux ~]#
chgrp
testing
install.log
chgrp: invalid group name `testing' <== an error message appears ~ the user group name cannot be found~
Fourth, the use of the chown function
In Linux C application programming, you can use the
chown function
to modify the owner and owner group of a file. This function is declared as follows:
/usr/include/unistd.h file
/* Change the owner and group of FILE. */
extern
int
chown
(
__const
char
*
__file
,
__uid_t
__owner
,
__gid_t
__group
)
__THROW __nonnull
((
1
))
__wur
;
The first parameter of this function is the file of the user to be modified, the second parameter is the owner of the modified file, and the third parameter is the group of the modified file owner.
For
open files
, use the
fchown function
to modify them. Its first parameter is the file descriptor of the opened file, and the others are the same as the chown function. The function is declared as follows:
/* Change the owner and group of the file that FD is open on. */
extern
int
fchown
(
int
__fd
,
__uid_t
__owner
,
__gid_t
__group
)
__THROW __wur
;
For
concatenating files
, you can use the lchown function. Its parameters are the same as the chown function.
/* Change owner and group of FILE, if it is a symbolic link the ownership of the symbolic
link is changed. */
extern
int
lchown
(
__const
char
*
__file
,
__uid_t
__owner
,
__gid_t
__group
)
__THROW __nonnull
((
1
))
__wur
;
If the above three functions are executed successfully, it will return 0, otherwise it will return -1.