Polecenia Linuksa

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.

Gry Zainstaluj najnowszy emulator Dolphin dla Gamecube i Wii w systemie Linux
Zainstaluj najnowszy emulator Dolphin dla Gamecube i Wii w systemie Linux
Emulator Dolphin pozwala grać w wybrane gry Gamecube i Wii na komputerach osobistych z systemem Linux (PC). Będąc ogólnodostępnym emulatorem gier o o...
Gry Jak korzystać z GameConqueror Cheat Engine w systemie Linux
Jak korzystać z GameConqueror Cheat Engine w systemie Linux
Artykuł zawiera przewodnik dotyczący korzystania z silnika oszukującego GameConqueror w systemie Linux. Wielu użytkowników, którzy grają w gry w syste...
Gry Najlepsze emulatory konsoli do gier dla systemu Linux
Najlepsze emulatory konsoli do gier dla systemu Linux
W tym artykule wymienimy popularne oprogramowanie do emulacji konsoli do gier dostępne dla systemu Linux. Emulacja to warstwa kompatybilności oprogram...