Changing File Ownership In Linux

Changing File Ownership In Linux

Subscribe to my newsletter and never miss my upcoming articles

Initially, the owner of a file is the user who creates it. The chown command is used to change the ownership of files and directories. In order to change the ownership you would require administrative access. A regular user cannot use this command to change the ownership of a file, or even to give the ownership of one of their own files to another user.
The chown command does not only allow you to change user but also group ownership.

The syntax

chown [OPTIONS] [OWNER] FILE

Remember our previous examples, we are going to work with them here too:
lets say we had something like this

admin@localhost:~$ cd ~/Documents

Currently all the files in the Documents directory are owned by the admin user. Have a doubt? lets verify that. If you remember the ls -l if you run this it will indicate user owner of each file, have a look

admin@localhost:~/Documents$ ls -l                                           
total 5                                                                       
drwx------ 5 admin   admin  4096 Dec 20  2017 School                        
drwx------ 2 admin   admin  4096 Dec 20  2017 Work                          
-rw-r--r-- 1   admin   admin    39 Dec 20  2017 adjectives.txt                     
-rwxr--r-- 1   admin   admin   647 Dec 20  2017 hello.sh                      
-rw-r--r-- 1   admin    admin    67 Dec 20  2017 hidden.txt

Now if we wanted to switch the owner of hello.sh to the root, we will have to use root as the first argument and hello.sh as second argument, and remember to use sudo command in order to gain the necessary administrative privileges. After running this you will have to enter your password and that's all.

admin@localhost:~/Documents$ sudo chown root hello.sh                        
[sudo] password for admin:

If You have read this far I really appreciate, Help me to grow my community:

Connect With me at Twitter | Insta | YouTube | LinkedIn | GitHub

Do share your valuable opinion, I appreciate your honest feedback!

 
Share this