selinux配置問題

setsebool命令html

配置VSFTPD時本地用戶沒法切換linux

用到命令web

setsebool -P allow_ftpd_anon_write=1 allow_ftpd_full_access=1 ftp_home_dir=1 ftpd_disable_trans=1 ftpd_is_daemon=1apache

setsebool命令查詢了相關的資料安全

是selinux的相關設置項bash

這是SELinux的設置命令.服務器

其實 man 一下setsebool也知道了:網絡

NAMEdom

  setsebool – set SELinux boolean valueide

在不熟悉SELnux前,把SELinux關掉也能夠的。有時間研究下SELinux。

Redhat從FC3開始,就使用了SELinux來加強安全,可是使用起來有時候太繁瑣,就想關閉它,可是若是安裝服務器的時候開始沒有圖形界面,還真很難找到地方能夠關閉。

正確方法以下:修改/etc/selinux/config文件中的SELINUX=」" 爲 disabled ,而後重啓。

2009年8。22更新


下面是一些 收集來的 設置命令

===ftp===

//If you want to share files anonymously

chcon -R -t public_content_t /var/ftp

//If you want to setup a directory where you can upload files

chcon -t public_content_rw_t /var/ftp/incoming

You must also turn on the boolean allow_ftpd_anon_write

setsebool -P allow_ftpd_anon_write=1

//If you are setting up this machine as a ftpd server and wish to allow users to access their home directorories

setsebool -P ftp_home_dir 1

//If you want to run ftpd as a daemon

setsebool -P ftpd_is_daemon 1

//You can disable SELinux protection for the ftpd daemon

setsebool -P ftpd_disable_trans 1

===httpd===

//If you want a particular domain to write to the public_content_rw_t domain

setsebool -P allow_httpd_anon_write=1

or

setsebool -P allow_httpd_sys__anon_write=1

//httpd can be setup to allow cgi s to be executed

setsebool -P httpd_enable_cgi 1

//If you want to allow access to users home directories

setsebool -P httpd_enable_homedirs 1

chcon -R -t httpd_sys_content_t ~user/public_html

//httpd is allowed access to the controling terminal

setsebool -P httpd_tty_comm 1

//such that one httpd service can not interfere with another

setsebool -P httpd_unified 0

//loadable modules run under the same context as httpd

setsebool -P httpd_builtin_ing 0

//httpd s are allowed to connect out to the network

setsebool -P httpd_can_network_connect 1

// You can disable suexec transition

setsebool -P httpd_suexec_disable_trans 1

//You can disable SELinux protection for the httpd daemon by executing

setsebool -P httpd_disable_trans 1

service httpd restart

===named===

//If you want to have named update the master zone files

setsebool -P named_write_master_zones 1

//You can disable SELinux protection for the named daemon by executing

setsebool -P named_disable_trans 1

service named restart

===nfs===

//If you want to setup this machine to share nfs partitions read only

setsebool -P nfs_export_all_ro 1

//If you want to share files read/write

setsebool -P nfs_export_all_rw 1

//If you want to use a remote NFS server for the home directories on this machine

setsebool -P use_nfs_home_dirs 1

===samba===

//If you want to share files other than home directorie

chcon -t samba_share_t /directory

//If you want to share files with multiple domains

setsebool -P allow_smbd_anon_write=1

//If you are setting up this machine as a Samba server and wish to share the home directories

setsebool -P samba_enable_home_dirs 1

//If you want to use a remote Samba server for the home directories on this machine

setsebool -P use_samba_home_dirs 1

//You can disable SELinux protection for the samba daemon by executing

setsebool -P smbd_disable_trans 1

service smb restart

===rsync===

//If you want to share files using the rsync daemon

chcon -t public_content_t /directories

//If you want to share files with multiple domains

setsebool -P allow_rsync_anon_write=1

//You can disable SELinux protection for the rsync daemon by executing

setsebool -P rsync_disable_trans 1

===kerberos===

//allow your system to work properly in a Kerberos environment

setsebool -P allow_kerberos 1

//If you are running Kerberos daemons kadmind or krb5kdc

setsebool -P krb5kdc_disable_trans 1

service krb5kdc restart

setsebool -P kadmind_disable_trans 1

service kadmind restart

===nis===

Allow your system to work properly in a NIS environment

setsebool -P allow_ypbind 1


1.1 SElinux概述

SELinux(Security-Enhanced Linux) 是美國國家安全局(NAS)對於強制訪問控 制的實現,在這種訪問控制體系的限制下,進程只能訪問那些在他的任務中所須要文件。大部分使用 SELinux 的人使用的都是SELinux就緒的發行版,例如 Fedora、Red Hat Enterprise Linux (RHEL)、Debian 或 Gentoo。它們都是在內核中啓用 SELinux 的,而且提供一個可定製的安全策略,還提供不少用戶層的庫和工具,它們均可以使用 SELinux 的功能。

