Linux files change group

How to Change File/Group Owner with chown Command in Linux

Short for change ownership, Chown command is a command-line utility that is used to change the user or group ownership of a file or directory and even links. The Linux philosophy is such that every file or directory is owned by a specific user or group with certain access rights.

Using different examples, we will try and see the various use cases of the chown command . Chown command employs quite a simple and straight forward syntax.

$ chown OPTIONS USER: GROUP file(s)

Let’s briefly flesh out the parameters:

The attribute USER refers to the username of the user that will own the file. You can specify either the username or the UID ( User ID). Meanwhile, the GROUP option indicates the name of the new group that the file will acquire after running the command. The file option represents a regular file or a directory or even a symbolic link. These are the three entities whose permissions can be altered.

A few points to note:

1) When the USER option is specified alone, ownership of the file/directory changes to that of the specified user while the group ownership remains unchanged. Here’s an example:

In the above command, user ownership of the file file1.txt changes from the current user to the user john.

2) If the USER option is proceeded by a full colon i.e. USER: and the group name is not provided, then the user takes ownership of the file but the file’s group ownership switches to the user’s login group. For example:

In this example, the user john takes ownership of the file file1.txt, but the group ownership of the file changes to john’s login group.

3) When both the user and group options are specified separated by a colon i.e USER:GROUP – without any spaces therein – the file takes ownership of the new user and the group as specified

In the above example, the file takes the user and group ownership of user john.

4) When the USER option is left out and instead the group option is preceded by the full colon :GROUP then, only the group ownership of the file changes.

Читайте также:  Linux efi boot stub

How to view file permissions

To view file permissions, simply use the ls -l command followed by the file name

list-file-permissions-linux

From the output, we can see that the file is owned by user linuxtechi which and belongs to the group linuxtechi in the 3rd and 4th columns respectively.

How to change file owner with chown command

Before changing permissions, always invoke sudo if you are not working as the root user. This gives you elevated privileges to change user and group ownership of a file.

To change file ownership, use the syntax:

$ sudo chown james file1.txt

Change-file-owner-linux-chown-command

From the output, you can clearly see that the ownership of the file has changed from linuxtechi to user james .

Alternatively, instead of using the username, you can pass the UID of the user instead. To get the UID, view the /etc/passwd file.

$ cat /etc/passwd | grep username

From the example below, we can see that the UID of user linuxtechi is 1002

uid-file-linux-command

To change the file ownership back to linuxtechi user, we shall execute the command:

chown-uid-linux-file-command

How to change the group owner with chown command

As earlier discussed, to change the group owner of a file, omit the user and simply prefix the group name with a full colon.

For example, to change the group owner of file1.txt from linuxtechi to docker , we executed the command:

$ sudo chown :docker file1.txt

Changing-group-owner-chown-linux-command

How to change both file owner and group owner of a file

If you want to change both the owner and group that a file belongs to, specify both the user and group options separated by a full colon as shown in the syntax below. Be sure that there are no spaces between the options and the colon.

$ sudo chown user:group filename

For example, the following command changes the ownership of the file file1.txt to user james and group redis as verified using the ls command.

$ sudo chown james:redis file1.txt

change-file-group-owner-chown-command

How to recursively change file ownership

When applying permissions to directories, you might want to apply changes recursively i.e make the ownership changes to descend and apply to files and sub-directories. To achieve this, user the recursive option -R or –recursive directive.

$ sudo chown -R user:group directory

For example, the command below assigns all files and folders in the /var/www directory ownership to the www-data group.

$ sudo chown -R :www-data /var/www

The example below assigns ownership of the directory reports alongside all the files and folders in the directory to the user linuxtechi.

$ sudo chown -R linuxtechi reports

How to change ownership using a reference file

Lastly, there’s a nifty way that you can use to change ownership of a file, and that is by using a reference file. Using the chown command, you can change the user and group ownership of a file using another file as the point of reference.

The syntax is shown below:

Читайте также:  Alt linux rescue disk

$ chown –reference=ref_file file

Suppose you want to assign user and group ownership of file1.txt to another file file2.txt. How would you go about it? This is illustrated in the command below.

$ chown --reference=file1.txt file2.txt

linux-chown-reference-command-example

The output above confirms that file2.txt inherits the user and group ownership of file1.txt .In the command, file1.txt is the reference file.

