0% found this document useful (0 votes)
4 views

Chown Command in Linux_ How to Change File Ownership

The document provides a comprehensive guide on using the chown command in Linux to change file ownership and group settings. It includes syntax, examples, and options for changing ownership for single files, multiple files, and recursively for directories. Additionally, it covers checking current ownership, using user and group IDs, and suppressing error messages during execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Chown Command in Linux_ How to Change File Ownership

The document provides a comprehensive guide on using the chown command in Linux to change file ownership and group settings. It includes syntax, examples, and options for changing ownership for single files, multiple files, and recursively for directories. Additionally, it covers checking current ownership, using user and group IDs, and suppressing error messages during execution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership

Get 15 TB FREE bandwidth (5 TB in Singapore) with Bare Metal Cloud!


DEPLOY NOW

Chown Command: Change Owner of File in


Linux

April 29, 2019


COMMANDS LINUX PERMISSIONS

Home » SysAdmin » Chown Command: Change Owner of File in Linux

 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

Get 15 TB FREE bandwidth (5 TB in Singapore) with Bare Metal Cloud!


DEPLOY NOW

Prerequisites

Linux or UNIX-like system


Access to a terminal/command line
A user with sudo privileges to change the ownership. Remember to run the commands
with sudo to execute them properly.

Linux Chown Command Syntax


The basic chown command syntax consists of a few segments. The help file shows the
following format:

chown [OPTIONS] USER[:GROUP] FILE(s)

[OPTIONS] – the command can be used with or without additional options.


[USER] – the username or the numeric user ID of the new owner of a file.
[:] – use the colon when changing a group of a file.
[GROUP] – changing the group ownership of a file is optional.
FILE – the target file.

Superuser permissions are necessary to execute the chown command.

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

To check the chown version on your machine, enter:

chown --version

The output will look similar to this:

How to Check Ownership of a File in


Linux
First, you need to know the original file owner or group before making ownership changes
using the chown command.

To check the group or ownership of Linux files and directories in the current location, run the
following command:

ls -l

An example output of the ls command looks like this:

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

Get 15 TB FREE bandwidth (5 TB in Singapore) with Bare Metal Cloud!


DEPLOY NOW

How to Change the Owner of a File


Changing the owner of a file with chown requires you to specify the new owner and the file.
The format of the command is:

chown NewUser FILE

The following command changes the ownership of a file sample from root to the user test:

chown test sample

Use the same format to change the ownership for both files and directories.

Change the Owner of a File With UID


Instead of a username, you can specify a user ID to change the ownership of a file.

For example:

chown 1002 sample2

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.

 Note: To check a user’s ID, run id -u USERNAME from the terminal.

Change Ownership of Multiple Linux Files


List the target file names after the new user to change the ownership for multiple files. Use
single spaces between the file names.

In the following example, root will be the new owner of files sample2 and sample3.

chown root sample2 sample3

Combine file names and directory names to change their ownership with one command. For
example:

chown root sample3 Dir1

Do not forget that the commands are case sensitive.

How to Change the Group of a File


With chown, you can change a group for a file or directory without changing the owning user.
The result is the same as using the chgrp command.

Run the chown command using the colon and a group name:

chown :NewGroup FILE

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

Get 15 TB FREE bandwidth (5 TB in Singapore) with Bare Metal Cloud!


chown :group3 sample3 DEPLOY NOW

List multiple names of files or directories to make bulk changes.

Change the Group of a File Using GID


Similar to UID, use a group ID (GID) instead of a group name to change the group of a file.

For example:

chown :1003 sample

Change Owner and the Group


To assign a new owner of a file and change its group at the same time, run the chown
command in this format:

chown NewUser:NewGroup FILE

Therefore, to set linuxuser as the new owner and group2 as the new group of the file sample2:

chown linuxuser:group3 sample3

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:

chown NewUser: FILE

The following example changes the group ownership to the login group of linuxuser:

chown linuxuser: sample3

Transfer Ownership and Group Settings from One File


to Another
Rather than changing the ownership to a specific user, you can use the owner and a group of
a reference file.

Add the --reference option to the chown command to copy the settings from one file to
another:

chown --reference=ReferenceFILE FILE

Remember to type in the names of the files correctly to avoid the error message:

Check Owner and Group Before Making


Changes
https://phoenixnap.com/kb/linux-chown-command-with-examples 7/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership

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:

chown --from=CurrentUser:CurrentGroup NewUser:NewGroup FILE

The example below shows we first verified the ownership and the group of the file sample3:

chown --from=root:group2 linuxuser:group3 sample3

Then chown changed the owner to linuxuser and the group to group3.

Check Owner Only


The option --from can be used to validate only the current user of a file.

chown --from=CurrentUser NewUser FILE

For example:

chown --from=root linuxuser sample3

Check Group Only


Similar to the previous section, you can validate only the group of a file using the option --fr
om.

chown --from=:CurrentGroup :NewGroup FILE

Here is an example where we verified the current group before changing it:

chown --from=:group3 :group4 FILE


https://phoenixnap.com/kb/linux-chown-command-with-examples 8/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership

Get 15 TB FREE bandwidth (5 TB in Singapore) with Bare Metal Cloud!


DEPLOY
Remember to use the colon for both group NOW
names to avoid error messages.

How to Recursively Change File


Ownership
The chown command allows changing the ownership of all files and subdirectories within a
specified directory. Add the -R option to the command to do so:

chown -R NewUser:NewGroup DirNameOrPath

In the following example, we will recursively change the owner and the group for all files and
directories in Dir1.

chown -R linuxuser:group3 Dir1

Chown Command and Symbolic Links


To change the owner of a symbolic link, use the -h option. Otherwise, the ownership of the
linked file will be changed.

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

Get 15 TB FREE bandwidth (5 TB in Singapore) with Bare Metal Cloud!


chown -h NewUser:NewGroup SymbolicLink
DEPLOY NOW

In the following example, we changed the owner and group of a symbolic link.

Display Chown Command Process


Details
By default, the terminal does not display the chown process information. To see what
happens under the hood, use one of the two command line flags:

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:

chown -v linuxuser sample2

The terminal produces the following output:

Switch from -v to -c and there will be no messages in this case. This happens because
there are no owner or group changes.

The information is particularly useful with the recursive chown command:

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

Get 15 TB FREE bandwidth (5 TB in Singapore) with Bare Metal Cloud!


DEPLOY NOW

In this example, the output lists all objects affected after running the command.

Suppress Chown Command Errors


To avoid seeing potential error messages when running the chown command, use the -f
option:

chown -f NewUser FILE

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.

Next you should read


Was this article helpful? Yes No
https://phoenixnap.com/kb/linux-chown-command-with-examples 11/14
8/21/22, 10:13 AM Chown Command in Linux: How to Change File Ownership

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

Nmap stands for


Network Mapper. It is
an open-source tool for
network exploration
and security auditing.
With ...
READ MORE

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

This guide details the


most useful grep
commands for Linux /
Unix systems. After
going through all the ...
READ MORE

 Live Chat  Get a Quote  Support | 1-855-330-1509  Sales | 1-877-588-5918

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

You might also like