In Linux, all files are associated with an owner and a group owner and assigned with permissions for 1
the file owner 2
the group members and the others. In this lecture, I'll show you how to change the user and the group 3
ownership of files and directories using the change owner and the change group commands. 4
Before going on, you should know that only root can change the file owner, so change owner or c h 5
o w n is a root only command 6
And in regards to changing the group, normal users can change the group of a file only if they own 7
that file, and only to a group of which they are a member of. Root can access the group ownership 8
of all files. 9
To see the owner and the group of a file or a directory, you normally run ls-l 10
command; let's practice the change owner command. 11
To change the owner of a file or a directory run the chown command followed by the user name 12
of the new owner and the target file or directory as an argument: 13
Let's create a file as an non unprivileged user and then change the ownership. 14
This command will list information about the scpu and I am redirecting everything to a file. 15
This is the owner and this is the group owner; if I try to change the owner of the file as a non privileged 16
user 17
I'll get an error 18
even though I'm the current owner; it's not possible to disown a file. 19
Let's find the user that we have created in the last sections. 20
There's a user called toor. 21
You can use any other user that exists on your system, so chown tdoor and the file. 22
And I've got an operation not permitted error, and as root, I will change the owner to another user. 23
I'll run the same commands as root by prefacing it with sudo. 24
And the owner was changed. 25
Remember that it's not mandatory for the owner of the file to be part of the group,owner of the same 26
file. In this example, toor does not belong to the student group. 27
It belongs only to two groups toor and sudo; if you want to change the owner of more than one file, just 28
specify the files as arguments to the chown command like this: 29
sudo chown the new owner toor and the files and directories. 30
cpu.txt interfaces.txt,i.txt and the directory Linux. 31
And they've changed the owner of all of those files and directories. 32
Also, the numeric user id can be used instead of the username. 33
Let's see a user ID. 34
For example, the user called the u1 has the user ID 1005 The following example 35
will change the ownership of the file to a new owner with the user ID 1005. 36
So sudo chown the user ID 1005 37
and the file. 38
Now the user with that ID is the owner. 39
If a user id exists having the same value as a username (in this example suppose there’s also 40
a username called 1005), then the ownership will be transferred to the username. To avoid 41
this confusion, prefix the user ID with a + sign like this. 42
This is much better. 43
It had the same effect. 44
The change owner command can be used to change both the owner and the group of a file ar the directory. 45
Just use a colon between the new owner and the group of the file, sudo chown , student : 46
daeman 47
cpu.txt Both the owner and the group should already exist. On some Linux distributions, you can also 48
use the dot symbol instead of the colon symbol to separate the owner from the group. 49
Like this! 50
To change only the group ownership of a file or a directory, invoke the change group command or 51
chgrp command followed by the new group name and then the target file or directory 52
as argument.For example I’m only changing the group of the cpu.txt file to sudo group: 53
sudo chgrp 54
the new group, sudo, and the file; the new group of the file is sudo. 55
You can get the same result by running the chown command followed by a colon and then the 56
new group with no space between them: 57
sudo chown : the new group and the file. 58
This command will have the same effect is the last one. 59
To recursively operate on all files and directories under a given directory use the capital R option. I'm changing 60
both the owner and the group of all the files and directories in my home directory to my current user, 61
its primary group. 62
sudo chown -R student:student and the diretory 63
My home directory.