文件的复制、移动、压缩等对SELinux属性关系详解
文件的復(fù)制、移動、壓縮等對SELinux屬性關(guān)系詳解
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 ?##修改文件的默認(rèn)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 ? ##恢復(fù)修改過的SELinux屬性為默認(rèn)屬性
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/ ? ##恢復(fù)為默認(rèn)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屬性設(shè)置為“samba_share_t”
[root@localhost tmp]# ls -Z /tmp/test.file ? ##測試文件的selinux屬性未發(fā)生變化
-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設(shè)置的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(/.*)?" ?##正則表達(dá)式“/html(/.*)?”表示/html目錄及其中
的任何文件或子目錄
[root@localhost ~]# restorecon -R -v /html/ ?##使semanage設(shè)置的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/ ? ##驗證修改結(jié)果
-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)如果要恢復(fù)/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屬性已經(jīng)從“httpd_sys_content_t”改為默認(rèn)的"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環(huán)境中,文件和目錄移動之后保持原有SELinux屬性不變
準(zhǔn)備測試文件:
[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
注意:
移動和復(fù)制對比中,復(fù)制更有利于保持文件的selinux屬性,以便能正常訪問。
4.檢測文件的默認(rèn)屬性
利用matchpathcon,可以驗證目錄下的文件selinux屬性標(biāo)記是否正確。
準(zhǔn)備測試文件:
[root@localhost ~]# touch /var/www/html/file{1,2,3} ?##創(chuàng)建三個文件
[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”應(yīng)
為“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/* ? ##修復(fù)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屬性標(biāo)記
tar命令不會保存屬于擴(kuò)展屬性的selinux屬性,使用“--selinux”或“--xattrs”可保存selinux屬性信息。
準(zhǔn)備對比步驟:
[root@localhost ~]# touch file{1..3} ?##創(chuàng)建文件
[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/ ? ##進(jìn)入/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屬性標(biāo)記
[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
轉(zhuǎn)載于:https://blog.51cto.com/stlong/1559628
總結(jié)
以上是生活随笔為你收集整理的文件的复制、移动、压缩等对SELinux属性关系详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tomcat目录和文件讲解
- 下一篇: Unity3D 渲染统计窗口