十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶(hù) + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專(zhuān)業(yè)推廣+無(wú)憂(yōu)售后,網(wǎng)站問(wèn)題一站解決
這篇文章將為大家詳細(xì)講解有關(guān)怎樣使用semanage管理SELinux安全策略,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

| 導(dǎo)讀 | Semanage是用于配置SE Linux策略某些元素而無(wú)需修改或重新編譯策略源的工具。 這包括將Linux用戶(hù)名映射到SELinux用戶(hù)身份以及對(duì)象(如網(wǎng)絡(luò)端口,接口和主機(jī))的安全上下文映射。 |
簡(jiǎn)介
Semanage是用于配置SELinux策略某些元素而無(wú)需修改或重新編譯策略源的工具。 這包括將Linux用戶(hù)名映射到SELinux用戶(hù)身份以及對(duì)象(如網(wǎng)絡(luò)端口,接口和主機(jī))的安全上下文映射。
實(shí)驗(yàn)環(huán)境
Centos7.7操作系統(tǒng)
Selinux已經(jīng)開(kāi)啟
開(kāi)啟方式:
[root@localhost ~]# sed -i '/^SELINUX/s/disabled/enforcing/g' /etc/selinux/config # 然后重啟一下操作系統(tǒng) [root@localhost ~]# reboot # 重啟完成之后檢查一下是否是enforcing模式 [root@localhost ~]# getenforce Enforcing
常用參數(shù)
port: 管理定義的網(wǎng)絡(luò)端口類(lèi)型
fcontext: 管理定義的文件上下文
-l: 列出所有記錄
-a: 添加記錄
-m: 修改記錄
-d: 刪除記錄
-t: 添加的類(lèi)型
-p: 指定添加的端口是tcp或udp協(xié)議的,port子 命令下使用
-e: 目標(biāo)路徑參考原路徑的上下文類(lèi)型,fcontext子 命令下使用
列出所有定義的端口
使用semanage port命令列出所有端口
[root@localhost ~]# semanage port -l SELinux Port Type Proto Port Number afs3_callback_port_t tcp 7001 afs3_callback_port_t udp 7001 afs_bos_port_t udp 7007 afs_fs_port_t tcp 2040 afs_fs_port_t udp 7000, 7005 afs_ka_port_t udp 7004 afs_pt_port_t tcp 7002 afs_pt_port_t udp 7002 afs_vl_port_t udp 7003 agentx_port_t tcp 705 agentx_port_t udp 705 amanda_port_t tcp 10080-10083 amanda_port_t udp 10080-10082 … …

列出指定的端口類(lèi)型的端口
[root@localhost ~]# semanage port -l|grep -w http_port_t http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000

通過(guò)查詢(xún)端口號(hào)來(lái)列出端口類(lèi)型
[root@localhost ~]# semanage port -l|grep -w 53 dns_port_t tcp 53 dns_port_t udp 53 [root@localhost ~]# semanage port -l|grep -w 20 ftp_data_port_t tcp 20 [root@localhost ~]# semanage port -l|grep -w 21 ftp_port_t tcp 21, 989, 990

創(chuàng)建、添加、修改端口
通過(guò)下面的命令為http添加新端口
[root@localhost ~]# [root@localhost ~]# semanage port -a -t http_port_t -p tcp 8888 [root@localhost ~]# # 查看新添加的端口 [root@localhost ~]# semanage port -l|grep -w 8888 http_port_t tcp 8888, 80, 81, 443, 488, 8008, 8009, 8443, 9000 # 也可以使用-C參數(shù)查看自定義的端口號(hào) [root@localhost ~]# semanage port -lC SELinux Port Type Proto Port Number http_port_t tcp 8888

添加一個(gè)范圍的端口
[root@localhost ~]# semanage port -a -t http_port_t -p tcp 11180-11188 [root@localhost ~]# [root@localhost ~]# semanage port -lC SELinux Port Type Proto Port Number http_port_t tcp 8888, 11180-11188

刪除端口
[root@localhost ~]# semanage port -d -t http_port_t -p tcp 8888 [root@localhost ~]# [root@localhost ~]# semanage port -d -t http_port_t -p tcp 11180-11188 [root@localhost ~]# # 查看一下,已經(jīng)沒(méi)有自定義的端口了 [root@localhost ~]# semanage port -lC

修改安全上下文
為samba共享目錄添加安全上下文
# 沒(méi)添加安全上下文之前是default_t [root@localhost ~]# ll -dZ /share/ drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /share/ [root@localhost ~]# semanage fcontext -a -t samba_share_t '/share(/.*)?' # 恢復(fù)文件默認(rèn)的安全上下文 [root@localhost ~]# restorecon -Rv /share restorecon reset /share context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:samba_share_t:s0 # 查看一下文件夾已經(jīng)變成samba_share_t了 [root@localhost ~]# ll -dZ /share drwxr-xr-x. root root unconfined_u:object_r:samba_share_t:s0 /share

為nfs共享目錄添加讀寫(xiě)
[root@localhost ~]# ll -dZ /nfsshare/ drwxr-xr-x. root root unconfined_u:object_r:default_t:s0 /nfsshare/ [root@localhost ~]# [root@localhost ~]# semanage fcontext -a -t public_content_rw_t '/nfsshare(/.*)?' [root@localhost ~]# restorecon -Rv /nfsshare [root@localhost ~]# ll -dZ /nfsshare/ drwxr-xr-x. root root unconfined_u:object_r:public_content_rw_t:s0 /nfsshare/

總結(jié)
本文講述了添加、修改和刪除端口,修改安全上下文。如果你的系統(tǒng)有安裝桌面,可以安裝圖形化管理軟件 policycoreutils-gui來(lái)進(jìn)行管理。
[root@localhost ~]# yum -y install policycoreutils-gui # system-config-selinux執(zhí)行該命令打開(kāi)圖形化管理界面 [root@localhost ~]# system-config-selinux

關(guān)于怎樣使用semanage管理SELinux安全策略就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。