1.1.1 SElinux特色

1.MAC

對訪問的控制完全化,對全部的文件、目錄、端口的訪問都是基於策略設定的,可由管理員時行設定。

2.RBAC

對於用戶只賦予最小權限。用戶被劃分紅了一些role(角色),即便是root用戶,若是不具備sysadm_r角色的話,也不是執行相關的管理。哪裏role能夠執行哪些domain,也是能夠修改的。

3.安全上下文

當啓動selinux的時候,全部文件與對象都有安全上下文。進程的安全上下文是域,安全上下文由用戶:角色:類型表示。

(1)系統根據pam子系統中的pam_selinux.so模塊設定登陸者運行程序的安全上下文

(2)rpm包安裝會根據rpm包內記錄來生成安全上下文,

(3)若是是手工他建的,會根據policy中規定來設置安全上下文,

(4)若是是cp,會從新生成安全上下文。

(5)若是是mv,安全上下文不變。

1.1.2 安全上下文格式

安全上下文由user:role:type三部分組成,下面分別說明其做用:

1.user identity:相似linux系統中的UID,提供身份識別,安全上下文中的一部分。

三種常見的user:

user_u-:   普通用戶登陸系統後預設;

system_u-:開機過程當中系統進程的預設;

root-:    root登陸後預設;

在targeted policy中users不是很重要;

在strict policy中比較重要,的有預設的selinux users都以 "_u"結尾,root除外。

2.role

文件與目錄的role,一般是object_r;

程序的role,一般是system_r;

用戶的role,targeted policy爲system_r;

strict policy爲sysadm_r,staff_r,user_r

用戶的role,相似於系統中的GID,不一樣的角色具有不一樣的權限;用戶能夠具有多個role;可是同一時間內只能使用一role;

role是RBAC的基礎;

3.type

type:用來將主體與客體劃分爲不一樣的組,組每一個主體和系統中的客體定義了一個類型;爲進程運行提供最低的權限環境。

當一個類型與執行的進程關聯時,該type也稱爲domain,也叫安全上下文。

域或安全上下文是一個進程容許操做的列表,決字一個進程能夠對哪一種類型進行操做。

1.1.3 SElinux配置文件

vi /etc/selinux/config

# This file controls the state of SELinux on the system.

# SELINUX= can take one of these three values:

# enforcing - SELinux security policy is enforced.

# permissive - SELinux prints warnings instead of enforcing.

# disabled - SELinux is fully disabled.

SELINUX=enforcing

#SELINUX=disabled

# SELINUXTYPE= type of policy in use. Possible values are:

# targeted - Only targeted network daemons are protected.

# strict - Full SELinux protection.

SELINUXTYPE=targeted

#SELINUX有「disabled」「permissive」,「enforcing」3種選擇。

1.模式的設置

enforcing:強制模式,只要selinux不容許,就沒法執行

permissive:警告模式,將該事件記錄下來,依然容許執行

disabled:關閉selinux;停用,啓用須要重啓計算機。


2.策略的設置

targeted:保護常見的網絡服務,是selinux的默認值;

stric:提供RBAC的policy,具有完整的保護功能,保護網絡服務,通常指令及應用程序。

策略改變後,須要從新啓動計算機。

也能夠經過命令來修改相關的具體的策略值,也就是修改安全上下文,來提升策略的靈活性。


3.策略的位置

/etc/selinux/<策略名>/policy/


1.2 SElinux命令介紹

1.2.1 查詢SElinux狀態命令

1.查詢selinux狀態

[root@redhat ~]# sestatus

SELinux status:                 enabled

SELinuxfs mount:                /selinux

Current mode:                   enforcing

Mode from config file:          enforcing

Policy version:                 21

Policy from config file:        targeted


2.查詢selinux激活狀態

[root@redhat ~]# selinuxenabled

[root@redhat ~]# echo $?

0

若是爲-256爲非激活狀態。

1.2.2 切換SElinux類型

1.切換成警告模式

[root@redhat ~]# setenforce 0或setenforce permissive

[root@redhat ~]# sestatus

SELinux status:                 enabled

SELinuxfs mount:                /selinux

Current mode:                   permissive

Mode from config file:          enforcing

Policy version:                 21

Policy from config file:        targeted

[root@redhat ~]# getenforce

Permissive


2.切換成強制模式

[root@redhat ~]# setenforce 1

[root@redhat ~]# getenforce

Enforcing

注:使用setenforce切換enforcing與permissive模式不須要重啓計算機。

1.2.3 檢查安全上下文

1.檢查賬號的安全上下文

[root@redhat ~]# id -Z

root:system_r:unconfined_t:SystemLow-SystemHigh


2.檢查進程的安全上下文

[root@redhat home]# ps -Z

LABEL                             PID TTY          TIME CMD

root:system_r:unconfined_t:SystemLow-SystemHigh 2383 pts/0 00:00:00 bash

