From its core, Linux is built to have multiple users. Be it a root user and a normal user or a bunch of human users.
But what if you want to access files created by other uses? Well, here's what it looks when you try accessing a file owned by another user:
While you can use the ls command to check file permissions, I will be using the
getfacl utility as it presents the output in a better way compared to the others:
And in this guide, I'll be showing you how to change ownership of files so no one can access them unless it is you!
Change file owner using the chown command
This guide will utilize the chown command as it is intended to deal with file and group ownership. So let's have a look at the syntax first:
chown [OPTIONS] USER[:GROUP] file
[OPTIONS]allows you to tweak the default behavior of chown.
[USER]is where you'll specify the new owner using username or user-id.
[:GROUP]is optional and should only be used when you want to change the group ownership of a file.
fileis where you have to a filename of which you want to change ownership.
So now, let's have a look at how you can change file ownership.
1. Change file ownership
To shift ownership of a file to a new user, you just have to append the filename with the new user:
sudo chown NewOwner File
For example, I'll be changing ownership of
Music.mp3 to the user named
sudo chown milan Music.mp3
As you can clearly see, the owner was changed to
milan after executing shown command.
2. Change ownership of multiple files and directories
To change ownership of multiple files, you just have to chain the filenames with a new owner:
sudo chown NewOwner File1 File2 File3
Similarly, you can combine the directory names with filenames and change their owner at once:
sudo chown NewOwner Dir1 File1 Dir2 File2
3. Change the group of file
As you can clearly see, the group users have their own privileges, and if you want to change the group of files, specify the group name with a colon (:) as shown:
sudo chown :NewGroup File
For example, I will be changing the group of a file named
sudo chown :LU Music.mp3
4. Change file ownership and group at once
To change the file owner and file group, you have to append the group name using a colon (:) to the new user:
sudo chown NewOwner:NewGroup File
For example, I will be changing the owner to
milan and the file group to
LU of the
sudo chown milan:LU Music.mp3
5. Change file ownership and group recursively
To change the file owner and group recursively, you'd need to utilize the
-R option and append the directory or path of the directory and the changes will be applied to every file and subdirectory.
To change only the file ownership recursively:
sudo chown -R NewOwner NameOfDirectory
Let's say I want to change the owner of every file present inside the directory name
Directory, so I'd have to follow the given command:
sudo chown -R milan Directory
Similarly, you can change the ownership and group at once by following the given command syntax:
sudo chown -R NewOwner:NewGroup DirectoryName
For example, I will be changing ownership to a user named
milan and change the group to
LU to every file present inside the
sudo chown -R milan:LU Directory
6. Change file ownership and group with the help of reference file
Let's say you have one file with desired owner and group already configured and now you want to apply those same changes to other files.
This can be done by appending the reference file to the
sudo chown --reference=Reference_File TargetedFile
I have a reference file named
PerfectFile.txt and I want to replicate the owner and group to the file named
sudo chown --reference=PerfectFile.txt Music.mp3
This was my take on how you can change ownership of files and groups with different options involved. And if you have any queries, feel free to ask in the comments.