Linux 系統安全SELinux講解配置

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.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

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

SELinux安全
一、SELinux配置文件
在CentOS 7系統中部署SELinux非常簡單,由於SELinux已經作爲模塊集成到內核中,默認SELinux已經處於激活狀態。對管理員來說,更多的是需要配置與管理SELinux,CentOS 7系統中SELinux全局配置文件爲/etc/sysconfig/selinux,內容如下:
[[email protected] ~]# vim /etc/sysconfig/selinux

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 - No SELinux policy is loaded.

SELINUX=enforcing

SELINUXTYPE= can take one of these two values:

targeted - Targeted processes are protected,

mls - Multi Level Security protection.

SELINUXTYPE=targeted
SELinux=enforcing爲SELinux總開關,有效值可以是enforcing、permissive或disabled。
其中,disabled代表禁用SELinux功能,由於SELinux是內核模塊功能,所以如果設置禁用,需要重啓計算機。permissive代表僅警告模式,處於此狀態下時,當主題程序試圖訪問無權限的資源時,SELinux會記錄日誌但不會攔截該訪問,也就是最終訪問是成功的,只是在SELinux日誌中記錄而已。enforcing模式代表強制開啓,SELinux會攔截非法的資源訪問並記錄相關日誌。
使用setenforce可以臨時在enforcing模式與permissive模式之間切換,切換會被立刻應用於當前系統,計算機重啓後無效,永久修改模式需要修改配置文件。
[[email protected] ~]# setenforce 0 #設置SELinux爲permissive模式
[[email protected] ~]# setenforce 1 #設置SELinux爲enforcing模式

6.2.6 查看與修改布爾值
SELinux布爾值可以實時被修改。如,你可以在不重新加載或編譯SELinux策略的情況下允許服務訪問NFS文件系統。getsebool是用來查看SELinux布爾值的命令,用法比較簡單,-a選項用來查看所有的布爾值。一般建議管理員通過管道過濾自己需要的布爾值參數,如getsebool -a |grep ftp過濾與FTP相關的布爾值信息,顯示效果中左側爲關鍵詞,右側爲開關,on代表開,off代表關,具體命令如下。
[[email protected] ~]# getsebool -a
abrt_anon_write off
abrt_handle_event off
allow_console_login on
allow_cvs_read_shadow off
allow_daemons_dump_core on
allow_daemons_use_tcp_wrapper off
allow_daemons_use_tty on
allow_domain_fd_use on
… …部分內容省略… …
修改SELinux布爾值狀態也非常簡單,使用setsebool name X即可實現。其中,name是布爾值名稱,X代表on或off。默認setsebool命令修改的布爾值參數會立即生效,但計算機重啓後會被還原,如果希望永久修改,需要使用-p參數。
[[email protected] ~]# setsebool ftp_home_dir on
[[email protected] ~]# setsebool -p ftp_home_dir on

可以執行以下命令
setsebool allow_ftpd_full_access 1
setsebool allow_ftpd_use_cifs 1
setsebool allow_ftpd_use_nfs 1
setsebool ftp_home_dir 1
setsebool httpd_enable_ftp_server 1
setsebool tftp_anon_write 1

setenforce: SELinux is disabled解決辦法
2014-10-21 17:01 來源: IT技術學習網 閱讀: 19674
如果在使用setenforce命令設置selinux狀態的時候出現這個提示:setenforce: SELinux is disabled
那麼說明selinux已經被徹底的關閉了
如果需要重新開啓selinux,請按下面步驟:
vi /etc/selinux/config
更改爲:SELINUX=1
必須重啓linux,不重啓是沒辦法立刻開啓selinux的
重啓完以後,使用getenforce,setenforce等命令就不會報“setenforce: SELinux is disabled”了
這時,我們就可以用setenforce命令來動態的調整當前是否開啓selinux。

★★ 暫時性的關掉或開啟 selinux ★★

getenforceEnforcing sudo setenforce 0
getenforcePermissive sudo setenforce 1
$ getenforce
Enforcing

★★ 永久性的關掉 selinux ★★
$ sudo vi /etc/sysconfig/selinux
找到
SELINUX=enforcing
然後修改為
SELINUX=disabled
要重新開機 reboot / restart 後才會套用

