chown Command in Linux: Changing Owner and Group of Files
The chown command in Linux changes the file owner and can also change the group the file belongs to.
In most permission problems, novice Linux users tend to change file permissions with the chmod command. But usually, changing the ownership of the parent directory or a file is sufficient. So pay attention to this post.
The chown command allows the following options:
- -v: Reports the owner of the files, including ownership changes;
- -c: Reports only the ownership change;
- -R: Changes the owner of all files and directories recursively within the same hierarchy.
The chown command can receive as a parameter:
Login
If only the user's login is provided, the file Owner property changes, but the group property remains unchanged.
Login:group
If a "login:group" or "login.group" is provided, both the file owner and the group are changed.
Login:
If a "login:" or "login." is provided without specifying the group, the file owner is changed, and the group is changed to the group the specified login belongs to.
:group
If ":group" or ".group" is provided without specifying the login, chown will act like the chgrp command, changing only the file's group.
Examples:
Changes the owner of the file Readme.txt to user uira:
$ chown uira Leiame.txt
Changes the owner of the file Readme.txt to user uira and the file's group to the user's group:
$ chown uira. Leiame.txt
Changes the owner of the file Readme.txt to user uira and the group to users:
$ chown uira:users Leiame.txt
Changes the file's group to users and leaves the file's owner unchanged:
$ chown .users Leiame.txt
Changes the owner of all files and directories within /home/uira to user uira:
$ chown –R uira /home/uira
A regular user can only transfer ownership of files and directories they own. The root user can change the ownership of any file or directory.
Most of the time, it is not necessary to change a directory's permissions to "777" as a way to grant permissions for a program 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 the group.
See PHP for example:
$ ps aux | grep phpapache 1232 0.1 2.6 533340 208332 ? S mai25 3:12 php-fpm: pool wwwapache 1233 0.1 2.6 773088 205856 ? Sl mai25 3:57 php-fpm: pool www
In this example, the user running php-fpm is apache.
So, to give permission for php to write something to the upload directory, it is not necessary to change the permission, but rather the directory's owner:
$ ls -l |grep uploaddrwxr-xr-x 2 root root 4096 mai 25 22:33 upload
You then change the owner of the upload directory to user apache. This way, the php-fpm process will be able to write, read, and access the upload directory:
$ sudo chown apache upload$ ls -l |grep uploaddrwxr-xr-x 2 apache root 4096 mai 25 22:33 upload
If you are an IT professional, support or developer, have you thought about learning Linux for real?
And even getting an LPI Linux Essentials certification on the side?
And even better, with the best Online course for Linux?
[RICH_REVIEWS_SHOW category="CURSO" num="all"]
Do like these people and create your account now.
Here you have everything you need to succeed in LPI and CompTIA exams
- Practical Shell Lessons
- Flashcards for memorization
- Crossword word games
- 06 virtual servers on Amazon just for you
- Mind Maps
- Video Glossary
- Practice exam for the tests
- Custom class schedule
- Guided exercises – Real labs
- Class resources (notes and more)
Want to know if a certification is worth it? Learn why the salaries are good, and why companies are looking for Linux professionals.
You will understand that the benefits far outweigh the sacrifice.
And if you have already decided, check out our Certification Guide.
And if you want to know where to take the exams, check the city list.
If you want to know the exam language, see this post.
Learn much more about Linux in our online course. You can enroll here. If you already have an account, you can access here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.