root:system_r:unconfined_t:SystemLow-SystemHigh 2536 pts/0 00:00:00 ps


3.檢查文件與目錄的安全上下文

[root@redhat home]# ls -Z

drwx------  tom  tom  system_u:object_r:user_home_dir_t tom

1.2.4 修改文件/目錄安全上下文與策略

1.chcon命令

chcon -u [user]  對象

     -r [role]

     -t [type]

     -R 遞歸

示例:

chcon -R -t samba_share_t /tmp/abc

注:安全上下文的簡單理解說明,受到selinux保護的進程只能訪問標識爲本身只夠訪問的安全上下文的文件與目錄。

例如:上面解釋爲使用smb進程可以訪問/tmp/abc目錄而設定的安全上下文。


2.getsebool命令

獲取本機selinux策略值,也稱爲bool值。

getsebool -a  命令同sestatus -b

[root@redhat files]# getsebool -a

NetworkManager_disable_trans --> off

allow_cvs_read_shadow --> off

allow_daemons_dump_core --> on

allow_daemons_use_tty --> off

allow_execheap --> off

allow_execmem --> on

allow_execmod --> off

allow_execstack --> on

allow_ftpd_anon_write --> off   /*是否容許ftp匿名訪問*/

allow_ftpd_full_access --> off

...

httpd_disable_trans --> off    /*只要有disable_trans關閉保護*/


說明:selinux的設置通常經過兩個部分完成的,一個是安全上下文,另外一個是策略,策略值是對安全上下文的補充。


3.setsebool命令

setsebool -P allow_ftpd_anon_write=1

-P 是永久性設置,不然重啓以後又恢復預設值。

示例:

[root@redhat files]# setsebool -P allow_ftpd_anon_write=1

[root@redhat files]# getsebool allow_ftpd_anon_write

allow_ftpd_anon_write --> on

說明:若是僅僅是安全上下文中設置了vsftpd進程對某一個目錄的訪問,配置文件中也容許可寫,可是selinux中策略中不容許可寫,仍然不可寫。因此基於selinux保護的服務中,安全性要高於不少。


1.3 SElinux應用

selinux的設置分爲兩個部分,修改安全上下文以及策略,下面收集了一些應用的安全上下文,供配置時使用,對於策略的設置,應根據服務應用的特色來修改相應的策略值。

1.3.1 SElinux與samba

1.samba共享的文件必須用正確的selinux安全上下文標記。

chcon -R -t samba_share_t /tmp/abc

若是共享/home/abc,須要設置整個主目錄的安全上下文。

chcon -R -r samba_share_t /home

2.修改策略(只對主目錄的策略的修改)

setsebool -P samba_enable_home_dirs=1

setsebool -P allow_smbd_anon_write=1

getsebool 查看

samba_enable_home_dirs -->on

allow_smbd_anon_write --> on /*容許匿名訪問而且可寫*/


1.3.2 SElinux與nfs

selinux對nfs的限制好像不是很嚴格,默認狀態下,不對nfs的安全上下文進行標記,並且在默認狀態的策略下,nfs的目標策略容許nfs_export_all_ro

nfs_export_all_ro

nfs_export_all_rw值爲0

因此說默認是容許訪問的。

可是若是共享的是/home/abc的話,須要打開相關策略對home的訪問。

setsebool -P use_nfs_home_dirs boolean 1

getsebool use_nfs_home_dirs


1.3.3 SElinux與ftp

1.若是ftp爲匿名用戶共享目錄的話,應修改安全上下文。

chcon -R -t public_content_t /var/ftp

chcon -R -t public_content_rw_t /var/ftp/incoming


2.策略的設置

setsebool -P allow_ftpd_anon_write =1

getsebool allow_ftpd_anon_write

allow_ftpd_anon_write--> on


1.3.4 SElinux與http

apache的主目錄若是修改成其它位置,selinux就會限制客戶的訪問。

1.修改安全上下文:

chcon -R -t httpd_sys_content_t /home/html

因爲網頁都須要進行匿名訪問,因此要容許匿名訪問。

2.修改策略:

setsebool -P allow_ftpd_anon_write = 1

setsebool -P allow_httpd_anon_write = 1

setsebool -P allow_<協議名>_anon_write = 1

關閉selinux對httpd的保護

httpd_disable_trans=0


1.3.5 SElinux與公共目錄共享

若是ftp,samba,web都訪問共享目錄的話,該文件的安全上下文應爲:

public_content_t

public_content_rw_t

其它各服務的策略的bool值,應根據具體狀況作相應的修改。


1.3.6 SElinux配置總結

以上內容的selinux的配置實驗還須要進行相關驗證,以便在實際環境中可以直接應用,相關的內容還須要繼續補充。

對於多於牛毛的策略,能夠用過濾還查看一個服務至關開啓哪些策略

相關文章
相關標籤/搜索