Blog Post
How to Use Linux Chown Command Properly

How to Use Linux Chown Command Properly

It is not easy to be a Linux newbie who tries to find their way around this amazing operating system. Fortunately, you can find many tutorials that make utilizing terminal and typing commands a piece of cake. The chown is one of those commands that can make your life easier. It allows you to adjust the file and folder ownership, and we will discover how you can do that in this file.

Basic Syntax

Here is how the chown command should look like:

chown [option] UID:GID filename

Now, let’s analyze the above line:

  • Option – you can set different parameters to get additional control of the command.
  • UID – here you type the ID of the user. You can also use the actual name of the user.
  • GID – specify the ID of the group that you are changing the ownership to in this section. You can also use the actual group’s name.
  • Filename – which file or folder’s ownership you want to change?

Keep in mind that you should use the “chown” command as the root user or with an account that has superuser permission.

How to See Who Is the Current Owner of the File?

Before you get to change the ownership, you may want to find out who is the file or folder’s current owner. You can do that by typing the following line:

ls -l filename

While this doesn’t have a direct connection with the chown command, it will secure proper information before using it.

Adjust File or Folder Ownership

Here is an easy command to adjust the ownership of a file:

chown username filename

Instead of the “username”, you should enter the desired file owner, and the “filename” should specify the path to the file.  You can use the same approach if you want to adjust the ownership of a directory.

You also have the option to mix and match by adjusting the ownership of multiple files to a particular user.

chown username filename1 folder_name1 filename2

Please note that you have the option of using the actual username to enter the desired new file owner, but you can also use UID (user ID). For the change to be successful, you need to ensure that a user with the specified username exists.

How to Adjust the Owner and Group by Using Chown Command

You can adjust both the file’s new owner and group in a single command. Here is how the line should look:

chown username:group_name filename

Instead of the actual user and group name, you are welcome to use UIDs and GIDs.

You can also use the following command:

chown username: filename

The above line will adjust both the user and group ownership of the specified filename. The group ownership will be set to the group that the username belongs to on the system.

How to Adjust the File or Folder Group on Linux

You may only want to change the group ownership of a particular file or directory. Here is how you can do that:

chown :group_name filename

Make sure to enter a valid group name or GID. Also, please note that if you want to find files or directories you can use the find command available on all UNIX based systems.

How to Adjust the File Ownership Recursively

If you want to issue a command and tell the system to adjust the ownership of files and folders recursively, here is the right line to use:

chown -R username:group_name folder_name

Using the above line will adjust that all filed and folders in the “folder_name” have their file ownership changed to the specified user and group name. The system will perform this command recursively.

Wrap Up

Chown command is one of those that allows you to take total control over your files and directories on Linux. For more information, you can use the “chown man” command and the system will display an overview of your potential options.

Related posts

Leave a Reply

Required fields are marked *

Copyright © 2022 All rights reserved.