Chown command is a powerful tool that is used for managing file and directory ownership. For additional information, check out the chown man pages.

Источник

5 Practical Examples of chgrp command in Linux

chgrp command is used for changing the group of a file or directory in Linux. This guide shows you how to use chgrp command in Linux with practical examples.

What is chgrp command in Linux?

If you used our chmod calculator, you are aware of file permissions in Linux, you are probably aware of the group ownership.

chgrp command in Linux is used for changing the group of a file or directory. It stands for ‘change group’.

The syntax for the chgrp command is:

chgrp [options] groupname file

5 Practical examples of chgrp command in Linux

Let’s see how to use chgrp command with these useful examples.

1. Change group of files/directories

This is the simplest and perhaps the most prominent use of the chgrp. To change the group ownership of a file or directory, you can use the chgrp command in the following manner:

chgrp group_name file_name

You can also change the group for multiple files at once:

chgrp group_name file1 file2 file3

You don’t have to be in the same directory as the file. You can provide the absolute or relative path as well.

Your current privileges matter. If you try to change the groups to admin or root, you might need super user privileges. You should see a ‘operation not permitted’ error in such cases.

chgrp supports tab completion. Just type a few letters for the group name and hit tab to see what groups exist with those letters.

2. Use chgrp recursively to change group all the files and sub-directories

By default, if you use chgrp with a directory, it only changes the group of the directory. The files and sub-directories remain the same.

If you want to change the group of all the files in the directories and in the sub-directories, you can use the recursive option -R.

chgrp -R group_name path_to_directory

3. Know if you managed to change the group

You can figure out if the group has been changed by using the ‘ls -l’ command. But what if you changed the group for several files at once like using the recursive option you saw in the previous section?

chgrp provides a verbose mode that tells you what operations your chgrp command performed. You can use the option -v to run chgrp command in verbose mode.

chgrp -vR abhishek samplechanged group of 'sample/agatha.txt' from sudo to abhishek group of 'sample/a.text' retained as abhishek changed group of 'sample/text/sherlock.txt' from sudo to abhishek changed group of 'sample/text' from sudo to abhishek changed group of 'sample' from sudo to abhishek

You may notice that the verbose mode also tells if the group of a file remained the same. If you want to see this information only for the files for which there were actually a change in group ownership, you can use option -c.

Tip: You can use chgrp to give execute permission to a command (in bin or in init) or so that the command can be executed by all users belonging to a specific group (instead of root).

4. Change the group ownership the same as a reference file

Imagine that you want to change the group of file A the same as file B. How would you do that? You can look for the group of file B and then use the chgrp command with the group name of file B.

Читайте также:  Install ssh on kali linux

Well, that’s one way of doing it. However, chgrp provides a dedicated way of changing the group based on a reference file instead of using the name of the group explicitly.

chgrp --reference=file2 file1

This is particularly helpful if you are writing a script where the group owners of files need to be changed as a reference file.

By default, if you use the chgrp command with a symbolic link, it’s the group owner of the referenced file that gets changed while the group of symbolic link remains as it is.

For example, this is the sate of the link and its referenced file:

ls -l agatha.txt link.txt -r--r--rw- 1 abhishek abhishek 457 Aug 10 11:55 agatha.txt lrwxrwxrwx 1 abhishek abhishek 10 Aug 19 10:19 link.txt -> agatha.txt

Now if you change the group of the symbolic link like this:

The group of symbolic link will remain the same while the group of the referenced file will be changed.

ls -l agatha.txt link.txt -r--r--rw- 1 abhishek sudo 457 Aug 10 11:55 agatha.txt lrwxrwxrwx 1 abhishek abhishek 10 Aug 19 10:19 link.txt -> agatha.txt

If you want to change just the group ownership of the symbolic link and not the referenced file itself, you can use the -h option.

However, I won’t suggest it because in Linux, link permissions don’t have meaning. The referenced file is what matters here.

Why use chgrp when you can use chown for changing group?

You can also use chown command for changing the group of a file but changing only the group through chown command is not standard. Also, it could be confusing to use chown for this purpose. chgrp is pretty straightforward and it is recommended to use chgrp command for changing the group of a file or directory.

I hope you liked the chgrp command examples. If you have questions or suggestions or a simple thanks, please use the comment box below.

Источник

Оцените статью
Adblock
detector