If you are just starting to learn about file permissions on a Linux system, the chmod and chown commands will be your starting point for granting or revoking file permissions for user accounts. chmod and chown are completely different commands, yet they go hand in hand when it comes to modifying file permissions on the Linux file system. The basic summary is that
chown can change the owner of a file, and
chmod can change the permissions of the file, but this explanation is only scratching the surface.
To gain the ability to fully edit the permissions of a file, it will be necessary to master both the
chown commands. But do not worry, neither one is that complicated, and both just require a little background to wrap your head around.
In this tutorial, we will explain the differences between the
chown commands on a Linux system. You will learn the command syntax through examples for both commands, and see how to use them in conjunction with each other in order to achieve the proper permissions and security settings that you desire for the files on your Linux system. Join us below to learn more.
In this tutorial you will learn:
- How to change file ownership with the
- How to change file permissions with the
- What is the difference between the
|Requirements, Conventions or Software Version Used
|Any Linux distro
|Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
chown vs. chmod Linux Commands
chown Linux commands are the main tools used for controlling file permissions in Linux. They both go hand in hand, and must be used in conjunction to effectively control which users have the ability to use certain files.
There are three main permissions in Linux:
- Read – the ability to open a file and read its contents
- Write – the ability to edit and save changes to files
- Execute – the ability to execute a file, such as a Bash script
These three types of permissions can then be delegated to three different user types:
- Owner – the user or owner of the file
- Group – a user group that has permissions on the file
- Other – all other users that are neither the owner nor in the group
Let’s say we have the following file:
$ ls -l example_file.txt -rwxrw-r-- 1 linuxconfig admins 22 Oct 2 18:00 example_file.txt
The output tells us that:
- The owner of the file is
linuxconfig, with read, write, and execute permissions
- The group of the file is
admins, with read and write permissions
- All other users only have read permissions on the file
Next, let’s see how to use both of these commands to change which users can interact with our files on Linux.
How to use chown
In order to assign permissions to individual users, or a group of users, we first need to use the
chown command to specify who the owner and group of the file should be. Once we have these settings applied, then we can edit the owner and group permissions with
- Let’s start by setting the owner of file
$ sudo chown linuxconfig:linuxconfig example_file.txt
Note that we used the
sudocommand here, which is necessary for changing file ownership. We also used the
linuxconfig:linuxconfigsyntax to change both the user and group simultaneously. This is effectively the same as giving no group permissions, since the
linuxconfiggroup only contains one user –
- Or if we want to configure both the owner
adminswe can execute:
$ sudo chown linuxconfig:admins example_file.txt
chgrpcommand can be used if you only want to change the group for a file, and not the owner. In this example we change the group to
$ sudo chgrp admins example_file.txt
How to use chmod
Now that we have the owner and group set correctly, we can use the
chmod command to modify the file permissions we need for the owner, group, and other users.
chmod command can accept many syntaxes and options, and it would be out of the scope of this tutorial to cover them all. Instead, see our chmod command tutorial for a list of various examples. In this section, we will just cover the basics in order to help you understand how the
chmod is different from
The following command will change permissions for the owner, group, and other users:
$ chmod 760 example_file.txt
7is equivalent to read, write, and execute permissions, and will grant these permissions to the owner
6is equivalent to read and write permissions, and will grant them to the group
0is equivalent to no permissions, and assigns them to all other users
In this way, we can see how the
chmod commands complement each other to achieve the permissions that we need for each file. The
chmod command controls permissions for all users, and the
chown command allows us to assign permissions in a more granular way by configuring the owner and group accounts.
In this tutorial, we learned about the differences between the
chmod commands on a Linux system. Both of these commands complement each other to give users the ability to delegate read, write, and execute permissions to users on the system. These two commands should be understood and mastered to secure files from being accessed by unintended users.