Chown command on Linux (change property) [Basic Guide]

The chown command in Linux changes the owner of the file and can also change the group to which this file belongs.

For most permission issues, users who are new to Linux tend to change file permissions with the command Chmod. But generally, changing ownership of the parent directory or of a file is sufficient. Then link to this post.

The chown command allows the following options:

  • -v: Reports the owner of the files, including changes of owner;
  • -c: Reports only the change of owner;
  • -R: Change the owner of all files and directories recursively within the same hierarchy.

The chown command can take as a parameter:

Login
If only the user login is entered, the ownership of the File Owner changes, but the group ownership remains unchanged.
Login:group
If a “login: group” or “login” is entered. group”, both the ownership of the file owner and the group are changed.
Login:
If a “login:” or “login” is entered. “without informing the group, the owner of the file is changed, and the group is changed to the group in which the specified login is part.
:group
If “: group” or “is entered. group” without entering the login, chown will act like the chgrp command, changing only the file group.

Examples:

Changes the owner of the Leiame.txt file for the uira user:

$ chown uira Leiame.txt

Changes the owner of the Leiame.txt file for the user uira and the file group for the user group:

$ chown uira. 

Leiame.txt

Changes the owner of the Leiame.txt file for the user uira and the group for users:

$ chown uira: users Leiame.txt

Changes the file group to users and leaves the file owner unchanged:

$ chown. Users Leiame.txt

Changes the owner of all files and directories inside /home/uira to the user uira:

$ chown —R uira /home/uira

An ordinary user can only pass ownership of files and directories that they own. The root user can change ownership of any file or directory.

Most of the time, it is not necessary to change the permissions of a directory to “777″ as a way of granting permissions for a program to be able to access it. Just check with the “ps -aux” command which user is running the program, and change the owner of the directory or file, or of the group.

Take PHP for example:

$ ps aux | grep php 
Apache 1232 0.1 2.6 533340 208332? And mai25 3:12 php-fpm: pool www
apache 1233 0.1 2.6 773088 205856? On May 25 3:57 php-fpm: pool www

In this example, the user running php-fpm is Apache.

So, to allow php to write something to the upload directory, it is not necessary to change the permission, but the owner of the directory:

$ ls -l |grep upload drwxr-xr-x 2 root root 4096 May 25 22:33 upload

The owner of the upload directory is then changed to the Apache user. This way, the php-fpm process will be able to write, read, and access the upload directory:

$ sudo chown Apache upload $ ls -l |grep upload drwxr-xr-x 2 Apache root 4096 May 25 22:33 upload

Are you an IT professional, support professional, or developer, have you ever considered learning Linux for real?

Is

it still bad to get an LPI Linux Essentials certification from breaking?

And even better, with the best online course for Linux?

[RICH_REVIEWS_SHOW CATEGORY=”Course” num=”all “]

Do like these people and create your account now.

Here you have everything you need to succeed in the LPI and CompTIA exams

  • Practical Classes at Shell
  • Flashcards for memorization
  • Crossword Games
  • 06 virtual servers on Amazon just for you
  • Mind Maps
  • Video Glossary
  • Preparatory simulation for the exams
  • Personalized class schedule
  • Guided exercises — Real Laboratories
  • Lesson Resources (notes and others)

Do you want to know if a certification is worthwhile? Learn why salaries are good, and why companies are looking for Linux professionals.

You’ll understand that the benefits far outweigh the sacrifice.

And if you’ve made up your mind, check out our Certification Guide.

And if you want to know where to take the tests, check the list of cities.

If you want to know the language of the exams, check out this post.

Learn much more about Linux in our online course. You can register here. If you already have an account, you can log in here.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Leave a Reply 0

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


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need help?