File (directory) ownership is the foundations of every operating system. This post will teach you how to change file(s) ownership in Linux using the command chown.
There are different scenarios when you would need to change the ownership of a file using either chown or chgrp. Note: you can change group ownership with the command chown. I’ll show you how to do this later in this article.

There is another command to change permissions in Linux chmod. Note that only the current owner of the file and the root user can do this task.

Although neither of the commands are difficult to use, modifying the file ownership with chmod is beyond the scope of this post. It belongs to another article.

Change the file(s) or directory ownership to suit your needs by following roughly the instructions below.

Figure Out Who’s The Owner Of The File(s)

ls -l myfile
ls -l option uses a long listing format. With this option ls returns file ownership together with permissions. Here you would probably want to decide how to alter the file or directory ownership.

Using Chown To Change Ownership

The simplest usage of using the command and setting the ownership to the user myuser is:
chown myuser myfile
ls -l
-rw-r--r-- 1 myuser root 28 june 25 2014 myfile

where the file myfile is owned now by myuser. If you want to change the group from root to mygroup, do it by slightly modifying the code:

chown myuser:mygroup myfile

In real life, we usually have to change ownership of an entire directory tree. You would do this by adding -R option which stands for recursive operation. With this option the command goes thru all files and directories within the directory in changes the ownership.
chown myuser:mygroup /home/myuser -R
Takes everything in myuser home directory back.

Leave a Reply

Your email address will not be published.

Related Posts

Linux System Administration

How to change ssh port

Advantages of Changing SSH Port First, I want to explain the reasons behind changing the ssh port of a server. Why changing ssh port when you have a strong password and/or a certificate? Additionally, it Read more…

Linux System Administration

SSH Hardening Techniques

SSH Hardening Techniques These are the top practices to harden ssh. In this article I am going to list the best and most important open ssh server security practices. SSH protocol is the best option Read more…

Linux System Administration

How To Use Public Key Authentication Instead Of Password

Password Login Belongs To The Past Public key authentication is an easier and more reliable compared to the old- fashioned password-based login. Keys are basically less prone to brute-force attacks. If you want to force Read more…