Basics of Using chown and chmod Commands

This post will discuss the chown and chmod commands in Linux! They can be daunting for a new Linux user, but you’re in the right place to learn about them. Also note, you may also want to read our most used Linux commands post to learn the basics about these commands. That post also discussed the chown and chmod command, but it shows how you can copy permissions.

YouTube player

What is the chown (Change Owner) command?

We use the chown command to change the owner of the files and directories. And you quite often use the ls -l command to check the owner and group of a particular file.

For example, running ‘ls -l song.txt’ will give you the following output:

localhost:~# ls -l song.txt
-rw-r--r--    1 sohail      admins            151 Jul  6 03:49 song.txt
                  ^            ^
                  |            |
                owner        group
How to use the chown command?

The chown command supports many syntaxes allowing you to add a new owner or owner and group.

chown [new_owner_name] file_name
chown [new_user]:[new_group] file_name
Chown Examples:
localhost:~# ls -l song.txt 
-rw-r--r-- 1 sohail      sohail          0 Aug 15 05:30 song.txt
localhost:~# sudo chown anthony song.txt 
localhost:~# ls -l song.txt 
-rw-r--r-- 1 anthony     sohail          0 Aug 15 05:30 song.txt

So, as you can see in the output using ls -l, the file owner was Sohail, and we changed it to Anthony. Also, do not forget to use sudo with chown command.

The following command will change the group of the file:

sudo chown [group_name] file_name

Now, let’s see an example of changing both the user and group of a file.

localhost:~# ls -l song.txt 
-rw-r--r-- 1 sohail   sohail          0 Aug 15 05:30 song.txt
localhost:~# sudo chown root:root song.txt 
localhost:~# ls -l song.txt 
-rw-r--r-- 1 root     root            0 Aug 15 05:30 song.txt

So, as you can see from the above output, we have changed the file-owner from Sohail to root and group from Sohail to root.

We can use the following command to change the ownership of many files.

sudo chown user_name file1 file2 file3

What is the chmod (Change Mode) command?

You use the chmod command to change access permissions of the files and directories.

For example:

localhost:~# ls -l song.txt
-rw-r--r--    1 sohail      sohail            151 Jul  6 03:49 song.txt
How to use the chmod command?
syntax:    chmod [options] mode filename
mode:      read(r),     write(w),     execute(x)

We will explore changing permissions using octal and symbol representations.

Octal Number Representation for chmod Command:
file_or_dir           owner             group             others
                      r w x             r w x             r w x
                      4 2 1             4 2 1             4 2 1

The same order you can see above would be the order of permissions as we read them from left to right when we run the ls -l command. Octal describes the base-8 number system, so it has numbers from 0 to 7.

Therefore, as you can see above, we use number 4 for reading permission, number 2 for write permission, and number 1 for execute permission.

Now that we understand the octal number representation, let’s look at an example using the ls -l command below:

sohail@ubuntu:~/Documents# ls -l
total 3
drwxr-xr-x 2 sohail sohail 4096 Aug 15 06:14 kubernetes

Running ls -l will, for example, result in the above output on line 3:

1.drwxr-xr-xare the 10 permission flags
2.2indicates the link count
3.Sohailis the owner of the file
4.SohailGroup name this file is associated to
5.4096is the size of the file
6.Aug 15 06:14show the file modification date and time
7.kubernetesis the name of the file

It is important to clarify what each of the permission flags means. Firstly, any line that starts with a hyphen (-) indicates information about a file. Secondly, any line that begins with an alphabet d implies that this is information about a directory.

Then, characters 2 to 4, which are rwx, represent the access permissions for the file owner. Next, the characters 5 to 7, which are r-x, represent the permissions for the group this file is associated with. Finally, the last 3 characters, which are again r-x represent the access permissions for other users.

Chmod Examples using 4 for reading permission:
chmod 400 test.txt
-r-------- 1 root root 0 Aug 15 06:47 test.txt

Consequently, as per the example above, the chmod 400 command results give read-only permission to the file owner.

chmod 040 test.txt
----r----- 1 root root 0 Aug 15 06:47 test.txt

Similarly, the chmod 040 command gives read-only permission to the group it belongs to.

chmod 004 test.txt

Lastly, the chmod 004 command gives read-only permission to everyone.

Chmod Examples using 2 for Writing permission:
chmod 200 test.txt

Consequently, as per the example above, the chmod 200 command gives write-only permission to the file owner.

chmod 020 test.txt

Similarly, the chmod 020 command gives write-only permission to the group it belongs to.

chmod 002 test.txt

Lastly, the chmod 002 command gives write-only permission to everyone.

Chmod Examples using 1 for Executing permission:
chmod 100 test.txt

Consequently, as per the example above, the chmod 100 command gives execute-only permission to the file owner.

chmod 010 test.txt

Similarly, the chmod 010 command gives execute-only permission to the group it belongs to.

chmod 001 test.txt

Lastly, the chmod 001 command gives execute-only permission to everyone.

Common chmod Permissions:
chmod 644 test.txt

The chmod 644 is overall quite a popular command that results in:

  • read and write permission to the owner of the file,
  • read permission to the group it belongs to,
  • and read permission to others.
chmod 777 test.txt

Also popular is the chmod 777 command that gives read, write and execute permission to everyone.

Symbolic Representation for chmod Command:

This is yet another way through which we can use the chmod command using symbols.

For example:

chmod [ugoa] [+-] [rwx] files
SymbolMeaning
u‘u’ means the owner of the file
g‘g’ means the users who are members of the file’s group
o‘o’ means the users who neither belong to u nor belong to g
a‘a’ means all the options like u, g, and o
+add permission
remove permission
rread permission
wwrite permission
xexecute permission
Chmod Examples:
chmod +x script.sh

This chmod command gives execute permissions to the current user where plus (+) symbols mean we want to add a permission, and in this case, that permission is ‘execute’ (x). Minus (-) symbols can also be used to take back any permission.

sohail@ubuntu:~/Documents# ls -l
total 1
-rw-r--r-- 1 sohail sohail 0 Aug 15 06:23 script.sh
   ^  ^  ^
   |  |  |
no execute permissions

sohail@ubuntu:~/Documents# chmod +x script.sh 

sohail@ubuntu:~/Documents# ls -l
total 1
-rwxr-xr-x 1 sohail sohail 0 Aug 15 06:23 script.sh
   ^  ^  ^
   |  |  |
execute permissions

As you can see from the output of the first ls -l command, there was no execute permission. We can then see that the execute permission is set once we use the chmod +x command.

chmod u+x script.sh

Consequently, this command will give execute permission to the owner of the file.

chmod g+rw folder_name

Finally, this command will give read(r) and write(w) permissions to the members of the file’s group.

Wrapping up chmod and chown

To sum up, we have learned about chmod and chown. So we know that chmod stands for change mode, and we use it to set permissions. We also know that chown stands for change owner. Furthermore, we have learned that you can use the ls -l command to see the owner and permissions. And then, we could see how chmod supports an ‘octal’ and ‘symbol’ mode.

You may also be interested in

About Anto Online

Anto, a seasoned technologist with over two decades of experience, has traversed the tech landscape from Desktop Support Engineer to enterprise application consultant, specializing in AWS serverless technologies. He guides clients in leveraging serverless solutions while passionately exploring cutting-edge cloud concepts beyond his daily work. Anto's dedication to continuous learning, experimentation, and collaboration makes him a true inspiration, igniting others' interest in the transformative power of cloud computing.

View all posts by Anto Online

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.