linux初级学习之selinux的管理2-10

来源:互联网 发布:网络协议服务器 编辑:程序博客网 时间:2024/06/08 09:40
seliunx管理[root@localhost ~]# vim /etc/sysconfig/selinux ##在配置文件中将disabled改成enforcing [root@localhost ~]# getenforce                 ##查看状态Enforcing                                      ##enforcing表示强制[root@localhost ~]# ps auxZ | grep vsftpd      ##ps -Z来显示selinux上下文system_u:system_r:ftpd_t:s0-s0:c0.c1023 root 849 0.0  0.0 52760   560 ?        Ss   00:55   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.confunconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 1889 0.0  0.0 112644 936 pts/1 R+ 00:58   0:00 grep --color=auto vsftpd [root@localhost pub]# touch westos[root@localhost pub]# mv westos /var/ftp/pub/   [root@localhost ~]# touch file[root@localhost ~]# mv file /var/ftp/pub/[kiosk@foundation66 Desktop]$ lftp 172.25.254.234     ##此时状态为enforcing,查看不到移动到本地的文件lftp 172.25.254.234:~> lsdrwxrwxr-x    3 0        50             70 Apr 27 05:02 publftp 172.25.254.234:/> cd /publftp 172.25.254.234:/pub> ls-rw-------    1 14       50       22105128 Apr 26 07:58 bigfile-rw-------    1 14       50           2176 Apr 26 07:34 passwddrwx------    2 14       50              6 Apr 26 03:31 redhatlftp 172.25.254.234:/pub> quit [root@localhost pub]# setenforce  0   ##setenforce 0|1(0表示状态改为permissive,1表示改为enforcing)[root@localhost pub]# getenforce Permissive[kiosk@foundation66 Desktop]$ lftp 172.25.254.234 ##在permissive状态下可以看到移动到当前的文件westos和filelftp 172.25.254.234:~> cd /pubcd ok, cwd=/publftp 172.25.254.234:/pub> ls-rw-------    1 14       50       22105128 Apr 26 07:58 bigfile-rw-r--r--    1 0        0               0 Apr 27 04:59 file-rw-------    1 14       50           2176 Apr 26 07:34 passwddrwx------    2 14       50              6 Apr 26 03:31 redhat-rw-r--r--    1 0        0               0 Apr 27 05:01 westoslftp 172.25.254.234:/pub> quit[root@localhost pub]# ls -Z      ##显示selinux安全上下文,此时file和westos与别的文件格式不一致-rw-------. ftp  ftp  system_u:object_r:public_content_t:s0 bigfile-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file-rw-------. ftp  ftp  system_u:object_r:public_content_t:s0 passwddrwx------. ftp  ftp  system_u:object_r:public_content_t:s0 redhat-rw-r--r--. root root unconfined_u:object_r:default_t:s0 westos[root@localhost pub]# chcon -t public_content_t  /var/ftp/pub/westos ##修该为westos的安全上下文[root@localhost pub]# ls -Z                                         ##westos变为与其他文件一样的安全上下文,为操作的file格式不一致-rw-------. ftp  ftp  system_u:object_r:public_content_t:s0 bigfile-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 file-rw-------. ftp  ftp  system_u:object_r:public_content_t:s0 passwddrwx------. ftp  ftp  system_u:object_r:public_content_t:s0 redhat-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 westos  3.如何更改文件安全上下文临时更改)chcon -t 安全上下文    文件chcon -t public_content_t /publicftp -R永久更改)semanage fcontext -l        ##列出内核安全上下文列表内容semanage fcontext -a -t public_content_t '/publicftp(/.*)?'restorecon -FvvR /publicftp/• restorecon 是 policycoreutil 软件包的一部分• semanage 是 policycoreutil-python 软件包的一部分• semanage fcontext 可用与显示或修改   restorrecon 用来设置默认文件上下文的规则• semanage fcontext 使用扩展正则表达式来指定路径和文件名。   fcontext 规则中最常用的扩展正则表达式是(/.*)?, 表示随意地匹配 / 后跟任何数量的字符• semanage fcontext 将递归地与在表达式前面列出的目录以及该目录中的所有内容相匹配[root@localhost mnt]# semanage fcontext  -a -t public_content_t  '/westos(/.*)?'[root@localhost mnt]# semanage fcontext -l | grep westos/westos(/.*)?                                      all files          system_u:object_r:public_content_t:s0 


[root@localhost mnt]# restorecon  -RvvF /westos/
restorecon reset /westos context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file1 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file2 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file3 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
restorecon reset /westos/file4 context unconfined_u:object_r:default_t:s0->system_u:object_r:public_content_t:s0
[root@localhost mnt]# touch /westos/file
[root@localhost mnt]# ls -Z /westos
-rw-r--r--. root root unconfined_u:object_r:public_content_t:s0 file
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file1
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file2
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file3
-rw-r--r--. root root system_u:object_r:public_content_t:s0 file4

4.如何控制selinux对服务功能的开关
getsebool -a | grep 服务名称
getsebool -a | grep ftp
setsebool -P 功能bool值 on|off
setsebool -P    ftpd_anon_write on
[root@localhost mnt]# getsebool  -a | grep ftp
ftp_home_dir --> off
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off

[root@localhost mnt]# setsebool  -P ftp_home_dir 1  ##打开服务开关 1/on打开  0/off关闭
[root@localhost mnt]# getsebool  -a | grep ftp      ##ftp服务打开
ftp_home_dir --> on
ftpd_anon_write --> off
ftpd_connect_all_unreserved --> off
ftpd_connect_db --> off
ftpd_full_access --> off
ftpd_use_cifs --> off
ftpd_use_fusefs --> off
ftpd_use_nfs --> off
ftpd_use_passive_mode --> off
httpd_can_connect_ftp --> off
httpd_enable_ftp_server --> off
sftpd_anon_write --> off
sftpd_enable_homedirs --> off
sftpd_full_access --> off
sftpd_write_ssh_home --> off
tftp_anon_write --> off
tftp_home_dir --> off



5.监控selinux的错误信息
setroubleshoot-server
• 必须安装 setroubleshoot-server 软件包 , 才能将SELinux 消息发送至 /var/log/messages

• etroubleshoot-server 侦听/var/log/audit/audit.log 中的审核信息并将简短摘要发送至 /var/log/messages
• 摘要包括 SELinux 冲突的唯一标识符 ( UUIDs ),可用于收集更多信息。 Sealert -l UUID 用于生成特定事件的报告。
 Sealert -a/var/log/audit/audit.log 用于在该文件中生成所有事件的报告





                                             
0 0