Linux change owner and group

Содержание
  1. Chown Command: Change Owner of File in Linux
  2. Linux Chown Command Syntax
  3. How to Check Ownership of a File in Linux
  4. How to Change the Owner of a File
  5. Change the Owner of a File With UID
  6. Change Ownership of Multiple Linux Files
  7. How to Change the Group of a File
  8. Change the Group of a File Using GID
  9. Change Owner and the Group
  10. Change Group to a Users Login Group
  11. Transfer Ownership and Group Settings from One File to Another
  12. Check Owner and Group Before Making Changes
  13. Check Owner Only
  14. Check Group Only
  15. How to Recursively Change File Ownership
  16. Chown Command and Symbolic Links
  17. Display Chown Command Process Details
  18. Suppress Chown Command Errors
  19. How to Change File/Directory User and Group Ownership in Linux
  20. What’s chown Command
  21. 1) How to Change the Owner of a File/Directory on Linux Using the chown Command
  22. 2) How to Change the Group Ownership of a File/Directory on Linux Using the chown Command
  23. 3) How to Change the User and Group Ownership of a File/Directory on Linux Using the chown Command
  24. 4) How to Change the User and Group Ownership Recursively on Linux Using the chown Command
  25. 5) How to Change User and Group Ownership of Multiple Files on Linux Using the chown Command
  26. 6) How to Change the User and Group Ownership of Link Files on Linux Using the chown Command
  27. 7) How do i Change the User and Group Ownership of a File Using the “–from=” option Using the chown Command?
  28. 8) How to Copy User and Group Ownership from One File to Another on Linux Using the chown Command
  29. 9) Bonus Tips: How To Find And Change Ownership of Files Based On Specific Extension
  30. 17 Useful Linux chown command examples to change owner and group
  31. SYNOPSIS
  32. chown command examples to Change Owner and Group
  33. Example 1: How to Check chown command version
  34. Example 2: How to Change User and Group Ownership of a File
  35. Example 3: How to Enable Verbose mode
  36. Example 4: How to report only when the user or group owner are changed
  37. Example 5: How to Change the ownership of a Directory
  38. Example 6: How to Only change the Owner of a File
  39. Example 7: How to Change Only the Group of a File
  40. Example 8: How to Change the owner and group of a Symbolic Link
  41. Example 9: How to replicate File Access to Another File
  42. Example 10: How to Recursively change the ownership of all the Files in a Directory
  43. Example 11: Change owner only when if it is assigned to Specific Owner
  44. Example 12: Change the group of a file Only when it is assigned to a Specific Group
  45. Example 13: Change the owner and group of a file Only when it is assigned to a Specific Owner and Group
  46. Example 14: How to forcefully change the owner and group of a Symbolic Link File
  47. Example 15: How to forcefully change the owner and group of a Symbolic link directory
  48. Example 16: How to Check all the other options of chown command
  49. Example 17: How to Check Man page of chown command

Chown Command: Change Owner of File in Linux

Home » SysAdmin » Chown Command: Change Owner of File in Linux

The chown command changes user ownership of a file, directory, or link in Linux. Every file is associated with an owning user or group. It is critical to configure file and folder permissions properly.

In this tutorial, learn how to use the Linux chown command with examples provided.

  • Linux or UNIX-like system
  • Access to a terminal/command line
  • A user with sudo privileges to change the ownership. Remember to run the commands with sudo to execute them properly.

Linux Chown Command Syntax

The basic chown command syntax consists of a few segments. The help file shows the following format:

  • [OPTIONS] – the command can be used with or without additional options.
  • [USER] – the username or the numeric user ID of the new owner of a file.
  • [:] – use the colon when changing a group of a file.
  • [GROUP] – changing the group ownership of a file is optional.
  • FILE – the target file.

Superuser permissions are necessary to execute the chown command.

In this guide, we tested the command examples with the chown version 8.28 in Ubuntu 18.04.2 LTS.

