Use Chown Command in Linux to Change File Ownership

How to Use Chown Command in Linux to Change File Ownership

Linux is a multi-user system and every file and directory has a group and owner assigned to it. A file’s default owner is the user who created it. However, there can be times when you need to change a file or directory’s ownership. To change the file owner or group, Chown command is used. In this article, you will learn How to Use Chown Command in Linux to Change File Ownership. In Linux, we use permissions to limit what a user may do with a file or directory to protect and secure it. As a file owner, you can read, write, and execute the file. The Chown command enables you to change the ownership of a file, link, or directory and configure its permissions correctly.

To manage changes to file owners or groups to simplify user management, you need to have a solid understanding of the Linux ‘chown’ command. As you might know, the root user has access to all the directories and files and can perform any operation as a superuser. On the other hand, the regular user only has limited access to files and directories to change the files that they own. Join us with this article to review some examples that help you learn the way you can use the Chown command to change the ownership of the user or group of a file, directory, or link.

When Should You Change Ownership?

Previously, you have learned how to use find command to search files and directories in Linux. Also, you might face some situations in which changing file ownership is needed in Linux. Let’s see the examples of those situations:

  • The user and group owners of the account you want to utilize the files under on the new Linux computer must be changed if you transfer data across Linux or Unix-like operating systems.
  • You can create a file or directory while signing in as root, but you want a specific user to be able to access it.
  • If a user leaves your company, another staff person will be in charge of all of his files. The staff person who is currently in charge of those files must be changed to the owner and group owner.
  • You might write a script that will be used by a particular user.

Prerequisites to Use Chown Command in Linux

Before learning how to use the chown command in Linux, make sure that your system is meeting below specifications:

  • A Server running Linux VPS to manage files and directory ownership on it.
  • A non-root user with sudo privileges. To run and use the chown command, you will need superuser permissions.
  • Access to a Terminal/Command line.

What is Chown Command and How to Use it in Linux?

Linux Chown command stands for ”Change Owner”. As the name says, this command is used to change the ownership of files and directories in a Linux system. So, it is used to modify the owner of a file or directory to another user or group. To control file ownership in Linux, the chown command is a crucial tool. You can modify a file or directory’s owner, group, or both using this feature. You can more efficiently handle your files and directories if you comprehend the chown command’s fundamental syntax and options. The chown command is a valuable addition to your toolbox, regardless of your level of experience. The proper application of the chown command can help secure your files from outsiders and stop unauthorized users from altering them. Chown by default returns zero after a successful operation and produces no output.

Basic Syntax of Linux Chown Command

Let’s see the Linux Chown Command Syntax which is used to change the file or group owner.

chown [OPTIONS] USER[:GROUP] FILE(s)

To see the way you can use the chown command in Linux, read the details below:

  • [OPTIONS] – Shows the flags that a user can send with the command. Using the man command, one can locate a complete list of flags. The syntax, explanation, and, in certain situations, examples for a specific command are also included in this command.
  • [USER] – If only the user is specified, the group ownership of the files remains unchanged and the user indicated will take ownership of them.
  • [:] – No change is made if simply a colon (:) is provided without identifying the user or the group.
  • [GROUP] – A file’s group ownership can be changed if desired.
  • USER:GROUP – When a user and a group are both specified (without a space in between), the ownership of the files is changed to the user and group specified, respectively.
  • FILE – Your Target File.
  • USER:: – The user becomes the owner of the files when the username is preceded by a colon (:) and the group name is omitted. The ownership of the files is then changed to the user’s login group.

Before using the chown command, you can conform user and group permissions. To do this, log in to your purchased VPS using SSH and switch to your considered directory. In this way, you can go to the /home/operavps, if the file is there:

cd /home/operavps

View Groups, UID, and GID

Run the following command to list the groups you are in:

groups

And, to view the list of the groups, their numerical IDs, and your UID and GID, type:

id

Some options enable you to refine the output with ID.

ID OptionDescriptionSyntax
-uList your UID.id -u
-gList your effective (current) GID.id -g
-nuList your user name.id -nu
-ngList your current group name.id -ng

Chown Version

You can run the command below to check the chown command on your system.

chown --version

Check Ownership of a Linux File

Before viewing user and group ownership of a file, run the following command to check the group or ownership of the current Linux files/directories:

ls -l

Change the Owner of a Linux File

First, look at the basic syntax of the target command to change the file ownership:

chown NewUser FILE

So, you can use the command below to change the ownership of a file (for example) sample to a new owner named operavps:

chown operavps sample

Note: You must change the ownership of both files and directories using the same format.

Again, you can use the ls -l command to verify the changes of ownership.

Using UID to Change the Owner of a Linux File

To change the ownership of a file instead of a username, specify a user ID:

chown 1000 sample2

Note: Ensure that no other user exists with the same name as the UID. If so, the username rather than the UID is given priority by the chown command.

Using GID to change the Group of a Linux File

Use a group ID (GID) as with UID to modify a file’s group rather than its group name.

For example:

chown :1000 sample

Edit Linux Group Ownership

You can use the chown command in Linux to edit/change the group. Here is the basic syntax:

chown OWNER[:group] FILE(s)

For instance, use the following command to change the owner and group of the same Sample file to newowner and newownergroup, as follows:

chown newowner:newownergroup Sample

