Linux 系统安全设置

一、SElinux安全子系统策略。

临时修改

命令:setenforce 0                           #临时关闭selinux。

命令:setenforce 1                           #临时开启selinux

命令:getenforce                              #查看selinux状态。

 

永久修改

文件路径:vi /etc/selinux/config                   #永久修改selinux。

注:修改三种模式。

 


 

二、netfilter  iptables 防火墙设置。

  • 防火墙默认三个主要规则链路。(INPUT)(FORWARD)(OUTPUT)实现过滤功能。

①表名

Raw:高级功能,如:网址过滤。
Mangle:数据包修改,用户实现服务质量。
Net:地址转换,用与网关路由器。
Filter:包过滤,用户防火墙规则,默认表。

 

②规则链路

INPUT链:处理输入数据包。
OUTPUT链:处理输出数据包。
PORWARD链:处理转发数据包。
PREROUTING链:用于目标地址转换(DNAT)。
POSTOUTING链:用于源地址转换(SNAT)。

 

③动作

accept:接收数据包。
DROP:丢弃数据包。
REDIRECT:重定向,映射,透明代理。
SNAT:原地址转换。
DNAT:目标地址转换。
MASQUERADE:IP伪装(NAT),用户ADSL。
LOG:日志记录。
-t<表>:指定要操纵的表;
-A:向规则链中添加条目;
-D:从规则链中删除条目;
-i:向规则链中插入条目;
-R:替换规则链中的条目;
-L:显示规则链中已有的条目;
-F:清楚规则链中已有的条目;
-Z:清空规则链中的数据包计算器和字节计数器;
-N:创建新的用户自定义规则链;
-P:定义规则链中的默认目标;
-h:显示帮助信息;
-p:指定要匹配的数据包协议类型;
-s:指定要匹配的数据包源ip地址;
-j<目标>:指定要跳转的目标;
-i<网络接口>:指定数据包进入本机的网络接口;
-o<网络接口>:指定数据包要离开本机所使用的网络接口。
参数

 

④管理命令

注:表内数据是重上往下读,优先生效上面的指令。

语法格式:

iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作
iptables –t net                                #指定表。
iptables –A INPUT                              #-A指定一张表
iptables –A INPUT –p tcp                       #-p指定协议,Tcp,udp,icmp。
iptables –A INPUT –p tcp –s 1.1.1.1            #-s来原IP。
iptables –I INPUT –p tcp –s 1.1.1.1 –j DROP    #-j状态,DROP禁止访问。
iptables –D INPUT –p tcp –s 1.1.1.1 –j DROP    #-D指定删除链的规则。
iptables –F                                    #-F删除所有规则。
iptables –Z                                    #-Z清0数据包计数器和字节数。
#允许本地回环接口(即运行本机访问本机)
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT  
             
#允许已建立的或相关连的通行
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT   

#允许所有本机向外的访问
iptables -A OUTPUT -j ACCEPT        

#允许访问22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT    

#允许访问80端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT    

#允许ftp服务的21端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT    

#允许FTP服务的20端口
iptables -A INPUT -p tcp --dport 20 -j ACCEPT

#禁止其他未允许的规则访问
iptables -A INPUT -j reject    

#禁止其他未允许的规则访问
iptables -A FORWARD -j REJECT    
开放指定的端口
#屏蔽单个IP的命令
iptables -I INPUT -s 123.45.6.7 -j DROP   

#封整个段即从123.0.0.1到123.255.255.254的命令
iptables -I INPUT -s 123.0.0.0/8 -j DROP

#封IP段即从123.45.0.1到123.45.255.254的命令
iptables -I INPUT -s 124.45.0.0/16 -j DROP

#封IP段即从123.45.6.1到123.45.6.254的命令是
iptables -I INPUT -s 123.45.6.0/24 -j DROP    
屏蔽IP
iptables -L -n -v


Chain INPUT (policy DROP 48106 packets, 2690K bytes)
 pkts bytes target     prot opt in     out     source               destination         
 5075  589K ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
 191K   90M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22
1499K  133M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:80
4364K 6351M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED
 6256  327K ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 3382K packets, 1819M bytes)
 pkts bytes target     prot opt in     out     source               destination         
 5075  589K ACCEPT     all  --  *      lo      0.0.0.0/0            0.0.0.0/0  
查看已添加的iptables规则
# 打印iptables 表 方便查看序列
[root@localhost ~]# iptables -nvL --line-num
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      106  8992 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
5        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 77 packets, 9496 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
  

# iptables -D 表名 重上往下数 多少个的序号  进行删除 
[root@localhost home]# iptables -D INPUT 8
删除以添加的iptables规则

 

⑤其他操作

  • 命令:service iptables save                      #保存。
  • 注:链路规则表保存在:/etc/sysconfig/iptables
  • 命令:iptables –t filter –nvL                      #查看防火墙链状态。
  • 注:标黄的部分是开机到现在所访问的流量。

 

posted @ 2018-01-17 20:14  kevin.Xiang  阅读(788)  评论(0编辑  收藏  举报