Changing File Ownership (chown)
It should be noted that you can’t change ownership of a file or directory that belongs to another user unless you are root. When logged in as yourself, and you are a member of “wheel” or are able to “sudo” or “su –“ for root access, you can make changes using the chown command. In the examples below the user is executing the command as “root”, designated by the “#” prompt.
Let’s say you created a file called “test.txt” as root in Sammy’s home directory.
$ su –
Password: xxxxxxxx (actual root password – or you use “sudo” with your password)
# pwd (shows your location)
# touch test.txt ( the “#” prompt indicates you are root)
# ls –al test.txt
-rw – r – – r – – root root 0 2013-08-04 16:12 test.txt
So the file you created here belongs to user root, group root. To change this file to be owned by Sammy:
# chown sammy /home/sammy/test.txt
# ls –l /home/sammy/test.txt
-rw – r – – r – – sammy root 0 2013-08-04 16:12 test.txt
Now the file is owned by Sammy but the group is still root. Ideally you would want to change both the user and group to sammy. To do so you would issue the following command:
# chown sammy:sammy /home/sammy/test.txt
# ls –l /home/sammy
-rw – r – – r – – sammy sammy 0 2013-08-04 16:12 test.txt
You can also use “chown” recursively as well. To change the directory test to belong to Sammy you would issue the following command:
# chown –R sammy:sammy /test
# ls -l
drwxr – xr – x sammy sammy 4096 2013-08-04 16:28 test
This command will make the directory test and everything below it belong to Sammy.