linux-command
Version:
85 lines (63 loc) • 3.09 kB
Markdown
semanage
===
默认目录的安全上下文查询与修改
## 补充说明
**semanage命令** 是用来查询与修改SELinux默认目录的安全上下文。SELinux的策略与规则管理相关命令:seinfo命令、sesearch命令、getsebool命令、setsebool命令、semanage命令。
### 语法
```shell
semanage {login|user|port|interface|fcontext|translation} -l
semanage fcontext -{a|d|m} [-frst] file_spec
```
### 选项
```shell
-l:查询。
fcontext:主要用在安全上下文方面。
-a:增加,你可以增加一些目录的默认安全上下文类型设置。
-m:修改。
-d:删除。
```
### 实例
查询一下`/var/www/html`的默认安全性本文的设置:
```shell
semanage fcontext -l
SELinux fcontext type Context
....(前面省略)....
/var/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0
....(後面省略)....
```
如上面例子所示,我们可以查询的到每个目录的安全性本文!而目录的设定可以使用正则表达式去指定一个范围。那么如果我们想要增加某些自定义目录的安全性本文呢?举例来说,我想要色设置`/srv/samba`成为 `public_content_t`的类型时,应该如何设置呢?
用semanage命令设置`/srv/samba`目录的默认安全性本文为`public_content_t`:
```shell
mkdir /srv/samba
ll -Zd /srv/samba
drwxr-xr-x root root root:object_r:var_t /srv/samba
```
如上所示,默认的情况应该是`var_t`这个咚咚的!
```shell
semanage fcontext -l | grep '/srv'
/srv/.* all files system_u:object_r:var_t:s0
/srv/([^/]*/)?ftp(/.*)? all files system_u:object_r:public_content_t:s0
/srv/([^/]*/)?www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0
/srv/([^/]*/)?rsync(/.*)? all files system_u:object_r:public_content_t:s0
/srv/gallery2(/.*)? all files system_u:object_r:httpd_sys_content_t:s0
/srv directory system_u:object_r:var_t:s0 //看这里!
```
上面则是默认的`/srv`底下的安全性本文资料,不过,并没有指定到`/srv/samba`。
```shell
semanage fcontext -a -t public_content_t "/srv/samba(/.*)?"
semanage fcontext -l | grep '/srv/samba'
/srv/samba(/.*)? all files system_u:object_r:public_content_t:s0
```
```shell
cat /etc/selinux/targeted/contexts/files/file_contexts.local
# This file is auto-generated by libsemanage
# Please use the semanage command to make changes
/srv/samba(/.*)? system_u:object_r:public_content_t:s0 #写入这个档案
```
```shell
restorecon -Rv /srv/samba* #尝试恢复默认值
ll -Zd /srv/samba
drwxr-xr-x root root system_u:object_r:public_content_t /srv/samba/ #有默认值,以后用restorecon命令来修改比较简单!
```
semanage命令的功能很多,这里主要用到的仅有fcontext这个选项的用法而已。如上所示,你可以使用semanage来查询所有的目录默认值,也能够使用它来增加默认值的设置!
<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ -->