DEV Community

Cover image for Changing Permissions in Linux System
yash sugandh
yash sugandh

Posted on

Change Permissions Linux Changing Permissions in Linux System

In the last post, we explored Permissions in Linux System.

If you have not read the previous post I would strongly recommend you read the last post first.

Now, let's move onto Changing Permissions in Linux System.

1. chmod Command

chmod stands for change file mode is used to change permissions of a file/directory.

Can anyone change the permissions of a file/directory?

No, only the owner or the root user(superuser) can change the file/directory permissions.

There are two way's in which we can change the mode(Permissions)

  1. Absolute mode
  2. Symbolic mode

Absolute mode:

In Absolute mode, we use the Octal Number to represents Permissions for Owner, Group, and Others.

Okay, but what do you mean by Octal Number?

Octal is a number system that is used to represent numbers on a computer.

In Octal, counting is done with numbers from 0 to 7

Octal Number Type of Permission Symbol
0 No Permission ---
1 Execute Only --x
2 Write Only -w-
3 Write and Execute -wx
4 Read Only r--
5 Read and Execute r-x
6 Read and Write rw-
7 Read, Write and Execute rwx

Let's look at the syntax for chmod

chmod syntax

To change the mode of a file/directory we just use chmod along with one of the modes from the above table and files/directories.

chmod example

In the above example, we used the command ls -l hello.txt to check the current permissions for owner, group, and others and found that the owner had read and write (rw-) permission, group also had read and write (rw-) permission and other had read-only permission (r--)

What if to make it more secure we wanted to change the permission for the group to read-only and keep the rest as is

chmod-update-permission

we used the command chmod 644 hello.txt where

chmod represents the command change mode
644 represents read-write, read-only and read-only permissions
hello.txt represents name of the file we want to change permissions for.

Now that we know how to use chmod command let's look at what are some most used modes

Mode File Attributes Meaning of mode
777 rwxrwxrwx No restrictions on permissions. Generally, not a good setting as anyone can change and execute your file
700 rwx------ The owner has full access. Nobody else has any rights. This setting is useful if we want to keep our files and directories private. Similarly we can use 600 for non-executable file
644 rw-r--r-- The owner may read and write a file, while all others may only read the file. This setting is useful if the owner should be the only one to change the file
666 rw-rw-rw- All users may read and write the file. This setting is useful if you have some common file

Now, let's move on the 2nd way we can change mode

Symbolic Notation

The Symbolic notation is divided into three parts

  • Who the change will affect

Unlike Absolute mode in Symbolic notation, we can actually specify who should be affected by the change

u represents user(Owner)
g represents group
o represents others
a represents all(i.e. u,g and o)

  • Which operation will be performed

+ represents an operation to add a permission
- represents an operation to remove a permission
= represents an operation to set permission and replace the previous one

  • What permission will be set

r represents read permission
w represents write permission
x represents execute permission

Let's take an example of each operation

  • Add Permission

add-permission

In the above example, we used the command chmod u+x hello.txt to add executable permission to the Owner where

chmod represents the command to change mode
u represents user(Owner)
+ represents the addition of permission
x represents executable permission

  • Remove Permission

remove permission

In the above example, we used the command chmod o-r hello.txt to remove the read-only permission from Other where

chmod represents the command to change mode
o represents other
- represents the removal of permission
r represents read permission

  • Assignment of Permission

Assignment Permission

In the above example, we used the command chmod g=wr hello.txt to assign the read and write permission to group where

chmod represents the command to change mode
g represents group
= represents the assignment of permission
wr represents read and write permission

Note: If we don't specify who will be affected, it is by default taken as all

This was all about changing permissions of a file/directory.

Now, let's look at how to change ownership and group of a file/directory.

chown Command

The chown command is used to change the owner/group of a file.

chown syntax

Let's take an example

chown example

In the example, we can see that the owner and group of the file hello.txt is "yash".

Let's try and change the owner of the file to "root".

Since we are changing the ownership of the file this operation will require the root access

chown-owner

In the above example, we first tried changing the ownership without using root privileges and it gave us an error.

Then we used the command sudo chown root hello.txt command where

sudo for executing as a superuser
chown represents the change ownership command
root represents the new owner
hello.txt represents the file to be affected

Let's take another example where we change our owner back to "yash" but change our group to "root"

chown-grp

In the above example, we used the command sudo chown yash:root hello.txt where

sudo for executing as a superuser
chown represents the change ownership command
yash represents the new owner
root represents the new group
hello.txt represents the file to be affected

What if we just wanted to change our group?

chgrp Command

To change the group using chgrp command we just use chgrp newGroupName fileName

Let's take an example, we recently changed the group of our file "hello.txt" from "yash" to "root".

Now, let's change it back

chgrp-example

In the above example, we used the command chgrp yash hello.txt where

chgrp represents the change group command
yash represents the new group
hello.txt represents the file to be affected

So, this was all about Changing Permissions in Linux System. Hope you understood.

Please let me know if there are any queries and suggestions.

See you in the funny papers 🚀

Top comments (1)

Collapse
 
titanhero profile image
Lex

Cool😉👍✌️😁