To check the chown version on your machine, enter:

The output will look similar to this:

How to Check Ownership of a File in Linux

First, you need to know the original file owner or group before making ownership changes using the chown command.

To check the group or ownership of Linux files and directories in the current location, run the following command:

An example output of the ls command looks like this:

How to Change the Owner of a File

Changing the owner of a file with chown requires you to specify the new owner and the file. The format of the command is:

The following command changes the ownership of a file sample from root to the user test:

Use the same format to change the ownership for both files and directories.

Change the Owner of a File With UID

Instead of a username, you can specify a user ID to change the ownership of a file.

Make sure there is no user with the same name as the numeric UID. If there is, the chown command gives priority to the username, not the UID.

Note: To check a user’s ID, run id -u USERNAME from the terminal.

Change Ownership of Multiple Linux Files

List the target file names after the new user to change the ownership for multiple files. Use single spaces between the file names.

In the following example, root will be the new owner of files sample2 and sample3.

Combine file names and directory names to change their ownership with one command. For example:

Do not forget that the commands are case sensitive.

How to Change the Group of a File

With chown, you can change a group for a file or directory without changing the owning user. The result is the same as using the chgrp command.

Run the chown command using the colon and a group name:

The following example changes the group of the file sample3 from grouptest to group3.

List multiple names of files or directories to make bulk changes.

Change the Group of a File Using GID

Similar to UID, use a group ID (GID) instead of a group name to change the group of a file.

Change Owner and the Group

To assign a new owner of a file and change its group at the same time, run the chown command in this format:

Therefore, to set linuxuser as the new owner and group2 as the new group of the file sample2:

Remember that there are no spaces before or after the colon.

Change Group to a Users Login Group

The chown command assigns the owner’s login group to the file when no group is specified.

Читайте также:  Установка ejabberd astra linux

To do so, define a new user followed by a colon, space, and the target file:

The following example changes the group ownership to the login group of linuxuser:

Transfer Ownership and Group Settings from One File to Another

Rather than changing the ownership to a specific user, you can use the owner and a group of a reference file.

Add the —reference option to the chown command to copy the settings from one file to another:

Remember to type in the names of the files correctly to avoid the error message:

Check Owner and Group Before Making Changes

The chown command —from option lets you verify the current owner and group and then apply changes.

The chown syntax for checking both the user and group looks like this:

The example below shows we first verified the ownership and the group of the file sample3:

Then chown changed the owner to linuxuser and the group to group3.

Check Owner Only

The option —from can be used to validate only the current user of a file.

Check Group Only

Similar to the previous section, you can validate only the group of a file using the option —from .

Here is an example where we verified the current group before changing it:

Remember to use the colon for both group names to avoid error messages.

How to Recursively Change File Ownership

The chown command allows changing the ownership of all files and subdirectories within a specified directory. Add the -R option to the command to do so:

In the following example, we will recursively change the owner and the group for all files and directories in Dir1.

To change the owner of a symbolic link, use the -h option. Otherwise, the ownership of the linked file will be changed.

The following image shows how symbolic links behave when -h is omitted.

The owner and group of the symbolic link remain intact. Instead, the owner and the group of the file textfile changed.

To push the changes to the link, run the chown command with the -h flag:

In the following example, we changed the owner and group of a symbolic link.

Display Chown Command Process Details

By default, the terminal does not display the chown process information. To see what happens under the hood, use one of the two command line flags:

  • The option –v produces the process details even when the ownership stays the same.
  • The option –c displays the output information only when an owner or group of the target file changes.

For example, if we specify the current owner as a new owner of the file:

The terminal produces the following output:

Switch from -v to -c and there will be no messages in this case. This happens because there are no owner or group changes.

The information is particularly useful with the recursive chown command:

In this example, the output lists all objects affected after running the command.

Suppress Chown Command Errors

To avoid seeing potential error messages when running the chown command, use the -f option:

The example below shows the error message for a non-existent file or directory:

Adding the -f flag suppresses most error messages. However, if you specify an invalid username, the error message appears:

Now you know how to use chown command in Linux to change a file’s user and/or group ownership.

Take extra caution when changing the group or ownership of a file or directories.

Источник

How to Change File/Directory User and Group Ownership in Linux

Linux is a multi-user system that can be accessed simultaneously by multiple users.

File permissions are a way to restrict users to access other user’s files on Linux.

It is controlled by file ownership and file permissions.

Each file in Linux has 3 types of owners, it’s user, group, and others.

A user is the owner of the file, and group is the one to which the user belongs to.

Each file and directory has three permissions for all three owners, such as read, write, and execute.

This can be done using the chown command and the chgrp command.

By default these commands do not display any output, so use the “-v” option to get information about what’s being done.

Use the ls command to find out who owns a file and which group the file belongs to.

What’s chown Command

The chown command is used to change the user and/or group ownership of each given file. This allows the user to perform various actions and the details are described below.

The common syntax is as follows:

  • USER – If you provide only a username, the group of files will not be changed, becoming the owner of a given user file.
  • USER: – If you provide a username followed by a colon, the given user will become the owner of the files and the group of files will be converted into that user login group.
  • USER:GROUP – If you provide a username followed by a colon and group name, the given user will become the owner of the files and the group of files.
  • :GROUP – If you provide a group name after the colon, and owner is excluded, only the group of files will be transferred. It works the same way as chgrp.
  • : – If you only provide a colon, no action is performed.

Alternatively, you can use “-c” switch instead of “-v” switch, it works like verbose but report only when a change is made.

1) How to Change the Owner of a File/Directory on Linux Using the chown Command

In this example, we are going to change the ownership of the “passwd-up.sh” file from “daygeek” to “root”.
Before

2) How to Change the Group Ownership of a File/Directory on Linux Using the chown Command

In this example, we are going to change the group ownership of the file “mysql_backup.sh” from “daygeek” to “root”.

3) How to Change the User and Group Ownership of a File/Directory on Linux Using the chown Command

Use the example below to change the file’s user and group ownership. In this example, the user and group ownership of the “test.txt” file will be changed from “daygeek” to “root”.

4) How to Change the User and Group Ownership Recursively on Linux Using the chown Command

In this example, the ownership of all the files and its sub-directories is changed from “daygeek” to “root”, which includes the parent directory rights.

5) How to Change User and Group Ownership of Multiple Files on Linux Using the chown Command

To change the user and group ownership of multiple files, use the example below.

6) How to Change the User and Group Ownership of Link Files on Linux Using the chown Command

Use the following examples for changing ownership of link files.

Читайте также:  Как проверить жесткий диск если windows не запускается

When you use the chown command as usual in link files, it only changes the ownership of the source file, not the link file itself. In this example, it changes the ownership of the “output.txt” file from “daygeek” to “root”.

Use the “-h” option with the chown command to change the ownership of the link file instead of the source file.

Note: “-L” and “-H” options can be used when recursively changing the symbolic link directory ownership.

7) How do i Change the User and Group Ownership of a File Using the “–from=” option Using the chown Command?

Alternatively, you can use the “–from=” option to change ownership of files.

8) How to Copy User and Group Ownership from One File to Another on Linux Using the chown Command

In some cases, if you want to copy ownership from one file to another, use the example below.
In this example, the owner permission is copied from the “2.txt” file to the “renu.txt” file.

9) Bonus Tips: How To Find And Change Ownership of Files Based On Specific Extension

In this example, the ownership of the “.sh” extension files will be changed to “daygeek”.

Here are some helpful find command tutorials for you.

Источник

17 Useful Linux chown command examples to change owner and group

Table of Contents

In this article, I will take you through 17 Linux chown command examples to change owner and group. As you might be aware that all the file and directories in Linux are having a owner and Group assigned so if you decide to change the owner and group of a file or directory in Linux then you need to use chown and chgrp command.

