[root@daixuan ~]# cat /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 - No SELinux policy is loaded. 禁用selinux
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
getenforce 是查询 SELinux 目前所使用的模式
[root@daixuan ~]# getenforce 得到selinux状态
Disabled
setenforce 指令是用来修改 SELinux 在执行中的模式
指令语法:setenforce [ Enforcing | Permissive | 1 | 0 ]
执行「setenforce enforcing」或「setenforce 1」是把 SELinux 修改成 Enforcing 模式;
执行「setenforce permissive」或「setenforce 0」是把 SELinux 修改成 Permissive 模式。
注意:需要修改配置文件,selinux重启后才能生效 /etc/selinux/config
[root@daixuan ~]# setenforce 0 临时关闭selinux,SELINUX=enforcing前提下才可以使用这条命令,
setenforce: SELinux is disabled
[root@daixuan ~]# setenforce 1 打开selinux,SELINUX=enforcing才可以使用这条命令
setenforce: SELinux is disabled
[root@daixuan ~]# rpm -qf `which setenforce` setenforce使用前需要安装libselinux-utils-2.0.94-5.8.el6.i686包
libselinux-utils-2.0.94-5.8.el6.i686[root@daixuan ~]# yum provides "/*setenforce" 一种方法找命令是有哪个包
防火墙netfilter 工具iptables
table 表
chain 链
[root@daixuan ~]# iptables -t filter -nvL -t filter查看表filter下面的链,有3个,可以自定义链
Chain INPUT (policy ACCEPT 98419 packets, 48M bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 115K packets, 36M bytes)
pkts bytes target prot opt in out source
[root@daixuan ~]# iptables -t nat -nvL -t nat表有三个链
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source
[root@daixuan ~]# iptables -t mangle -nvL -t mangle有5个链
filter 过滤进包和出包 INPUT OUTPUT
[root@daixuan ~]# iptables -t filter -I INPUT -p tcp --dport 80 -s 12.12.12.12 -j REJECT 过滤filter表的输入表INPUT的tcp包到达80端口来源IP拒绝
[root@daixuan ~]# iptables -t filter -nvL 查看指定表filter的信息
Chain INPUT (policy ACCEPT 66 packets, 5658 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT tcp -- * * 12.12.12.12 0.0.0.0/0 tcp dpt:80 reject-with icmp-port-unreachable
[root@daixuan ~]# iptables -nvL 不加-t指定表,默认filter表
Chain INPUT (policy ACCEPT 668 packets, 54802 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT tcp -- * * 12.12.12.12 0.0.0.0/0 tcp dpt:80 reject-with icmp-port-unreachable
添加规则:
-I 插入规则,相对之前的其他规则先生效;
-A 添加规则,相对之前添加的规则后生效;
-D删除规则;
-j DROP 看都不看直接丢掉
-j REJECT 包拿过来看一看再决定丢掉
-j ACCEPT 接收所有的包
第一条规则匹配ACCEPT就通过,后面不再匹配了
[root@daixuan ~]# iptables -Z zero ,清空访问的pkts为0
[root@daixuan ~]# iptables -nvL
Chain INPUT (policy ACCEPT 9 packets, 1014 bytes) -F 清空所有的规则,默认是清空filter表
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
[root@daixuan ~]# service iptables restart 如果防火墙的规则没有保存,则重启iptables规则丢失
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
iptables:应用防火墙规则: [确定]
[root@daixuan ~]# service iptables save 保存添加的iptables规则
iptables:将防火墙规则保存到 /etc/sysconfig/iptables: [确定]
[root@daixuan ~]# iptables-save > 1.ipt 备份iptables规则到文件1.ipt中,默认是filter表
[root@daixuan ~]# cat 1.ipt
# Generated by iptables-save v1.4.7 on Thu Nov 19 16:45:45 2015
*filter
:INPUT ACCEPT [183:18378]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [107:13428]
-A INPUT -s 192.168.101.17/32 -p tcp -m tcp --dport 80 -j ACCEPT
COMMIT
[root@daixuan ~]# iptables -F 先清空iptables的规则
[root@daixuan ~]# iptables-restore < 1.ipt 还原1.ipt的规则到iptables中,默认的是filter表
将新装的CentOS系统的默认规则清空并保存清空规则后的状态
[root@daixuan ~]# service iptables save
iptables:将防火墙规则保存到 /etc/sysconfig/iptables: [确定]
[root@daixuan ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 40 packets, 7096 bytes) 将公网IP转换为内网IP
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 3 packets, 732 bytes) 将内网IP转换为公网IP
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 3 packets, 732 bytes)
pkts bytes target prot opt in out source destination
mangle表主要用来给包打标记
-P policy策略
chain ACCEPT 默认所有的数据包接收
chain DROP 默认所有的数据包拒绝
[root@daixuan ~]# iptables -P INPUT DROP 很危险,轻易不能做
[root@daixuan ~]# iptables -P INPUT ACCEPT