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:
Copychown [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.
Copychown [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.
as shown below.
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:
Copychown root newfile.txt
To verify the change in ownership, once again use the
ls -l command.
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
Copychown :[group-name] [file-name]
For instance, to change group ownership to root user run
Copychown :root newfile.txt
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
Copychown user:group newfile.txt
For instance to change both the user and the group to ‘james‘ execute:
Copychown james:james newfile.txt
Changing directory ownership
Let’s now talk about changing ownership of directories. The syntax remains fairly similar,
Copychown user:group ./directory-name/
I have a directory called
linux created by root user and it contains 3 text files:
Copyfile1.txt file2.txt file3.txt
To change user and group to ‘james’ run
Copychown james:james ./linux/
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
Copychown -R root:root linux
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
Copychown uid:gid [filename]
For example, to change the
newfile.txt to uid 1000 and gid 1000 execute:
Copychown 1000:1000 newfile.txt
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
Copychown root:root newfile.txt -v