Linux рекурсивно задать права

How to PROPERLY apply chmod recursively? [SOLVED]

Let me guess if you are here, not able to assign permission to directories and files? Getting permission denied error? Or you have a lot of directories and files and trying to figure how how to use chmod recursively to change permissions? Do you want to apply 777 or 755 permission across all the files or directories?

Well you are at the right place. In this tutorial guide I will help us understand the usage of chmod and how you can change permissions of files and directories using a single command

The chmod command in Unix-like operating systems is used to change the permissions of files and directories. The «recursive» option allows you to change the permissions of a directory and all of its contents, including subdirectories and files.

When you use chmod with the -R or —recursive option, it will apply the permission changes to the specified directory and all of its subdirectories and files. This can be useful when you want to change the permissions of a directory tree without having to change the permissions of each individual file and directory separately.

You can also add/modify/remove special permissions such as setuid, setgid, sticky bit with chmod

Linux Permissions Syntax

You can use this table to understand the different symbolic or octal value to use with chmod

Permission Octal Syntax (Symbolic)
read+write+execute 4+2+1=7 rwx
read+write 4+2+0=6 rw-
read+execute 4+0+1=5 r-x
read 4+0+0=4 r—
write+execute 0+2+1=3 -wx
write 0+2+0=2 -w-
execute 0+0+1=1 —x

Perform chmod recursive with -R or —recursive

If all your files and directories are under one parent directory then you can directly use chmod -R to assign the permission recursively

The syntax to modify the file and directory permission recursively:

I hope you know, in a syntax anything under square braces [] is optional and anything under curly braces <> is mandatory

In this syntax I have also added —changes or -c as optional as this will inform you if the change really happened in the backend

To assign 755 permission of all files and directories under /opt/dir

This would also remove any special permission if already assigned to any of the files or directories under /opt/dir . For example, I created a file with Sticky Bit Special permission under /opt/dir . Once I apply chmod recursively, the special sticky bit permission was also removed

mode of '/opt/dir/' changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x---) mode of '/opt/dir/file' changed from 7555 (r-sr-sr-t) to 0750 (rwxr-x---)

If you want to change permission of files and directories under /opt/dir and not the permission of /opt/dir itself then you should use chmod -c -R /opt/dir/ * . This will change permission of everything under /opt/dir/ but not for dir directory itself

Читайте также:  Prestigio 141a02 установка linux

You can also use octal method here, in this example I have added full permission to user, read and execute permission for for group and removed all permission from others which is equivalent to 750 in octal method

# chmod -c -R u+rwx,g+rx,o-rwx /tmp/dir1/* mode of '/tmp/dir1/dir2/file5' changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x---) mode of '/tmp/dir1/dir2/dir3' changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x---) mode of '/tmp/dir1/dir2/dir3/file1' changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x---) mode of '/tmp/dir1/dir2/dir3/file2' changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x---) mode of '/tmp/dir1/dir2/dir3/file5' changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x---) mode of '/tmp/dir1/dir2/dir3/file3' changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x---) mode of '/tmp/dir1/dir2/dir3/file4' changed from 0755 (rwxr-xr-x) to 0750 (rwxr-x---)
# tree -p /tmp/dir1/ /tmp/dir1/ ├── [drwxr-x---] dir2 │ ├── [drwxr-x---] dir3 │ │ ├── [-rwxr-x---] file1 │ │ ├── [-rwxr-x---] file2 │ │ ├── [-rwxr-x---] file3 │ │ ├── [-rwxr-x---] file4 │ │ └── [-rwxr-x---] file5 │ └── [-rwxr-x---] file5 └── [-rwxr-x---] file10 2 directories, 7 files

