What is `chown` command used for?

QuestionsQuestions8 SkillsProDec, 20 2025
0150

The chown command stands for "change owner." While chmod changes what can be done (permissions), chown changes who owns the file or directory.

In Linux, every file and directory is owned by a specific User and belongs to a specific Group.

1. Basic Syntax

The basic syntax for chown is:

sudo chown [user]:[group] [file_or_directory]

(Note: Since changing ownership is a sensitive tasks, you almost always need to use sudo.)

2. Common Use Cases

  • Change the owner only:
    sudo chown newuser file.txt
  • Change both the owner and the group:
    sudo chown labex:developers project_folder
  • Change the group only (though the command chgrp also exists, chown is often used like this):
    sudo chown :newgroup file.txt

3. Recursive Change (The -R flag)

If you have a folder full of files and you want to change the owner of everything inside at once, use the -R (recursive) flag:

sudo chown -R labex:labex /home/labex/project/my_work

4. Why would you need chown?

In this lab environment, you'll mainly use it if:

  1. You used sudo to create a file: Sometimes, if you create a file using sudo, it becomes owned by the root user. You might find you can't edit it normally later. You would use chown to give it back to the labex user.
  2. Moving files: If you copy files from another user's directory, ownership labels might need updating so you can work on them.

Summary: The Difference between chmod and chown

  • chmod: "I want to make this file readable/writable." (Changing Mode)
  • chown: "I want this file to belong to a different person or group." (Changing Owner)

Would you like to try changing the owner of a test file to see how it affects your ability to edit it?

0 Comments

no data
Be the first to share your comment!