- 一、防火墙
- 二、Iptables
- 二、Firewalld
- 三、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.
- SELINUXTYPE= can take one of three values:
- targeted - Targeted processes are protected,
- minimum - Modification of targeted policy. Only selected processes are protected.
- mls - Multi Level Security protection.
一、防火墙
软件防火墙:在软件系统内核级别实现网络流量的过滤,性能稍弱,成本低
在linux上提供的软件防火墙,名字叫IPtables,它是防火墙命令行工具,iptables还是一个客户端代理,通过iptables代理,将用户配置的安全策略,执行到对应的安全框架netfilter中。
iptables只是一个命令行工具,处于用户空间,离用户最近的;真正实现流量过滤的软件是netfilter,处于系统内核空间,和操作系统离得最近的。
iptables+netfilter共同组成了linux的软件防火墙
在centos7下,firewalld软件,又代替了iptables工具firewall是把用户配置的防火墙规则,交给内核层的nftables网络过滤器去处理的
二、Iptables
匹配规则:从上往下进行匹配,命令结束此次匹配;没有命中则执行默认匹配规则。一般规则编写为:允许详细,默认禁止;禁止详细,默认允许
iptables把用于处理和过滤流量的策略,称之为规则;多条规则组成规则链,而规则链依据数据包处理位置不同进行分类为:input、output、forward(转发的数据包)、prerouting(路由选择前处理的数据包)、postrouting(路由选择后处理的数据包)
iptables命令可以根据流量的源地址、目的地址、传输协议、服务类型等信息进行匹配。
流量在进入input链之后,遵循如下动作去处理这些数据包:
- Accept 允许数据包通过
- Reject 拒绝数据包通过,还会给客户返回一个响应,告知他被拒绝了
- Log 在linux系统的日志目录下/var/log/message中记录防火墙日志,在进行下一个数据包处理
- drop 直接丢弃数据包,不给与客户端任何的回应
- SNAT 源地址转换,解决内网用户同一个公网的问题,用于Forward链
- DNAT 目标地址转换
- Redirect 在本机做端口映射 ```python iptables中常用的参数: -P 设置默认策略 -F 清空规则链 -L 查看规则链 —A 在规则链的末尾加入新规则 -I 在规则链的头部加入新规则 -D 删除某一条规则 -s 匹配来源地址 -d 匹配目标地址 -i 从这块网卡流入的数据 -o 匹配从这块网卡流出的数据 -p 匹配协议 —dport num 匹配目标端口号 —sport num 匹配来源端口号
iptables -t 表名<-A/I/D/R> 规则链名 [规则号] <-i/0 网卡名> -p 协议名 <-s 源IP/源子网> —sport 源端口 <-d 目标IP/目标子网> —dport 目标端口 -j 动作
```python[root@ylin ~]# iptables -F #清空规则[root@ylin ~]# iptables -P INPUT DROP #设置默认规则为拒绝 如果执行该命令后服务器不能上网,添加如下命令15 iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT16 iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT[root@ylin ~]# iptables -P INPUT ACCEPT #设置默认规则为允许[root@ylin ~]# service iptables save #设置永久生效,命令执行失败;需要安装iptables-service服务,并设置开机自动启动才行iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@ylin ~]# iptables -I INPUT -s 192.168.159.0/24 -p tcp --dport 22 -j ACCEPT #设置只允许这个网段ssh登录[root@ylin ~]# iptables -I INPUT 10 -s 192.168.159.0/24 -p tcp --dport 22 -j ACCEPT #设置只允许这个网段ssh登录,插入在10行[root@ylin ~]# iptables -D INPUT 3 #删除规则[root@ylin ~]# iptables -R INPUT 3 -j ACCPET #修改第三条规则为允[root@ylin ~]# iptables -I INPUT -p udp --dport 22 -j DROP #直接丢弃数据包[root@ylin ~]# iptables -I INPUT -p icmp -j REJECT #如果规则为REJECT则为拒绝会处理[root@ylin ~]# iptables -L |headChain INPUT (policy DROP)target prot opt source destinationDROP udp -- anywhere anywhere udp dpt:sshDROP tcp -- anywhere anywhere tcp dpt:serialgatewayDROP icmp -- anywhere anywhereACCEPT tcp -- 192.168.159.0/24 anywhere tcp dpt:ssh
在配置完iptables后重启新配置的会失效该命令列出当前的防火墙配置 iptables-save保存配置 #需要通过yum安装yum install iptables-services服务才行#保存 iptables-save > /etc/sysconfig/iptables#恢复 iptables-restore < /etc/sysconfig/iptablesservice iptables restart 重启服务仍然有效。
二、Firewalld
Firewalld新添加zon的概链,可以根据此来写策略
三、selinux
selinux的状态:
- enforcing 强制模式,他受selinux保护,就是违反了策略就无法执行操作
- permissive 提示模式
- disbaled 禁用selinux
- selinuxtype=targeted 定义linux系统使用哪个策略模块保护系统。 ```python 配置文件位置:/etc/selinux/config 链接文件:/etc/sysconfig/selinux中
[root@ylin ~]# cat /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=disabled
SELINUXTYPE= can take one of three values:
targeted - Targeted processes are protected,
minimum - Modification of targeted policy. Only selected processes are protected.
mls - Multi Level Security protection.
SELINUXTYPE=targeted
```python
[root@ylin ~]# getenforce #查看selinux状态
Disabled
setenforce设定selinux运行状态,1开启(Enforce),0关闭(Permissive)重启会恢复的
