Ubuntu

How do you Change the Ownership of a File in Linux/Ubuntu?

How to Change File Permissions and Ownership in Linux Ubuntu

Linux Distros can have multiple users and each user can have their own permissions for accessing specific files. The files having different users and Ubuntu having multiple users as well reduce the risk of damaging or crashing the system. Having different users for specific files reduces the security risks, improves the privacy of the Ubuntu system, and is useful for resource sharing.

This article discusses the use of changing the ownership of files in Ubuntu.

How do you Change the Ownership of a File in Linux/Ubuntu?

The “chown” command changes the owner of a file or folder in Ubuntu. The syntax to use the “chown” command is:

chown <newUserName> <FileName>

The file name should be complete, i.e. the name string and the extension. Considering the file “fileA.txt” in our case:

To see the current owner of the file, use the command:

getfacl <fileName>

In our case, the file name is “fileA.txt”:

getfacl fileA.txt

This will display the owner’s details in the terminal:

Before changing the ownership of the file, ensure the user is already added to your system. To list all the users use the command:

getent passwd | awk -F: '{print $1}'

The “awk” file filters the search. If you don’t know how it works, check out List Users with awk Command to learn more about “awk”.

The “getent passwd” will list all the current users:

In our case, scrolling down, we have the “bob” user as well:

Now to change the owner of the file, we will use the “chown” command:

sudo chown bob fileA.txt

This will change the ownership of “fileA.txt” from “taha” to “bob”:

To verify the change in ownership, use the “getfacl” command again with “fileA.txt”:

getfacl fileA.txt

The owner of “fileA.txt” will be “bob”:

Change the Ownership of Multiple Files in Linux/Ubuntu

The Owners of Multiple files can be changed using the syntax:

chown <newUserName> <fileA><fileB>....<fileN>

In our case, we will change the owner of the file “abc.txt”, “cde.txt”, and “fileH.txt” to “bob”. The current ownership of these files can be found using the “getfacl” command:

getfacl abc.txt cde.txt fileH.txt

The current owner is “taha”:

Now to change the ownership to “bob”, the “chown” command will be used again with multiple files:

sudo chown bob abc.txt cde.txt fileH.txt

The file ownership will be changed to “bob”:

The new ownership can be verified using the “getfacl” command again:

Change the Group of Files in Linux/Ubuntu

Apart from changing the ownership of a file, the Group of a file can also be changed. The syntax to change the group of a file is:

chown :<newGroupName> <fileName>

In our case, we have a file “fileL.txt”. The group of the file can be checked using the same “getfacl” command:

getfacl fileL.txt

The group of “fileL.txt” is “taha” as seen below:

Now to change the group of “fileL.txt”, the “chown” command will be used:

sudo chown :genz fileL.txt

The group of “fileL.txt” will change to “genz”:

Change the Group and Ownership of Files at the Same Instant in Linux/Ubuntu

To change the Group and the Owner of a file at the same time, use the new owner name with the Colon of the new Group:

chmod <newUserName>:<newGroupName> <fileName>

In our case, the “fileJ.txt” has the owner and group “taha”:

To change the Ownership to “bob” and the group to “genz”, use the command:

sudo chown bob:genz fileJ.txt

The ownership and group of “fileJ.txt” will change:

The owner and group changes can be verified using the “getfacl” command again:

Change the Ownership of Files in Sub Directory in Linux/Ubuntu

The ownership of files inside a directory and sub-directory can also be changed. Considering our previous example, we have a Documents Directory and inside the Documents Directory, multiple sub-directories exist:

To change the ownership of all the files of Documents and files inside the sub-directories, use the command:

sudo chown -R bob Documents/

The ownership of all files inside the Documents Directory and all the files in the Sub-directory will change to “bob”:

The changed ownership can be verified using the below command:

sudo ls -lR Documents/

This will display the owner of all files and files inside the sub-directories as well. In our case, the ownership changed to “bob”:

Change the Ownership of a File according to a Reference in Linux/Ubuntu

The ownership of a file can also be changed according to a reference to a specific file. The file that needs a new owner will have the owner of that referenced file. In our case, the file “fileA.txt” has the owner “taha” and can be checked using the “getfacl” command:

The file “fileB.txt” has the owner “bob” and can be verified using the “getfacl” command:

Now to change the ownership of the file “fileB.txt” to the owner of “fileA.txt” the command used will be:

sudo chown --reference=fileA.txt fileB.txt

This will make the owner of “fileB.txt” the exact same as “fileA.txt”:

To verify, the “getfacl” command can be used:

getfacl fileB.txt

As you can see the owner of the “fileB.txt” is the same as the owner of “fileA.txt”:

Conclusion

Multiple ways and methodologies can be used to change the ownership of files in Ubuntu. The “chown” is a useful command to change the ownership of files and can be used to change the owner of either a single file or multiple files inside a directory and a sub-directory as well. In this article, we discussed multiple methods of changing the owner of files using the “chown” command.

Similar Posts