chown is an open source Linux command used for changing the ownership of Files and Directories. chgrp is also an open source Linux command to change the group of files and directories. Here we will only look at the chown command and will see chgrp command in later articles. I will go through the usage of chown command using various examples.

SYNOPSIS

chown [OPTION]. [OWNER][:[GROUP]] FILE.
chown [OPTION]. —reference=RFILE FILE.

chown command examples to Change Owner and Group

Example 1: How to Check chown command version

If you want to check chown command version then you need to use chown —version command as shown below. As you can see from below output current chown version is 8.22 .

Example 2: How to Change User and Group Ownership of a File

If you want to change the owner of a file then you need to use below chown command. In this example we are changing the ownership of hello.txt file to user centos and group centos by using chown centos:centos hello.txt command.

Example 3: How to Enable Verbose mode

If you want to enable the verbose mode then you need to use -v option with chown command as shown below. Enabling the verbose mode will show the command output in detail.

-v : output a diagnostic for every file processed. More can be checked on chown Man Page.

Example 4: How to report only when the user or group owner are changed

If you want to check that the chown command is changing the user or group of the file or directory then you need to use -c option as shown below. Let’s say if you give the same user and group name who is already the owner of file or directory then chown command won’t show anything in the output as shown below. Here owner centos user and centos group are already associated with hello.txt file so assigning same user and group won’t show anything in the output.

-c : like verbose but report only when a change is made. More can be checked on chown command Man Page.

But if you change the owner to different user and group then it will show that change in the output as shown below. Here we are changing the owner of hello.txt file to user root and group root from previous owner centos and group centos using chown -c root:root hello.txt command so it is displaying that change in the output. This confirms that we are assigning different owner and group to hello.txt file.

Example 5: How to Change the ownership of a Directory

If you want to change the ownership of a directory then you need to use same chown command as you have used in above examples. There is not much difference in changing the ownership of a file and directory. In this example we are changing the ownership of example directory but first we need to check the current owner and group of this directory by using ls -lrtd example command. As you can see from below output owner is root and associated group is also root.

So to change the ownership of example directory to user centos and group to centos we need to use chown -v centos:centos example command as shown below.

Here you need to remember that we are only changing the ownership of example directory and not the files and directories contains within it as you can see below. The owners of all the files of example directory are still the same. It has not changed its owner to User centos and group centos .

Example 6: How to Only change the Owner of a File

If you are interested in only changing the owner of a file and not the group then you can use below chown command. In this example we are changing the owner of hello.txt from user centos to user root but first we are checking the current owner of hello.txt file using ls -lrt hello.txt command as shown below.

Now we can change the owner of hello.txt file to root using chown -v root hello.txt command as shown below.

If you verify again using ls -lrt hello.txt command then you can see that owner is not changed to root .

Example 7: How to Change Only the Group of a File

If you are only interested in changing the group of a File then you can do that by using below chown command. In this example hello.txt group is root which we will change it to group centos . Before changing the group we need to verify current group of hello.txt file by using ls -lrt hello.txt command as shown below.

Now we can change the group of hello.txt file using chown -v :centos hello.txt command as shown below.

If you verify again using ls -lrt hello.txt command then you can see that the group is now changed to centos .

Читайте также:  Irbis nb211 установка windows 10

Example 8: How to Change the owner and group of a Symbolic Link

If you want to change the owner and group of a Symbolic Link then you need use below chown command. In this example, we have a symbolic link hello pointing to hello.txt file as shown below. Also we can see root is the current owner and group of this symbolic link.

Now to change the ownership to user centos and group centos we need to use chown -v centos:centos hello command as shown below.

If you want to verify the changes then you can run ls -lrt hello command and check the ownership.

Example 9: How to replicate File Access to Another File

If you want to replicate access of a file to another file then you need to use —reference option with chown command as shown below. In this example we are replicating the access of hello.txt file to example.txt file. First check the access of hello.txt file using ls -lrt hello.txt command.

Then replicate the hello.txt file access to example.txt file using chown -v —reference=hello.txt example.txt command as shown below.

—reference=RFILE : use RFILE’s owner and group rather than specifying OWNER:GROUP values. More can be checked on chown command Man Page.

Example 10: How to Recursively change the ownership of all the Files in a Directory

If you want to recursively change the ownership of all the files in a directory then you need to use -R option with chown command as shown below. In this example, we have a directory example which contains two files hello.rs and hello .

Now to recursively change the ownership of all files of example directory to user centos and group centos you need to use chown -R -v centos:centos example command as shown below.

-R : operate on files and directories recursively. More can be checked on chown command Man Page.

If you again check the access of all the files of example directory then you can see it has now changed to user centos and group centos .

Example 11: Change owner only when if it is assigned to Specific Owner

If you want to change the owner of a file only when if it is assigned to a Specific Owner then you need to use —from option with chown command as shown below. In this example, we will change the owner of hello.txt file to root only when its current owner is centos . If it has some other owner then it won’t change the ownership to root user. First we need to check the current owner of hello.txt file using ls -lrt hello.txt command.

Now if we try to change the owner of file hello.txt to test from admin user using command chown -v —from=admin test hello.txt then it won’t change it as the current owner is centos and not the admin user.

—from : change the owner and/or group of each file only if its current owner and/or group match those specified here. More can be checked on chown command Man Page.

Again if you run the command chown -v —from=centos root hello.txt then it will now change the ownership to user root as the current owner is user centos .

Example 12: Change the group of a file Only when it is assigned to a Specific Group

If you want to change the owner of a file only when if it is assigned to a Specific Owner then you need to use —from option with chown command as shown below. In this example, we will change the owner of hello.txt file to root only when its current owner is centos . If it has some other owner then it won’t change the ownership to root user. First we need to check the current owner of hello.txt file using ls -lrt hello.txt command.

Now to change the group of hello.txt file to root from centos group you need to use chown -v —from=:centos :root hello.txt command as shown below.

Example 13: Change the owner and group of a file Only when it is assigned to a Specific Owner and Group

If you want to change the owner and group of a file only when it is assigned to a specific user and group then you need to use below chown command. First we need to check the owner and group of hello.txt file using ls -lrt hello.txt command.

Now to change the owner and group of hello.txt file to centos you need to use chown -v —from=root:root centos:centos hello.txt command as shown below.

If you verify the ownership of hello.txt file again using ls -lrt hello.txt command then you can see it has now changed to user centos and group centos .

Example 14: How to forcefully change the owner and group of a Symbolic Link File

If you want to forcefully change the owner and group of a Symbolic Link file then you need to use -h option with chown command as shown below. First we need to check the owner and group of symbolic link hello using ls -lrt hello command.

Now to change the owner and group of hello symbolic link to centos you need to use chown -v -h centos:centos hello command as shown below.

-h : affect symbolic links instead of any referenced file (useful only on systems that can change the ownership of a symlink). More can be checked on chown command Man Page.

If you again check the owner and group of symbolic Link hello using ls -lrt hello command then you can see it has now changed to centos as described below.

Example 15: How to forcefully change the owner and group of a Symbolic link directory

If you want to forcefully change the owner and group of a Symbolic link directory then you need to use -H option with chown command as shown below. In this example, first we are checking the owner and group of example directory symlink test using ls -lrt test command as shown below.

Here we are changing the owner and group of symbolic link test using chown -v -R -H centos:centos test command as shown below.

-H : if a command line argument is a symbolic link to a directory, traverse it. More can be checked on chown command Man Page.

Example 16: How to Check all the other options of chown command

If you want to check all the available options of chown command then you need to use chown —help command as shown below.

Example 17: How to Check Man page of chown command

If you want to check the man page of chown command then you need to use man chown command as shown below.

Popular Recommendations:-

Источник

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