Linux

如何在文件/文件夾權限中添加點遞歸

  • January 1, 2019

具有權限的文件範例

-rwxr-xr-x. 1 root root  659 Jan  4  2018 zookeeper-server-initialize
-rwxr-xr-x. 1 root root  649 Jan  4  2018 zookeeper-server-cleanup
-rwxr-xr-x. 1 root root  648 Jan  4  2018 zookeeper-server
-rwxr-xr-x. 1 root root  303 Jan  4  2018 zookeeper-client
-rwxr-xr-x. 1 root root 6822 Jan  4  2018 zkServer.sh
-rwxr-xr-x. 1 root root   12 Jan  4  2018 zkServer-initialize.sh
-rwxr-xr-x. 1 root root 2840 Jan  4  2018 zkEnv.sh
-rwxr-xr-x. 1 root root 1709 Jan  4  2018 zkCli.sh
-rwxr-xr-x. 1 root root 2155 Jan  4  2018 zkCleanup.sh

如果我們只有這個文件,例如:(沒有點)

-rwxr-xr-x 1 root root 2155 Jan  4  2018 zkCleanup.sh

那麼將這個點添加到文件權限的命令是什麼?

第二個如何添加點遞歸

例如在所有子文件夾下

/usr/hdp/current/zookeeper-server

之前的例子

pwd
/usr/hdp/current/zookeeper-server


ls -Z
drwxr-xr-x root root ?                                bin
lrwxrwxrwx root root ?                                conf -> /etc/zookeeper/2.6.4.0-91/0
drwxr-xr-x root root ?                                doc
drwxr-xr-x root root ?                                etc
drwxr-xr-x root root ?                                lib
drwxr-xr-x root root ?                                man
drwxr-xr-x root root ?                                usr
-rw-r--r-- root root ?                                zookeeper-3.4.6.2.6.4.0-91.jar
lrwxrwxrwx root root ?                                zookeeper.jar -> zookeeper-3.4.6.2.6.4.0-91.jar

預期輸出:

pwd
/usr/hdp/current/zookeeper-server

ls -Z
drwxr-xr-x. root root system_u:object_r:bin_t:s0       bin
lrwxrwxrwx. root root unconfined_u:object_r:usr_t:s0   conf -> /etc/zookeeper/2.6.4.0-91/0
drwxr-xr-x. root root system_u:object_r:usr_t:s0       doc
drwxr-xr-x. root root system_u:object_r:usr_t:s0       etc
drwxr-xr-x. root root system_u:object_r:lib_t:s0       lib
drwxr-xr-x. root root system_u:object_r:usr_t:s0       man
drwxr-xr-x. root root system_u:object_r:usr_t:s0       usr
-rw-r--r--. root root system_u:object_r:usr_t:s0       zookeeper-3.4.6.2.6.4.0-91.jar
lrwxrwxrwx. root root system_u:object_r:usr_t:s0       zookeeper.jar -> zookeeper-3.4.6.2.6.4.0-91.jar

我們試圖

restorecon -r /usr/hdp
ls -ltr
total 12
drwxr-xr-x. 27 root root 4096 Oct 29 10:05 2.6.0.3-8
drwxr-xr-x  29 root root 4096 Nov  6 11:29 2.6.4.0-91
drwxr-xr-x.  2 root root 4096 Dec 31 13:01 current

但是 2.6.4.0-91 文件夾具有相同的權限(包括 2.6.4.0-91 下的子文件夾)

ls -Z
drwxr-xr-x. root root system_u:object_r:usr_t:s0       2.6.0.3-8
drwxr-xr-x  root root ?                                2.6.4.0-91
drwxr-xr-x. root root unconfined_u:object_r:usr_t:s0   current

尾部.表示文件具有 SElinux 安全上下文。要設置預設安全上下文,您應該能夠執行

restorecon -r /usr/hdp 

以上要求您在系統上啟用 SELinux。如果不這樣做,則必須啟用它並標記文件系統

編輯 /etc/selinux/config 並確保

SELINUX=enforcing # or permissive
SELINUXTYPW=targeted

然後執行命令

touch /.autorelabel
reboot

引用自:https://serverfault.com/questions/947214