How to Change the Owner and User Group in A Linux File?

Source: Internet
Author: User
Keywords change user group linux ubuntu change user group how to change user and group in linux

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, file permissions (rwx) are divided into three parts, one is the permissions owned by the owner of the file, the other is the permissions owned by the user in the user group where the file is located, and the other is owned by other users permission. For file permissions, please refer to "Linux chmod command"


Alibaba Cloud Simple Application Server:  Anti COVID-19 SME Enablement Program
$300 coupon package for all new SMEs and a $500 coupon for paying customers.


The permissions of files (including folders, the same below) can be completed in the shell through the chmod command. 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.

In addition, in the shell, the current user who wants to modify the file must have the administrator root authority. You can switch to the root user through the su command, or you can get root privileges through sudo.

Two, use the chown command to change the file owner

In the shell, you can use the chown command 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 a user name recorded in the /etc/passwd file.

The chown command has many uses, and you can directly modify the name of the user group by the way. In addition, if you want to change the file owner of all subdirectories or files in the directory at the same time, just add the -R parameter directly.

Basic syntax:
chown [-R] Account name File or directory
chown [-R] Account name: User group name File or directory

parameter:
-R: Perform recursive continuous changes, that is, together with all files and directories in subdirectories
Are updated to become this user group. It is often used when changing a certain 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 // View the file owner is yangzongde, but the group is still root

Example 2:
chown bin install.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

Three, 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. This command is the abbreviation of change group (change user group). Note 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: Perform recursive continuous changes, that is, together with all files and directories in subdirectories
Are updated to become this user group. It is often used when changing a certain directory.

Example 3
[root@localhost home]#ls testfile -l
-rw--w--w- 1 yangzongde root 0 Jun 7 19:35 testfile //View 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 once
[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 ~ Cannot find this user group name ~

Fourth, the use of chown function
In Linux C application programming, you can use the chown function to modify the file owner and owner group. 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 owner of the modified file.

For the opened file, use the fchown function to modify it. The first parameter is the file descriptor of the opened file, and the others are the same as the chown function. The function declaration is 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 connection files, you can use the lchown function. The parameters are the same as the chown function. To

/* 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;

To

If the above three functions are executed successfully, it will return 0, otherwise it will return -1.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.