OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

chown – Change File Owner And Group

The chown command is used to change the owner and group owner of a file or directory. Superuser privileges are required to use this command. The syntax of chown looks like this:



chown [owner][:[group]] file...

chown [owner][:[group]] file...


chown can change the file owner and/or the file group owner depending on the first ar- gument of the command. Here are some examples:


Table 9-7: chown Argument Examples


Argument Results

Argument Results

bob Changes the ownership of the file from its current owner to user

bob.


image

bob:users Changes the ownership of the file from its current owner to user

bob and changes the file group owner to group users.


image

:admins Changes the group owner to the group admins. The file owner is unchanged.


image

bob: Change the file owner from the current owner to user bob and changes the group owner to the login group of user bob.


image


Let's say that we have two users; janet, who has access to superuser privileges and tony, who does not. User janet wants to copy a file from her home directory to the home directory of user tony. Since user janet wants tony to be able to edit the file, janet changes the ownership of the copied file from janet to tony:


[janet@linuxbox ~]$ sudo cp myfile.txt ~tony

Password:

[janet@linuxbox ~]$ sudo ls -l ~tony/myfile.txt

-rw-r--r-- 1 root root root 2016-03-20 14:30 /home/tony/myfile.txt [janet@linuxbox ~]$ sudo chown tony: ~tony/myfile.txt

[janet@linuxbox ~]$ sudo ls -l ~tony/myfile.txt

-rw-r--r-- 1 tony tony tony 2016-03-20 14:30 /home/tony/myfile.txt

[janet@linuxbox ~]$ sudo cp myfile.txt ~tony

Password:

[janet@linuxbox ~]$ sudo ls -l ~tony/myfile.txt

-rw-r--r-- 1 root root root 2016-03-20 14:30 /home/tony/myfile.txt [janet@linuxbox ~]$ sudo chown tony: ~tony/myfile.txt

[janet@linuxbox ~]$ sudo ls -l ~tony/myfile.txt

-rw-r--r-- 1 tony tony tony 2016-03-20 14:30 /home/tony/myfile.txt


Here we see user janet copy the file from her directory to the home directory of user tony. Next, janet changes the ownership of the file from root (a result of using sudo) to tony. Using the trailing colon in the first argument, janet also changed the group ownership of the file to the login group of tony, which happens to be group tony.

Notice that after the first use of sudo, janet was not prompted for her password. This is because sudo, in most configurations, “trusts” you for several minutes until its timer runs out.


Top OS Cloud Computing at OnWorks: