Por que o chmod está recursivamente mudando as permissões de arquivo também?

1

Quando eu faço chmod _+x -R /dir onde "_" é qualquer combinação de (u, g, o, a), se depois eu fizer chmod g+X -R /dir , os arquivos também ganham permissões executáveis.

Por que isso acontece? Esse comportamento só acontece se eu usar primeiro o "x" inferior e, em seguida, usar o "X" superior.

Primeiro exemplo:

[root@jesc5161 home]# chmod a-rwx -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
d---------. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
d---------. 2 root root    6 Apr 22 21:41 accounting
----------. 1 user user    0 Apr 22 22:06 myfile1
----------. 1 user finance 0 Apr 22 22:12 myfile2
----------. 1 user user    0 Apr 22 22:12 myfile3

[root@jesc5161 home]# chmod u+x -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
d--x------. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
d--x------. 2 root root    6 Apr 22 21:41 accounting
---x------. 1 user user    0 Apr 22 22:06 myfile1
---x------. 1 user finance 0 Apr 22 22:12 myfile2
---x------. 1 user user    0 Apr 22 22:12 myfile3

Aqui só desejo dar permissão executável ao grupo, mas os arquivos também recebem permissões executáveis:

[root@jesc5161 home]# chmod g+X -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
d--x--x---. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
d--x--x---. 2 root root    6 Apr 22 21:41 accounting
---x--x---. 1 user user    0 Apr 22 22:06 myfile1
---x--x---. 1 user finance 0 Apr 22 22:12 myfile2
---x--x---. 1 user user    0 Apr 22 22:12 myfile3

Outro exemplo:

[root@jesc5161 home]# chmod a-rwx -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
d---------. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
d---------. 2 root root    6 Apr 22 21:41 accounting
----------. 1 user user    0 Apr 22 22:06 myfile1
----------. 1 user finance 0 Apr 22 22:12 myfile2
----------. 1 user user    0 Apr 22 22:12 myfile3

[root@jesc5161 home]# chmod u+rwx -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
drwx------. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
drwx------. 2 root root    6 Apr 22 21:41 accounting
-rwx------. 1 user user    0 Apr 22 22:06 myfile1
-rwx------. 1 user finance 0 Apr 22 22:12 myfile2
-rwx------. 1 user user    0 Apr 22 22:12 myfile3

Mais uma vez, desejo apenas dar permissão executável ao grupo, mas os arquivos também ganham permissões executáveis.

[root@jesc5161 home]# chmod g+X -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
drwx--x---. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
drwx--x---. 2 root root    6 Apr 22 21:41 accounting
-rwx--x---. 1 user user    0 Apr 22 22:06 myfile1
-rwx--x---. 1 user finance 0 Apr 22 22:12 myfile2
-rwx--x---. 1 user user    0 Apr 22 22:12 myfile3

Aqui está um exemplo em que "funciona", mas como você pode ver, NÃO usei "x" inferior antes de usar o "X" superior

[root@jesc5161 home]# chmod a-rwx -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
d---------. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
d---------. 2 root root    6 Apr 22 21:41 accounting
----------. 1 user user    0 Apr 22 22:06 myfile1
----------. 1 user finance 0 Apr 22 22:12 myfile2
----------. 1 user user    0 Apr 22 22:12 myfile3

[root@jesc5161 home]# chmod a+rw -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
drw-rw-rw-. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
drw-rw-rw-. 2 root root    6 Apr 22 21:41 accounting
-rw-rw-rw-. 1 user user    0 Apr 22 22:06 myfile1
-rw-rw-rw-. 1 user finance 0 Apr 22 22:12 myfile2
-rw-rw-rw-. 1 user user    0 Apr 22 22:12 myfile3

[root@jesc5161 home]# chmod g+X -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
drw-rwxrw-. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
drw-rwxrw-. 2 root root    6 Apr 22 21:41 accounting
-rw-rw-rw-. 1 user user    0 Apr 22 22:06 myfile1
-rw-rw-rw-. 1 user finance 0 Apr 22 22:12 myfile2
-rw-rw-rw-. 1 user user    0 Apr 22 22:12 myfile3

[root@jesc5161 home]# chmod o+X -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
drw-rwxrwx. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
drw-rwxrwx. 2 root root    6 Apr 22 21:41 accounting
-rw-rw-rw-. 1 user user    0 Apr 22 22:06 myfile1
-rw-rw-rw-. 1 user finance 0 Apr 22 22:12 myfile2
-rw-rw-rw-. 1 user user    0 Apr 22 22:12 myfile3

[root@jesc5161 home]# chmod u+X -R finance/

[root@jesc5161 home]# ll
total 4
drwxr-xr-x. 3 root root      17 May  2  2015 ec2-user
drwxrwxrwx. 3 root finance   65 Apr 22 22:12 finance
drwx------. 9 user user    4096 Apr 22 21:28 user

[root@jesc5161 home]# ll finance/
total 0
drwxrwxrwx. 2 root root    6 Apr 22 21:41 accounting
-rw-rw-rw-. 1 user user    0 Apr 22 22:06 myfile1
-rw-rw-rw-. 1 user finance 0 Apr 22 22:12 myfile2
-rw-rw-rw-. 1 user user    0 Apr 22 22:12 myfile3
    
por jes516 23.04.2016 / 05:35

1 resposta

3

+X means para definir o bit de execução:

if the file is a directory or if the current (unmodified) file mode bits have at least one of the execute bits (S_IXUSR, S_IXGRP, or S_IXOTH) set. It shall be ignored if the file is not a directory and none of the execute bits are set in the current file mode bits.

Depois de executar chmod -R _+x dir , o bit de execução é definido para pelo menos parte do usuário / grupo / outro para cada arquivo (que você tem permissão para modificar). Isso significa que -X se aplica a todos esses arquivos também.

Se você quiser apenas afetar os diretórios e não houver outros arquivos executáveis na árvore, poderá executar o comando +X antes de outras modificações. Caso contrário, você pode usar find :

find dir -type d -exec echo chmod g+x {} \+

Encontra todos os diretórios ( -type d ) em dir e executa echo chmod g+x em todos eles, com {} transformado em todos os caminhos.

    
por 23.04.2016 / 05:55