日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

文件的复制、移动、压缩等对SELinux属性关系详解

發布時間:2025/3/17 linux 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 文件的复制、移动、压缩等对SELinux属性关系详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文件的復制、移動、壓縮等對SELinux屬性關系詳解

1.臨時修改文件的類型屬性

文件的類型屬性不正確是常見的SELinux拒絕訪問的主要原因


1)修改文件的SELinux屬性:

[root@localhost ~]# touch test.file ? ##新建文件

[root@localhost ~]# ls -Z test.file ? ##查看文件的SELinux屬性

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 test.file

[root@localhost ~]# chcon -t samba_share_t test.file ?##修改文件的默認SELinux屬性

[root@localhost ~]# ls -Z test.file ??

-rw-r--r--. root root unconfined_u:object_r:samba_share_t:s0 test.file

[root@localhost ~]# restorecon -F -v test.file ? ##恢復修改過的SELinux屬性為默認屬性

restorecon reset /root/test.file context unconfined_u:object_r:samba_share_t:s0->system_u:object_r:admin_home_t:s0

[root@localhost ~]# ls -Z test.file?

-rw-r--r--. root root system_u:object_r:admin_home_t:s0 test.file


2)修改目錄的SELinux屬性(所有的操作與文件對比多了一個“-R”代表遞歸):

[root@localhost ~]# mkdir /web

[root@localhost ~]# touch /web/file{1,2}

[root@localhost ~]# ls -dZ /web ?##查看目錄的SELinux屬性

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /web

[root@localhost ~]# ls -lZ /web/ ? ##查看目錄下文件的SELinux屬性

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file2

[root@localhost ~]# chcon -R -t httpd_sys_content_t /web/ ? ?##臨時修改目錄的SELinux屬性為httpd_sys_content_t

[root@localhost ~]# ls -dZ /web/

drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 /web/

[root@localhost ~]# ls -lZ /web/

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file2

[root@localhost ~]# restorecon -R -v /web/ ? ##恢復為默認SELinux屬性

restorecon reset /web context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:default_t:s0

restorecon reset /web/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:default_t:s0

restorecon reset /web/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->unconfined_u:object_r:default_t:s0

[root@localhost ~]# ls -lZ /web/

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file2

[root@localhost ~]# ls -dZ /web/

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /web/


2.永久修改文件的類型屬性

永久修改文件及目錄的類型屬性使用下列命令:

semanage fcontext -{a|d|l|m} [-frst] filespec ?##-a增加、-d刪除、-l顯示、-m修改

restorecon -v filespec ? ?##由于“semanage fcontext”命令只是將屬性定義項加載到

“/etc/selinux/targeted/contexts/files/file_contexts.local”文件中,

使用該命令才是將文件的selinux屬性永久地修改.

1)文件的selinux屬性修改

[root@localhost tmp]# touch test.file

[root@localhost tmp]# ls -Z test.file?

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 test.file

[root@localhost tmp]# yum install policycoreutils-python ?##安裝semanage管理工具提供軟件

[root@localhost tmp]# semanage fcontext -a -t samba_share_t /tmp/test.file ?##將測試文件的selinux屬性設置為“samba_share_t”

[root@localhost tmp]# ls -Z /tmp/test.file ? ##測試文件的selinux屬性未發生變化

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 /tmp/test.file

[root@localhost tmp]# restorecon -v /tmp/test.file ? ?##使semanage設置的selinux屬性永久的生效

restorecon reset /tmp/test.file context unconfined_u:object_r:user_tmp_t:s0->unconfined_u:object_r:samba_share_t:s0

[root@localhost tmp]# ls -Z /tmp/test.file ? ##測試文件的selinux屬性已改

-rw-r--r--. root root unconfined_u:object_r:samba_share_t:s0 /tmp/test.file



2)目錄的selinux屬性修改:(完成后目錄下新建的文件自動繼承selinux屬性)

[root@localhost ~]# mkdir /html

[root@localhost ~]# touch /html/file{1,2}

[root@localhost ~]# ls -dZ /html

drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /html

[root@localhost ~]# ls -lZ /html/

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:default_t:s0 file2

[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t "/html(/.*)?" ?##正則表達式“/html(/.*)?”表示/html目錄及其中


的任何文件或子目錄

[root@localhost ~]# restorecon -R -v /html/ ?##使semanage設置的selinux屬性永久的生效

restorecon reset /html context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

restorecon reset /html/file2 context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

restorecon reset /html/file1 context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0

[root@localhost ~]# ls -lZ /html/ ? ##驗證修改結果

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 file2

[root@localhost ~]# ls -dZ /html/

drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 /html/


3)如果要恢復/html的文件屬性可使用下列命令:

[root@localhost ~]# semanage fcontext -d "/html(/.*)?" ? ##刪除自定義的selinux屬性

[root@localhost ~]# restorecon -F -R -v /html/ ?##永久生效

restorecon reset /html context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0

restorecon reset /html/file2 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0

restorecon reset /html/file1 context unconfined_u:object_r:httpd_sys_content_t:s0->system_u:object_r:default_t:s0

[root@localhost ~]# ls -lZ /html ?##驗證文件的selinux屬性已經從“httpd_sys_content_t”改為默認的"default_t"

