The chown
command means that you can change the consumer and/or group possession of a given file, listing, or symbolic hyperlink.
In Linux, all recordsdata are related to an proprietor and a gaggle and assigned with permission entry rights for the file proprietor, the group members, and others.
On this tutorial, we’ll present you the right way to use the chown
command by sensible examples.
The right way to Use chown
Earlier than going into the right way to use the chown
command, let’s begin by reviewing the essential syntax.
The chown
command expressions takes the next kind:
chown [OPTIONS] USER[:GROUP] FILE(s)
USER
is the consumer title or the consumer ID (UID) of the brand new proprietor. GROUP
is the title of the brand new group or the group ID (GID). FILE(s)
is the title of a number of recordsdata, directories or hyperlinks. Numeric IDs needs to be prefixed with the +
image.
USER
– If solely the consumer is specified, the required consumer will change into the proprietor of the given recordsdata, the group possession isn’t modified.USER:
– When the username is adopted by a colon:
, and the group title isn’t given, the consumer will change into the proprietor of the recordsdata, and the recordsdata group possession is modified to consumer’s login group.USER:GROUP
– If each the consumer and the group are specified (with no area betwen them), the consumer possession of the recordsdata is modified to the given consumer and the group possession is modified to the given group.:GROUP
– If the Person is omitted and the group is prefixed with a colon:
, solely the group possession of the recordsdata is modified to the given group.:
If solely a colon:
is given, with out specifying the consumer and the group, no change is made.
By default, on success, chown
doesn’t produce any output and returns zero.
Use the ls -l
command to seek out out who owns a file or what group the file belongs to:
$ ls -l filename.txt
Output:
-rw-r--r-- 12 linuxize customers 12.0K Apr 8 20:51 filename.txt
|[-][-][-]- [------] [---]
| |
| +-----------> Group
+-------------------> Proprietor
Regular customers can change the group of the file provided that they personal the file and solely to a gaggle of which they’re a member. Administrative customers can change the group possession of all recordsdata.
The right way to Change the Proprietor of a File
To alter the proprietor of a file use the chown
command adopted by the consumer title of the brand new proprietor and the goal file as an argument:
chown USER FILE
For instance, the next command will change the possession of a file named file1
to a brand new proprietor named linuxize
:
$ chown linuxize file1
To alter the possession of a number of recordsdata or directories, specify them as a space-separated listing. The command under modifications the possession of a file named file1
and listing dir1
to a brand new proprietor named linuxize
:
$ chown linuxize file1 dir1
The numeric consumer ID (UID) can be utilized as a substitute of the username. The next instance will change the possession of a file named file2
to a brand new proprietor with UID of 1000
:
$ chown 1000 file2
If a numeric proprietor exists as a consumer title, then the possession can be transferred to the consumer title. To keep away from this prefix the ID with +
:
$ chown 1000 file2
The right way to Change the Proprietor and Group of a File
To alter each the proprietor and the group of a file use the chown
command adopted by the brand new proprietor and group separated by a colon (:
) with no intervening areas and the goal file.
chown USER:GROUP FILE
The next command will change the possession of a file named file1
to a brand new proprietor named linuxize
and group customers
:
$ chown linuxize:customers file1
When you omit the group title after the colon (:
) the group of the file is modified to the required consumer’s login group:
$ chown linuxize: file1
The right way to Change the Group of a File
To alter solely the group of a file use the chown
command adopted by a colon (:
) and the brand new group title (with no area between them) and the goal file as an argument:
chown :GROUP FILE
The next command will change the proudly owning group of a file named file1
to www-data
:
$ chown :www-data file1
One other command that you should utilize to alter the group possession of recordsdata is chgrp
.
The right way to Change Symbolic Hyperlinks Possession
When the recursive possibility isn’t used, chown
command modifications the group possession of the recordsdata to which the symlinks factors, not the symbolic hyperlinks themselves.
For instance, in the event you attempt to change the proprietor and the group of the symbolic hyperlink symlink1
that factors to /var/www/file1
, chown
will change the possession of the file or listing the symlink factors to:
$ chown www-data: symlink1
Chances are high that as a substitute of fixing the goal possession, you’re going to get a “can not dereference ‘symlink1’: Permission denied” error.
The error happens as a result of by default on most Linux distributions symlinks are protected, and you can’t function on track recordsdata. This feature is laid out in /proc/sys/fs/protected_symlinks
. 1
means enabled and 0
disabled. We suggest to not disable the symlink safety.
To alter the group possession of the symlink itself, use the -h
possibility:
$ chown -h www-data symlink1
The right way to Recursively Change the File Possession
To recursively function on all recordsdata and directories beneath the given listing, use the -R
(--recursive
) possibility:
chown -R USER:GROUP DIRECTORY
The next instance will change the possession of all recordsdata and subdirectories beneath the /var/www
listing to a brand new proprietor and group named www-data
:
$ chown -R www-data: /var/www
If the listing incorporates symbolic hyperlinks go the -h
possibility:
$ chown -hR www-data: /var/www
Different choices that can be utilized when recursively altering the listing possession are -H
and -L
.
If the argument handed to chown
command is a symbolic hyperlink that factors to a listing, the -H
possibility will trigger the command to traverse it. -L
tells chown
to traverse every symbolic hyperlink to a listing that’s encountered. Often, you shouldn’t use these choices since you may mess up your system or create a safety threat.
Utilizing a Reference File
The --reference=ref_file
possibility means that you can change the consumer and group possession of given recordsdata to be identical as these of the required reference file (ref_file
). If the reference file is a symbolic hyperlink chown
will use the consumer and group of the goal file.
chown --reference=REF_FILE FILE
For instance, the next command will assign the consumer and group possession of the file1
to file2
$ chown --reference=file1 file2
Conclusion
chown
is a Linux/UNIX command-line utility for altering the file’s consumer and/or group possession.
To be taught extra in regards to the chown
command go to the chown man web page or kind man chown
in your terminal.
When you’ve got any questions or suggestions, be at liberty to depart a remark.
0 Comments