uTools-Manuals/docs/linux/semanage.html
2019-04-21 11:50:48 +08:00

48 lines
3.6 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1 id="semanage">semanage</h1>
<p>默认目录的安全上下文查询与修改</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>semanage命令</strong> 是用来查询与修改SELinux默认目录的安全上下文。SELinux的策略与规则管理相关命令seinfo命令、sesearch命令、getsebool命令、setsebool命令、semanage命令。</p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">semanage {login|user|port|interface|fcontext|translation} -l
semanage fcontext -{a|d|m} [-frst] file_spec</code></pre>
<h3 id="选项">选项</h3>
<pre><code class="language-bash">-l查询。
fcontext主要用在安全上下文方面。
-a增加你可以增加一些目录的默认安全上下文类型设置。
-m修改。
-d删除。</code></pre>
<h3 id="实例">实例</h3>
<p>查询一下<code>/var/www/html</code>的默认安全性本文的设置:</p>
<pre><code class="language-bash">semanage fcontext -l
SELinux fcontext type Context
....(前面省略)....
/var/www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0
....(後面省略)....</code></pre>
<p>如上面例子所示,我们可以查询的到每个目录的安全性本文!而目录的设定可以使用正则表达式去指定一个范围。那么如果我们想要增加某些自定义目录的安全性本文呢?举例来说,我想要色设置<code>/srv/samba</code>成为 <code>public_content_t</code>的类型时,应该如何设置呢?</p>
<p>用semanage命令设置<code>/srv/samba</code>目录的默认安全性本文为<code>public_content_t</code></p>
<pre><code class="language-bash">mkdir /srv/samba
ll -Zd /srv/samba
drwxr-xr-x root root root:object_r:var_t /srv/samba</code></pre>
<p>如上所示,默认的情况应该是<code>var_t</code>这个咚咚的!</p>
<pre><code class="language-bash">semanage fcontext -l | grep &#39;/srv&#39;
/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 //看这里!</code></pre>
<p>上面则是默认的<code>/srv</code>底下的安全性本文资料,不过,并没有指定到<code>/srv/samba</code></p>
<pre><code class="language-bash">semanage fcontext -a -t public_content_t &quot;/srv/samba(/.*)?&quot;
semanage fcontext -l | grep &#39;/srv/samba&#39;
/srv/samba(/.*)? all files system_u:object_r:public_content_t:s0</code></pre>
<pre><code class="language-bash">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 #写入这个档案
</code></pre>
<pre><code class="language-bash">restorecon -Rv /srv/samba* #尝试恢复默认值
ll -Zd /srv/samba
drwxr-xr-x root root system_u:object_r:public_content_t /srv/samba/ #有默认值以后用restorecon命令来修改比较简单</code></pre>
<p>semanage命令的功能很多这里主要用到的仅有fcontext这个选项的用法而已。如上所示你可以使用semanage来查询所有的目录默认值也能够使用它来增加默认值的设置</p>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->