You can ignore the ownership part if you only consider changing the group. So, the required command to change only the group of the newownergroup would be like the below:

chown :newownergroup Sample

Change Ownership of Multiple Linux Files

To use the chown command in Linux to change the ownership of multiple files, you need to list the target file names following the new user. For example, to choose root as the new owner of sample2 and sample3 files, run:

chown root sample2 sample3

To change the ownership of files name and directories, type:

chown root sample3 Dir1

Change Linux Directory Ownership

It is possible to use the chown command in Linux for directories. So far, you learned to use the ls -l command to view your considered directory permissions. You will see the part represents the user, group, and other folder permissions, and the directory owner in the output.

Then, you are ready to change the owner and group of the directory. For example, to set the user newowner as the owner of the Dir1Permissions, run:

chown newowner /Dir1Permissions

Use the command below to change the group alone:

chown :newownergroup /Dir1Permissions

Run the command below to change the owner and group of the directory:

chown newowner:newownergroup /Dir1Permissions

Then, you can set the permissions for multiple files or directories. The basic syntax is:

chown [OPTIONS] [OWNER][:GROUP] file1 file2

Generally, the below command is the way you can set newowner as the owner and newownergroup as the group for the Dir1Permissions directory along with the Sample file inside:

chown newowner:newownergroup /home/Dir1Permissions/ /home/Dir1Permissions/Sample

 Change the Group of a Linux File

To change a group for a file or directory, you can use the chown command in Linux. In this way, you will do this without changing the owning user.

chown :NewGroup FILE

Change Owner and the Linux Group

You can also use the chown command in Linux to assign a new owner of a file and change its group at the same time. Simply run:

chown NewUser:NewGroup FILE

For example, when you want to set linuxuser as the new owner and group 2 as the new group of the file sample2, run:

chown linuxuser:group3 sample3

Use Recursive Chown in Linux

To Recursively change file ownership, you can use the chown command in Linux. In this way, you can change the permissions for all the sub-directories and files inside a specified directory. All you need is to add the -R option to the command:

chown -R [USER][:GROUP] Directory

To check it, take the same Dir1Permissions directory and set newowner as its owner recursively:

chown -R newowner /home/Dir1Permissions/

The user newowner will now be the owner of every file and folder in the Dir1Permissions directory.

Transfer Ownership and Group Settings Between Files

You can use the chown command in Linux to use the owner and a group of reference files instead of changing the ownership to a specific user. Adding the –reference option to the chown command copies the settings from one file to another:

chown --reference=ReferenceFILE FILE

Edit the Owner of a Linux Symbolic Link

A symbolic link or Symlink refers to a real file that already exists. It may also be regarded as an advanced shortcut similar to those found on Windows. you can use the chown command in Linux to change the owner and group of symbolic links. For example, make a symbolic link to the chownSample file.

ln -s chownSample mysymlink

Use the ls -l command, to verify the ownership and group information. One entry corresponds to the physical file, and the other to the symbolic link.

To change the owner to newowner for the newly created symlink and change the ownership for the file chownSample, run:

chown newowner mysymlink

Also, you can change the symbolic link’s ownership by running the following command:

chown -h newowner mysymlink

Check Owner and Group Before Apply Changes

You can use the chown command in Linux to check the owner and group before making changes. The basic syntax to check both the user and group is as below:

chown --from=CurrentUser:CurrentGroup NewUser:NewGroup FILE

How to Check Owner Only

To only verify the current user of a file, use the –from option:

chown --from=CurrentUser NewUser FILE

How to Check Group Only

Similar to the section above, you can use the option –from to validate only a file’s group.

chown --from=:CurrentGroup :NewGroup FILE

Viewing the Details of Chown Command Process

Since the terminal does not display the chown process information by default, use the chown command in Linux with the below-explained flags to display chown command process details.

  1. The option –v even when the ownership is unchanged, generates the process details.
  2. The option –c only when the target file’s owner or group changes does the output information be shown.

Eliminate Chown Command Errors

You can use the chown command in Linux to prevent possible error messages from appearing while using the chown command. It is done by using -f option.

chown -f NewUser FILE

You’re all set. You know everything about the chown command in Linux.

FAQ

:GROUP- The user must be omitted and the group be prefixed with a colon :.

You might be using a normal user. Normal users can only alter the group of a file to a group they are a member of and only if they own the file. All files have group ownership that can be changed by administrative users.

 

Linux system divides into three parts: User, Groups, and other permissions.

A group's members all have the same permissions. A user will have the same capabilities as the group owner, for instance, if they are included to the administrator group. It's important to remember that each user must belong to a group, and that groups may contain numerous users. Any ownership changes made by a group user will be reflected for all users.

‘’User’’, ‘’Group’’, and ‘’Others’’ groups have their permissions indicated by the rwx logic. r stands for read, w for write, and x for execute.

Conclusion

In this article, you learned How to Use Chown Command in Linux to Change the Ownership of Files. Undoubtedly, you must exercise extreme caution when modifying a file’s group or directory ownership. From now on, you will be able to manage the ownership of your Linux files or directories. Using the chown command in Linux, you can assign different owners and specify the group for various files/directories.

If you know more commands to use Chown command in Linux, then the comment section is all yours.

Leave a Reply

Your email address will not be published. Required fields are marked.