Linux中FTP的使用
轉載 2015年09月28日 14:32:43
• 323
轉自:http://blog.163.com/fankb222@126/blog/static/7117402320124743024279/
1、首先安裝vsftpd,如果未安裝,則執行yum -y install vsftpd,這樣將會自動在網上down and setup
vsftpd。
2、創建ftp 用戶組及用戶:
# groupadd ftpgroup
# useradd ftpuser -g ftpgroup -d /ftp -m
# passwd ftpuser
輸入密碼2遍
/ftp是ftp 用戶訪問的文件夾
3、在windows中cmd執行ftp IP顯現
500 OOPS: vsftpd: cannot locate user specified in ‘ftp_username’:ftp
的錯誤消息
需要在vsftpd.conf中加入了ftp_username=ftpuser(用戶)這一行,ftp_username的缺省用戶應該是ftp

4、修改vi /etc/vsftpd/vsftpd.conf 文件 將下面的註釋去掉
Anon_upload_enable=yes
Anon_mkdir_write_enable=yes
Write_enable=yes
5、/etc/init.d/vsftpd restart後成功登陸
如果出現“550 create directory operation failed”
是SELinux安裝機制搞的鬼.只要disable SELinux就可以了.

vi /etc/selinux/config

將 SELINUX=XXX –>XXX 代表級別
改爲
SELINUX=disabled
或者setsebool -P ftpd_disable_trans on 或者 setsebool -P ftp_home_dir on
如果不能執行,提示“
Could not change active booleans: Invalid boolean

可以執行以下命令
setsebool allow_ftpd_full_access 1
setsebool allow_ftpd_use_cifs 1
setsebool allow_ftpd_use_nfs 1
setsebool ftp_home_dir 1
setsebool httpd_enable_ftp_server 1
setsebool tftp_anon_write 1

6、 service vsftpd restart

當然,我們現在知道這個問題是由於SELinux引起的,但還不知其所以然,實際上問題的原因此時已經被audit進程記錄到了相應的日誌裏,可以這樣查看:
shell> audit2why < /var/log/audit/audit.log

5.啓動vsftp服務器
service vsftpd start 開啓命令
service vsftpd restart 重啓
service vsftpd stop 停止

關於CentOS/RHEL中SELinux的相關學習記錄整理
linux入門教程 memory 發佈於December 19, 2016 標籤: CentOS, Linux
很多運維人出於方便,將seLinux默認給關閉了,我以前也這樣幹,後來發現,在內網這樣乾的確挺省事兒,但公網服務器這樣幹了,的確有點SB,所以這兒分享一些關於SELinux的學習筆記的整理文檔
配置文件路徑
/etc/sysconfig/seLinux
SELinux配置文件中可修改項有兩項
開機的三種狀態
SELINUX = Enforcing (默認)

Disabled : 停用SELinux功能
Permissive : 僅顯示警告信息,不阻止
Enforcing : 強制執行SELinux功能,產生警告信息
SELINUXTYPE=targeted

strict : 完整的保護功能,包含網絡服務,一般指令及應用程序
targeted : 保護網絡相關服務
網絡相關服務包括
dhcpd
httpd
mysqld
named
nscd
ntpd
portmap
postgres
snmpd
squid
syslogd
SELinux相關命令
sestatus #查看SELinux的狀態
sestatus -b#查看application管理狀態
getenforce #顯示目前SELinux工作模式
setenforce 0#臨時將SELinux模式設爲寬容模式
setenforce 1#臨時將SELinux模式設爲強制模式
注:setenforce必需在SELinux enable狀態纔有用
ls -Z 文檔名#查看SELinux對檔案管理狀態
ls -dZ 資料夾#查看SELinux對資料夾管理狀態
chcon#變更檔案、資料夾管理
getsebool#查看application管理狀態
setsebool#變更application管理
restorecon#恢復成原有的SELinux type
semanage#安全性文件的查詢與修改
semanage port -l |grep http#查看SELinux管理http套件所使用的端口
semanage port -a -t http_port_t -p tcp 90#加入90端口給httpd使用
注:必需安裝policycoreutils-python套件,在加入Port時需先檢查該port是否已經被使用了。
seinfo#查看政策中規則數量
注:必需安裝setools-console套件
sealert -l #SELinux錯誤信息詳細列出,爲錯誤代碼。
注:必需安裝setroubleshoot及setroubleshoot-server套件,安裝完後需要重啓系統。
查看SELinux狀態
sestatus

