Chown Command in Linux_ How to Change File Ownership
Chown Command in Linux_ How to Change File Ownership
Contents
Introduction
The chown command changes user ownership of a file, directory, or link in Linux. Every file is
associated with an owning user or group. It is critical to configure file and folder permissions
properly.
In this tutorial, learn how to use the Linux chown command with examples provided.
https://phoenixnap.com/kb/linux-chown-command-with-examples 1/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership
Prerequisites
https://phoenixnap.com/kb/linux-chown-command-with-examples 2/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership
Getguide,
In this 15 TB we FREE
tested bandwidth
the command(5 TB in Singapore)
examples with
with the chown Bare
version Metal
8.28 Cloud!
in Ubuntu
18.04.2 LTS. DEPLOY NOW
chown --version
To check the group or ownership of Linux files and directories in the current location, run the
following command:
ls -l
https://phoenixnap.com/kb/linux-chown-command-with-examples 3/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership
The following command changes the ownership of a file sample from root to the user test:
Use the same format to change the ownership for both files and directories.
For example:
https://phoenixnap.com/kb/linux-chown-command-with-examples 4/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership
Getsure
Make 15there
TB FREE bandwidth
is no user (5 TB
with the same in Singapore)
name as the numericwith Bare
UID. If thereMetal Cloud!
is, the chown
command gives priority to the username, not theNOW
DEPLOY UID.
In the following example, root will be the new owner of files sample2 and sample3.
Combine file names and directory names to change their ownership with one command. For
example:
Run the chown command using the colon and a group name:
The following example changes the group of the file sample3 from grouptest to group3.
https://phoenixnap.com/kb/linux-chown-command-with-examples 5/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership
For example:
Therefore, to set linuxuser as the new owner and group2 as the new group of the file sample2:
https://phoenixnap.com/kb/linux-chown-command-with-examples 6/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership
Get 15 that
Remember TB there
FREEare
bandwidth (5 TBorinafter
no spaces before Singapore)
the colon. with Bare Metal Cloud!
DEPLOY NOW
Change Group to a Users Login Group
The chown command assigns the owner’s login group to the file when no group is specified.
To do so, define a new user followed by a colon, space, and the target file:
The following example changes the group ownership to the login group of linuxuser:
Add the --reference option to the chown command to copy the settings from one file to
another:
Remember to type in the names of the files correctly to avoid the error message:
TheGet 15command
chown TB FREE--from
bandwidth (5 TB
option lets you in Singapore)
verify the currentwith
ownerBare Metal
and group Cloud!
and then
apply changes. DEPLOY NOW
The chown syntax for checking both the user and group looks like this:
The example below shows we first verified the ownership and the group of the file sample3:
Then chown changed the owner to linuxuser and the group to group3.
For example:
Here is an example where we verified the current group before changing it:
In the following example, we will recursively change the owner and the group for all files and
directories in Dir1.
The following image shows how symbolic links behave when -h is omitted.
The owner and group of the symbolic link remain intact. Instead, the owner and the group of
the file textfile changed.
To push the changes to the link, run the chown command with the -h flag:
https://phoenixnap.com/kb/linux-chown-command-with-examples 9/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership
In the following example, we changed the owner and group of a symbolic link.
The option –v produces the process details even when the ownership stays the same.
The option –c displays the output information only when an owner or group of the target
file changes.
For example, if we specify the current owner as a new owner of the file:
Switch from -v to -c and there will be no messages in this case. This happens because
there are no owner or group changes.
https://phoenixnap.com/kb/linux-chown-command-with-examples 10/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership
In this example, the output lists all objects affected after running the command.
The example below shows the error message for a non-existent file or directory:
Adding the -f flag suppresses most error messages. However, if you specify an invalid
username, the error message appears:
Goran Jevtic
Conclusion
Goran combines his leadership skills and passion for research, writing, and technology as a
Technical Writing Team Lead at phoenixNAP. Working with multiple departments and on
Now youprojects,
various know how to use
he has chown command
developed in Linuxunderstanding
an extraordinary to change a file’s user and/or
of cloud group
and virtualization
ownership. trends and best practices.
technology
Take extra caution when changing the group or ownership of a file or directories.
Get 15 TBSecurity,
Networking, FREE bandwidth (5 TB in Singapore) with Bare Metal Cloud!
Web Servers DEPLOY NOW
17 Nmap
Commands with
Examples for
Linux Network &
System
Administrators
May 14, 2019
SysAdmin
How to Use The
APT-GET
Command In
Linux
May 6, 2019
Advanced Package
Tool (APT) is a
package management
system used on
Debian, Ubuntu and
other Linux ...
READ MORE
SysAdmin, Web
Servers
https://phoenixnap.com/kb/linux-chown-command-with-examples 12/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership
Servers
Get 15
How TB FREE
to List Usersbandwidth (5 TB in Singapore) with Bare Metal Cloud!
DEPLOY NOW
in Linux, List all
Users Command
April 16, 2019
Linux OS is unique
because of its
multiuser
characteristic. It allows
multiple users on one
system, at the ...
READ MORE
SysAdmin
How To Use grep
Command In
Linux/UNIX
March 28, 2019
Contact Us
Legal
Pi P li
https://phoenixnap.com/kb/linux-chown-command-with-examples 13/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership
Privacy Policy
Get 15 TB FREE bandwidth (5 TB in Singapore) with Bare Metal Cloud!
Terms of Use
DMCA DEPLOY NOW
GDPR
Sitemap
© 2022 Copyright phoenixNAP | Global IT Services. All Rights Reserved.
https://phoenixnap.com/kb/linux-chown-command-with-examples 14/14