Once in a while, you will be faced with the prospect of changing permissions of files and folders. Linux chown command enables you to modify file and directory permissions as you deem fit. This comes in handy, especially when configuring features and services. Let’s see how the command can be used.
Table of Contents
Linux chown Command Syntax
The syntax for chown command usage is as shown:
chown [option] [owner]:[group] file
Let’s take a look at a couple of options that come with the command.
Changing file ownership
How do you change ownership of a file? The syntax is quite simple as shown below.
chown [owner] file
Let’s look at a file 'newfile.txt'
created by user ‘james’. By default, this file belongs to user “james” and group “james” as shown in the output below.
ls -l
as shown below.
Sample output
As illustrated, the first attribute after the file permissions denotes the user who owns the file and the second attribute denotes the group the file belongs to.
To change file ownership to a different user, for instance, root user, execute:
chown root newfile.txt
To verify the change in ownership, once again use the ls -l
command.
Sample output
Changing the group a file belongs to
From the previous example, we managed to change the ownership of a file from one user to another. However, if you were keen enough, you must have observed that the group did not change. Changing the group a file belongs to is very similar to changing the user. The syntax differs a little as shown
chown :[group-name] [file-name]
For instance, to change group ownership to root user run
chown :root newfile.txt
Sample output
This time around, we’ve managed to change the file’s group.
Changing both the user and group the file belongs to in one command
If you want to make your work easy and change both the user and the group to which the file belongs, then use the syntax
chown user:group newfile.txt
For instance to change both the user and the group to ‘james‘ execute:
chown james:james newfile.txt
Sample output
Changing directory ownership
Let’s now talk about changing ownership of directories. The syntax remains fairly similar,
chown user:group ./directory-name/
I have a directory called linux
created by root user and it contains 3 text files:
file1.txt
file2.txt
file3.txt
To change user and group to ‘james’ run
chown james:james ./linux/
Sample output
As you have keenly noted, despite changing the user and group to which the directory belongs, the directory contents remain unaltered. To cascade file ownerships down to the folder contents, we are going to recursively change permissions as you will learn in the next sub-topic.
Recursively change file permissions of a directory
To recusively effect file permissions, use the -R
option
chown -R root:root linux
Sample output
From the output, we can clearly see that the file permissions have been effected on the files contained in the ‘linux’ directory.
Change UID and GID of a file
Instead of specifying the user or group in chown command, one may opt to specify the GID or UID to which the file will belong. To accomplish this, use the syntax
chown uid:gid [filename]
For example, to change the newfile.txt
to uid 1000 and gid 1000 execute:
chown 1000:1000 newfile.txt
Sample output
Display verbose output
You can choose to display the operation that’s taking place on the terminal as permissions are being changed. To do this, use the -v
options
chown root:root newfile.txt -v