The chown command is an essential tool in any Linux server admin's toolkit. It is an abbreviation of "change owner" and has been in continual use since its release in 1971.
What is chown?
The chown command allows users to change the owner of a file or directory to a specified user or group. You can specify any username or group name present in their databases or type a user or group ID.
What does chown do?
By changing the ownership of files and folders, chown can allow or deny certain applications or users to read and write to them. It's therefore an important security and usability tool. In an ideal setup, you would ensure that applications, users, and services only the ownership required to function.
How to use the chown command
The basic chown command syntax looks like something like this:
chown [OPTIONS] USER[:GROUP] Name of file/directory
This may not make sense to you right now, so let's break it down into parts:
- Chown: The base command.
- OPTIONS: Flags to modify the behavior of chown (for example
-v
to give a verbose output. - USER: As you would expect, this is the name of the user you want to be the new owner of the file. It can be expressed as a username (
bitlaunch
), or a User ID (1). You can enter just the user if you want them to become the user of a given file or files without any changes to the group ownernship. - :GROUP: Changes the group ownership of the files. If only group is used and there is no
USER
, only the group ownership will be modified. - USER:GROUP: The user ownership is changed to the specified user and the group to the specified group.
Chown command examples
Chown's syntax is easier to understand once you look at some examples.
chown bitlaunch yourfile.txt
Would change the ownership of yourfile.txt
to a user called bitlaunch
.
chown bitlaunch yourdirectory
Changes the owner of the specified directory (yourdirectory
) to the user bitlaunch
.
chown bitlaunch yourfile.txt yourdirectory
Changes both the owner of yourfile.txt
and yourdirectory
to bitlaunch
. You can use a non-comma seperated list in this way to change multiple files or directories at once.
How to change the group of a file with chown
Changing the group of a file is simple, too. You can view groups in Linux using cat /etc/group
. Then run the command:
chown :yourgroup fileordirectory
Alternatively, change the owner and group using:
chown bitlaunch:group fileordirectory
How to use recursive chown
In many instances, you'll want to chown recursively, instructing the command to apply the changes to all files and directories under your given directory. To give an example, chown bitlaunch directory
would affect the following:
directory ↪ file1.txt |file2.exe |file3.png | subdirectory ↪ file.exe | file.jpg | subdirectory2 ↪ file.png
If you wanted to chown the subdirectories and their folders, you would need to use the recursive chown option (-R
). This should be placed in the command syntax before your specified user or group.
chown -R user:group directoryname
directory ↪ file1.txt |file2.exe |file3.png |subdirectory ↪ file.exe |file.jpg |subdirectory2
Or, if you also wanted to affect symbolic links, chown -hR
.
How to check file ownership in Linux
It's always important to verify that the ownership changes you made applied as expected, particularly when modifying important files. In the absence of any errors, you can navigate to the chowned directory and use the command ls -l
to check the ownership of files in it. The output will look similar to this:
total 16
drwxr-xr-x 2 bitlaunch bitlaunch 4096 Aug 1 07:20 directory2
-rw-r--r-- 1 bitlaunch bitlaunch 8 Aug 1 07:20 file
-rw-r--r-- 1 bitlaunch bitlaunch 5 Aug 1 07:20 file1.txt
-rw-r--r-- 1 bitlaunch bitlaunch 9 Aug 1 07:20 file2.exe
The first column shows the type of file and its permission settings. Here's what each column means:
Type and permissions | Links to file | Owner | Group | File size | Last modified | File name |
---|---|---|---|---|---|---|
-rw-r--r-- | 1 | bitlaunch | bitlaunch | 8 | 01/08/2001 07:20 | file1.txt |
Closing words
These are the basics of using the chown command to manage ownership on your Linux PC or server. Remember, you can always find additional options and information by typing chown --help
in the command line of your distro of choice.
We recommend practicing these techniques on some sample or non-important files before using them on mission-critical directories. Alternatively, sign up to BitLaunch and use a free trial of one of our Linux VPS servers. This will allow you to test these commands without affecting your local PC.