-rw-r--r--. root root system_u:object_r:default_t:s0 ? file1

-rw-r--r--. root root system_u:object_r:default_t:s0 ? file2

[root@localhost ~]# ls -dZ /html

drwxr-xr-x. root root system_u:object_r:default_t:s0 ? /html


4)file_t與default_t

file_t:文件沒有selinux屬性

default_t:文件或目錄的selinux屬性與file-context配置文件定義的模式不匹配

兩種的類型的文件或目錄,受限制的域的程序均不能訪問


3.移動文件對selinux屬性的影響

在SELinux環境中,文件和目錄移動之后保持原有SELinux屬性不變

準備測試文件:

[root@localhost ~]# touch test.file

[root@localhost ~]# ls -Z test.file?

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 test.file

[root@localhost ~]# ls -dZ /var/www/html/

drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

移動測試:

[root@localhost ~]# mv test.file /var/www/html/

[root@localhost ~]# ls -Z /var/www/html/test.file ? ##移動后selinux屬性依然是“admin_home_t”,并未繼承“httpd_sys_content_t”

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /var/www/html/test.file


注意:

移動和復制對比中,復制更有利于保持文件的selinux屬性,以便能正常訪問。


4.檢測文件的默認屬性

利用matchpathcon,可以驗證目錄下的文件selinux屬性標記是否正確。

準備測試文件:

[root@localhost ~]# touch /var/www/html/file{1,2,3} ?##創建三個文件

[root@localhost ~]# ls -Z /var/www/html/file*

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/file1

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/file2

-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/html/file3


修改測試:

[root@localhost ~]# chcon -t samba_share_t /var/www/html/file1 ?##臨時修改selinux屬性

[root@localhost ~]# ls -Z /var/www/html/file1

-rw-r--r--. root root unconfined_u:object_r:samba_share_t:s0 /var/www/html/file1

[root@localhost ~]# matchpathcon -V /var/www/html/file? ? ##檢測selinux屬性的正確性,提示file1為“samba_share_t”應


為“httpd_sys_content_t”

/var/www/html/file1 has context unconfined_u:object_r:samba_share_t:s0, should be system_u:object_r:httpd_sys_content_t:s0

/var/www/html/file2 verified.

/var/www/html/file3 verified.


[root@localhost ~]# touch test.file ?

[root@localhost ~]# ls -Z

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 test.file

[root@localhost ~]# mv test.file /var/www/html/ ?##移動新文件測試

mv:是否覆蓋"/var/www/html/test.file"? y

[root@localhost ~]# ls /var/www/html/test.file ?-Z

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 /var/www/html/test.file

[root@localhost ~]# restorecon -v /var/www/html/* ? ##修復selinux屬性

restorecon reset /var/www/html/file1 context unconfined_u:object_r:samba_share_t:s0-


>unconfined_u:object_r:httpd_sys_content_t:s0

restorecon reset /var/www/html/test.file context unconfined_u:object_r:admin_home_t:s0-


>unconfined_u:object_r:httpd_sys_content_t:s0

[root@localhost ~]# matchpathcon -V /var/www/html/* ? ##提示verified表示校驗成功

/var/www/html/file1 verified.

/var/www/html/file2 verified.

/var/www/html/file3 verified.

/var/www/html/test.file verified.


5.tar文件與selinux屬性標記

tar命令不會保存屬于擴展屬性的selinux屬性,使用“--selinux”或“--xattrs”可保存selinux屬性信息。

準備對比步驟:

[root@localhost ~]# touch file{1..3} ?##創建文件

[root@localhost ~]# ls -Z

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file2

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file3

[root@localhost ~]# tar zcf test.tar.gz ./file* ? ##不加“--selinux”選項

[root@localhost ~]# cp test.tar.gz /tmp/ ?##拷貝文件

[root@localhost ~]# cd /tmp/ ? ##進入/tmp目錄

[root@localhost tmp]# tar zxvf test.tar.gz ? ##解壓

./file1

./file2

./file3

[root@localhost tmp]# ls -Z ? ##驗證,selinux屬性丟失

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 file1

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 file2

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 file3

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 test.tar.gz

保留selinux屬性:

[root@localhost tmp]# cd

[root@localhost ~]# touch file{4..5}?

[root@localhost ~]# ls -Z file{4..5}

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file4

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file5

[root@localhost ~]# tar zcf test.se.tar.gz ./file{4..5} --selinux ? ##壓縮是保留selinux屬性標記

[root@localhost ~]# cp test.se.tar.gz /tmp/

[root@localhost ~]# cd /tmp/

[root@localhost tmp]# tar zxvf test.se.tar.gz?

./file4

./file5

[root@localhost tmp]# ls -Z ?##驗證file1、2、3丟失了原有的selinux屬性,file4、5保留了“admin_home_t”的selinux屬性;請思考:


test開頭的兩壓縮文件為什么是這樣的selinux屬性?

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 file1 ? ?

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 file2

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 file3

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file4

-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file5

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 test.se.tar.gz

-rw-r--r--. root root unconfined_u:object_r:user_tmp_t:s0 test.tar.gz


轉載于:https://blog.51cto.com/stlong/1559628

總結

以上是生活随笔為你收集整理的文件的复制、移动、压缩等对SELinux属性关系详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。