Since I used /tmp/dir1/* in my command, the permission of dir1 is not modified.

# ls -ld /tmp/dir1/ drwxr-xr-x 3 root root 4096 May 23 12:02 /tmp/dir1/

Now some of the drawbacks of above command

  1. It will modify the permission of all the files and directories under single path. If you have many files or directory under different locations such as /opt , /var , /tmp then you will end up executing chmod multiple times
  2. There is no way to differentiate between files and directories. If you have a requirement to change permission of only files or directories then the tool itself can not handle this
  3. Similarly if you want to change permission of certain files or directory with specific naming syntax, the tool can not handle itself such filter

So we need find command to take care of these filters before applying chmod command

find can apply these filters and then it can be combined with exec or xargs to execute chmod command. If you use find in combination with chmod then you do not need —recursive or -R as find itself will search all the files and pass individual file for chmod as an input argument. Let me give you some examples to help you understand better:

Change permission recursively using find command

Method-1: Use find with exec to change permission recursively

I have already written a descriptive tutorial to learn and understand find and exec combination.
The syntax to change permission using find with chmod would be

find [OPTIONS] -exec [COMMAND] <> \;
  • Replace PATH with the location of your files or directories
  • You can choose all the supported options with find command such as define -type , or -name etc
  • Replace COMMAND with chmod
  • Do not remove or replace <> \; as this will be used by find to recursively look for files or directories and provide the found file as an input argument to COMMAND
  • So if find command finds a file named myfile then the COMMAND would become » chmod myfile «

Search for all files under /tmp and change their permission to 644

# find /tmp -type f -exec chmod --changes 644 <> \;

To assign the same permission using symbolic method:

# find /tmp -type f -exec chmod --changes u=rw,go=r <> \;

Search for all directories under /tmp and change their permission to 755

# find /tmp -type d -exec chmod --changes 755 <> \;

To assign the same permission using symbolic method:

# find /tmp -type d -exec chmod --changes u=rwx,go=rx <> \;

Method-2: Use find with xargs to change permission recursively

Similarly you can also combine find with xargs to assign permission recursively

Читайте также:  Linux mint определение файловых систем

The syntax to use find with xargs and chmod would be:

find [OPTIONS] -print0 | xargs -0 [COMMAND]
  • -print0 Tells find to print all results to std, each separated with the ASCII NUL character ‘\000’
  • -0 Tells xargs that the input will be separated with the ASCII NUL character ‘\000’

Search for all files under /tmp and change their permission to 644

# find /tmp -type f -print0 | xargs -0 chmod --changes 644

To assign the same permission using symbolic method:

# find /tmp -type f -print0 | xargs -0 chmod --changes u=rw,go=r

Search for all directories under /tmp and change their permission to 755

find /tmp -type d -print0 | xargs -0 chmod --changes 755

To assign the same permission using symbolic method:

# find /tmp -type d -print0 | xargs -0 chmod --changes u=rw,go=rx

What is —preserve-root? Why you should use it?

  • In this tutorial I will be using —preserve-root with all of my chmod commands
  • This is a recommended option to use when you are planning to assign permission recursively as this can be destructive
  • Imagine by mistake you removed all permission from / (root) directory, this would lead in an unusable system
  • It is similar to running rm -rf / which if you are a Linux Administrator should know, this can destroy your server
  • With —preserve-root we inform the command not to modify root directory permission

DO NOT USE THIS COMMAND

# chmod --changes --recursive 755 /

Snippet from my terminal with this command, see the damage this command can do to your server.

chmod recursive usage guide for absolute beginners

This would recursively change the permission of all files and dir under / which can also destroy your system

In such case it is always recommended to use

# chmod --changes --recursive --preserve-root 755 / chmod: it is dangerous to operate recursively on '/' chmod: use --no-preserve-root to override this failsafe

It may not be possible to use this additional option every time with chmod so you can create an alias

# alias chmod='chmod --preserve-root'

and also add this to your /etc/bashrc or individual user’s .bashrc file for permanent changes

Now if we use chmod , it does not allow to modify root permission

# chmod -c --recursive 755 / chmod: it is dangerous to operate recursively on '/' chmod: use --no-preserve-root to override this failsafe

Conclusion

chmod can be used with -R or also referred as —resursive to assign/modify permission to files and directories recursively. Although it is recommended to use —changes and —preserve-root along with the command to avoid any destruction.
It is possible that you may not want out STDOUT on the screen so you can choose to reduce all messages and errors to a different file by using

# chmod --recursive --changes 755 /tmp/dir1/ >& /tmp/logfile

So this would store all the output to /tmp/logfile which you can use for debug purpose.

Читайте также:  Tcp connection timeout linux

Lastly I hope the steps from the article to apply chmod recursively on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

References

I have used below external references for this tutorial guide
man page for chmod

Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

Как рекурсивно изменить права доступа к файлу в Linux

Если вы используете Linux в качестве основной операционной системы или управляете серверами Linux, вы столкнетесь с ситуацией, когда попытаетесь создать или отредактировать файл и получите ошибку «Permission deny». Как правило, ошибки, связанные с недостаточными разрешениями, можно решить, установив правильные права доступа или владельца .

Linux — это многопользовательская система, и доступ к файлам контролируется с помощью разрешений, атрибутов и владельцев файлов. Это гарантирует, что только авторизованные пользователи и процессы могут получить доступ к файлам и каталогам.

Для получения дополнительной информации о правах доступа к файлам см. «Команда Umask в Linux» .

В этой статье мы объясним, как рекурсивно изменять права доступа к файлам и каталогам.

Chmod Рекурсивный

Команда chmod позволяет изменять права доступа к файлам в символьном или числовом режиме.

Чтобы рекурсивно работать со всеми файлами и каталогами в данном каталоге, используйте команду chmod с параметром -R , ( —recursive ). Общий синтаксис для рекурсивного изменения прав доступа к файлу следующий:

Например, чтобы изменить права доступа для всех файлов и подкаталогов в каталоге /var/www/html на 755 вы должны использовать:

Режим также можно указать с помощью символьного метода:

chmod -R u=rwx,go=rx /var/www/html

Только root, владелец файла или пользователь с привилегиями sudo могут изменять права доступа к файлу. Будьте особенно осторожны при рекурсивном изменении разрешений файлов.

Использование команды find

Как правило, файлы и каталоги не должны иметь одинаковые разрешения. Большинству файлов не требуется разрешение на выполнение, тогда как вы должны установить разрешения на выполнение для каталогов, чтобы изменить их.

Наиболее распространенный сценарий — рекурсивное изменение разрешений файла веб-сайта на 644 и разрешений каталога на 755 .

find /var/www/html -type d -exec chmod 755 <> ;find /var/www/html -type f -exec chmod 644 <> ;
find /var/www/html -type d -exec chmod u=rwx,go=rx <> ;find /var/www/html -type f -exec chmod u=rw,go=r <> ;

Команда find ищет файлы или каталоги в /var/www/html и передает каждый найденный файл или каталог команде chmod для установки разрешений.

При использовании find с -exec команда chmod запускается для каждой найденной записи. Используйте команду xargs чтобы ускорить операцию, передав сразу несколько записей:

find /var/www/html -type d -print0 | xargs -0 chmod 755 find /var/www/html -type f -print0 | xargs -0 chmod 644

Выводы

Команда chmod с параметрами -R позволяет рекурсивно изменять права доступа к файлу.

Чтобы рекурсивно установить разрешения для файлов в зависимости от их типа, используйте chmod в сочетании с командой find .

Если у вас есть какие-либо вопросы или отзывы, не стесняйтесь оставлять комментарии.

Источник

Оцените статью
Adblock
detector