Linux基础-安全管理第二部分

linux安全机制第二部分

在linux系统中,firewalld 服务和iptables服务是冲突的,所以二者只有一个能运行,如果开启了iptables,那么必须永久关闭防火墙,iptables也会有默认的基本配置,保证网络安全。

systemctl  stop firewall 

systemctl  disable firewall

 

这样才能用iptables    

iptables  -F   可以删掉所有的规则

iptables-save  保存当前配置 

 

 

 

Selinux 安全子系统

SELinux(Security-Enhanced Linux)是美国国家安全局在Linux开源社区的帮助下开发的一个强制访问控制(MAC,Mandatory Access Control)的安全子系统。RHEL 7系统使用SELinux技术的目的是为了让各个服务进程都受到约束,使其仅获取到本应获取的资源。

selinux在linux环境中有非常重要的用途

在我们linux环境中,如果禁用selinux,是可以减少非常多的报错,但是在生产环境中相当不推荐

 

selinux在linux当中的三个用途:

可以允许或者拒绝访问我们的文件和资源

可以明确一个进程,服务,可以调用哪些端口

可以对一个文件做更精确的限制,远远比我们的用户权限高很多,更精确。

 

 

selinux有三个模式:

强制模式  enforcing  selinux会强制执行严格的访问控制,系统默认就是这个模式,他会拒绝其他服务访问违规端口,同时也限制这些服务的访问执行权限,而且但凡发现了违规行为,会记录在日志里面。

许可模式 permissive  不执行严格的访问控制,但是会记录这些违规的警告,这个模式主要用于测试和故障排除

关闭模式  disabled  selinux功能完全关闭,不拒绝仍和的违规行为,而且不予记录。

 

三种模式的查看和切换:     *只是当前有效  关机重启后会变成原来的模式

getenforce   查看当前模式

setenforce  0   设置为许可模式

setenforce  1   设置为强制模式

 

关闭模式不能通过简单命令来实现 需要修改配置文件,同时修改配置文件也是永久修改默认模式的方法

selinux的配置文件: /etc/selinux/config

 

SELINUX = disabled   

 enforcing

 permissive

 

selinux 三个模式:

targeted: 默认模式,针对网络服务的限制比较多,对本机的限制比较少

minimun:只会选择性的保护我们的系统模块  例如内核 device

mls:完整的selinux限制,对系统各方面都做限制  

 

默认是targeted 模式,因为一般只需要对网络服务做限制

 

安全上下文:

在启动selinux的时候,selinux会给系统中的资源设置一个安全上下文,保存在 /etc/selinux/targeted/contexts/files/file_contexts

 

ls  -Z   查看安全上下文

 

 

[root@rhel8-server /]# ls -Zd    /var/www/html/

system_u:object_r:httpd_sys_content_t:s0 /var/www/html/

在文件上设置的selinux安全上下文,是由用户段,角色段,类型段等多个信息组成

system_u 代表了系统进程的身份

object_r   代表了文件目录的角色

httpd_sys_content_t  代表了网站服务的系统文件

S0 :  sensitivity

 

semanage 管理安全策略

 

使用getsebool命令查询并过滤出所有与HTTP协议相关的安全策略。其中,off为禁止状态,on为允许状态。

[root@localhost ~]# getsebool -a | grep http

httpd_anon_write --> offhttpd_builtin_scripting --> onhttpd_can_check_spam --> offhttpd_can_connect_ftp --> offhttpd_can_connect_ldap --> offhttpd_can_connect_mythtv --> offhttpd_can_connect_zabbix --> offhttpd_can_network_connect --> offhttpd_can_network_connect_cobbler --> offhttpd_can_network_connect_db --> offhttpd_can_network_memcache --> offhttpd_can_network_relay --> offhttpd_can_sendmail --> offhttpd_dbus_avahi --> offhttpd_dbus_sssd --> offhttpd_dontaudit_search_dirs --> offhttpd_enable_cgi --> onhttpd_enable_ftp_server --> offhttpd_enable_homedirs --> offhttpd_execmem --> offhttpd_graceful_shutdown --> onhttpd_manage_ipa --> offhttpd_mod_auth_ntlm_winbind --> offhttpd_mod_auth_pam --> offhttpd_read_user_content --> offhttpd_run_ipa --> offhttpd_run_preupgrade --> offhttpd_run_stickshift --> offhttpd_serve_cobbler_files --> offhttpd_setrlimit --> offhttpd_ssi_exec --> offhttpd_sys_script_anon_write --> offhttpd_tmp_exec --> offhttpd_tty_comm --> offhttpd_unified --> offhttpd_use_cifs --> offhttpd_use_fusefs --> offhttpd_use_gpg --> offhttpd_use_nfs --> offhttpd_use_openstack --> offhttpd_use_sasl --> offhttpd_verify_dns --> offnamed_tcp_bind_http_port --> offprosody_bind_http_port --> off[root@localhost ~]# setsebool -P httpd_enable_homedirs=on

 

面对如此多的SELinux域安全策略规则,实在没有必要逐个理解它们,我们只要能通过名字大致猜测出相关的策略用途就足够了。比如,想要开启httpd服务的个人用户主页功能,那么用到的SELinux域安全策略应该是httpd_enable_homedir

 

 

semanage-port   管理安全上下文  

-a  增加

-d  删除

-t   指定文件上下文类型

 

实验: 修改httpd服务监听端口改成82 (默认是80)

vim /etc/httpd/conf/httpd.conf 

需要修改配置文件为Listen 82端口

systemctl restart httpd

journalctl -xe

semanage port -a -t http_port_t -p tcp 82

systemctl restart httpd

posted @ 2022-02-09 16:13  终究还是避免不了遗憾  阅读(105)  评论(0编辑  收藏  举报