getenforce
SELinux 使用思路 在SELinux開啓的情況下,配置vsftpd服務
1】設置SELinux 允許模式
setenforce Permissive
2】安裝啓動vsftpd 服務
yum install vsftpd -y
service vsftpd start
3】測試
匿名登錄並下載
使用本地用戶登錄,並下載,上傳
4】設置SELinux 強制模式
setenforce Enforcing
5】再次測試相關功能
匿名用戶登錄並下載 仍然可以
使用本地用戶登錄,並下載,上傳 — 被拒絕
6】配置SELinux,放行需要的功能
6.1】方法一 看日誌
正常情況下 日誌會在服務器的 /var/log/messages 文件中存在
還會在 /var/log/audit/audit.log 存在
查看日誌
tail /var/log/messages
sealert -l xxxxxxxxxxxxxxxxxxxx
sealert -a /var/log/audit/audit.log
日誌中提示說
setsebool -P ftp_home_dir=1
6.2】方法二 看man手冊
man -k ftp | grep selinux
man 8 ftp_selinux

開了SELinux和防火牆,沒想到引出了vsftp的問題。FTP登錄報錯:500 OOPS: cannot change directory。下面來看看產生這個問題的原因和對策。

首先,分析一下衝突原因:
1. 爲鎖定用戶在自己的home目錄中,在vsftpd.conf打開chroot_local_user。
這樣FTP登錄用戶的“/”,就是passwd中的home path,比如/var/www/a.com/。避免FTP用戶跑到/etc亂闖。這樣設置過,FTP登錄時,會自動執行CWD /var/www/html/www.xxx.com,並且把這個目錄設置爲FTP進程的根目錄,用戶就無法離開了。
vi /etc/vsftpd/vsftpd.conf

You may specify an explicit list of local users to chroot() to their home

directory. If chroot_local_user is YES, then this list becomes a list of

users to NOT chroot().

chroot_local_user=YES

chroot_list_enable=YES

(default follows)

chroot_list_file=/etc/vsftpd/chroot_list

當然也可以用chroot_list_enable=YES的辦法。但要逐個在chroot_list中指定FTP用戶名,很麻煩。也容易出現疏漏。所以還是推薦用chroot_local_user來限制。

  1. 下面,問題就出來了。打開SELinux後,SELinux會阻止ftp daemon讀取用戶home目錄。所以FTP會甩出一句 “500 OOPS: cannot change directory”。無法進入目錄,出錯退出。

解決辦法有兩個:

  1. 降低SELinux安全級別,把enforcing降低到permissive
    vi /etc/sysconfig/selinux

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=permissive

這時FTP的登錄功能就正常了。但降低整體系統安全作爲代價來解決一個小問題,這總不是最佳方案。

  1. 經過研究,又找到了另一個更理想的辦法。首先查看SELinux中有關FTP的設置狀態:
    getsebool -a|grep ftp

allow_ftpd_anon_write –> off
allow_ftpd_full_access –> off
allow_ftpd_use_cifs –> off
allow_ftpd_use_nfs –> off
allow_tftp_anon_write –> off
ftp_home_dir –> off
ftpd_connect_db –> off
ftpd_disable_trans –> on
ftpd_is_daemon –> on
httpd_enable_ftp_server –> off
tftpd_disable_trans –> off

經過嘗試發現,打開ftp_home_dir或者 ftpd_disable_trans。都可以達到在enforcing級別下,允許FTP正常登錄的效果。

setsebool -P ftp_home_dir 1

CentOS6裏,是這樣

setsebool -P allow_ftpd_full_access 1

service vsftpd restart

加-P是保存選項,每次重啓時不必重新執行這個命令了。最後別忘了在/etc/sysconfig/selinux中,修改SELINUX=enforcing。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章