Comenzi Linux

Linux chmod Example

Linux chmod Example

In this quick tutorial, we will see how we can use chmod command in an Ubuntu machine to find, modify and remove user permissions from specific files which exist on the user's file system. Let's play through various conditions so that we can master basic chmod commands which can make our everyday life easier with Ubuntu.

Linux Permissions

Linux Permissions are a great set of rules which are simple to understand if we grasp the basics rights. The three main points which we need to understand to know how Linux Permissions work are:

There are two basic elements in Linux Filesystem:

  1. Directories
  2. Files

There are three actions which can be performed:

  1. Read
  2. Write
  3. Execute. Apart from executing scripts, same actions is needed to create files and other folders inside it

User who can perform these actions are:

  1. Owner of the file
  2. Group of the owner of the file
  3. User which are not associated with owner group or owner itself

To see permissions related to a file, run the following command:

ls -l

Here is what we get back with this command:

Find permission for files

In the output, the first 10 characters presents the permission for the file:

Changing Permissions

Syntax for modifying permission of a file looks like:

chmod permissions file [file 2]…

Octal representation for Permissions

We can present permissions as an octal number. For example, for setting read, write & execute permissions for the owner, read & write permissions for its group, and no permission for others, to a hello.txt file, we will execute the following command:

sudo chmod 760 hello.txt

Once we execute the above command and try to read a file with a non-owner account using the following command:

sudo -u notowner-user more hello.txt

We will get the following error:

hello.txt: Permission denied

But where does this number come from? Each digit of that number represents a set of permissions. Let us see how were they derived:

For assigning read, write & execute permissions for the owner, we assigned him the number 7(= 4 + 2 + 1). Let us better understand this in a table of digits:

Number Binary Read Write Execute
0 000 NO NO NO
1 001 NO NO YES
2 010 NO YES NO
3 011 NO YES YES
4 100 YES NO NO
5 101 YES NO YES
6 110 YES YES NO
7 111 YES YES YES

Above table is much clear in what each represents in terms of file permissions.

Character representation for Permissions

We can present permissions as an octal number. For example, for setting read, write & execute permissions for the owner, read & write permissions for its group, and no permission for others, to a hello.txt file, we will execute the following command:

sudo chmod u=rwe,g=rw,o-rwx hello.txt

To add permissions to an existing user, we can also do:

sudo chmod g+w hello.txt

Here, the write permission was being assigned to the user group of the owner of the file.

Recursive Permission Changes

We can also change permissions for file contained in a specific directory with a single command. To modify the permissions of each and every file and folder in a provided directory at once, use sudo chmod with -R:

sudo chmod 777 Directory/*

We can see the following output which clearly reflects the change in file permissions:

Recursively changing file permissions

Conclusion

In this lesson, we looked at how we can modify a file permssions and if need be, do it recursively. We understood basic concepts behind how Linux permissions which can help us in our everyday work a lot.

Cursor jumps or moves randomly while typing in Windows 10
If you find that your mouse cursor jumps or moves on its own, automatically, randomly while typing in Windows laptop or computer, then some of these s...
How to reverse Mouse and Touchpads scrolling direction in Windows 10
Mouse and Touchpads not only make computing easy but more efficient and less time-consuming. We cannot imagine a life without these devices, but still...
How to change Mouse pointer and cursor size, color & scheme on Windows 10
The mouse pointer and cursor in Windows 10 are very important aspects of the operating system. This can be said for other operating systems as well, s...