linux防火墙
selinux
在配制远程连接时是要关闭selinux的。
临时关闭selinux:setenforce 0
永久关闭要编辑配置文件
vim /etc/selinux/config SELINUX=disabled
平时一般关闭selinux
2.iptables
iptables是linux的防火墙机制。在centos6时,是用的netfilter机制,centos7时用的是firewalld机制。
由于我们装的是centos7,防火墙机制就是firewalld,想使用netfilter,就必须将firewald停掉,开启netfilter.
关闭firewalld:
不让firewalld开机启动:
[root@shuai-01 network-scripts]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
停掉firewalld服务:
[root@shuai-01 network-scripts]# systemctl stop firewalld
安装netfilter:
[root@shuai-01 network-scripts]# yum install -y iptables-services
开启iptables服务:
[root@shuai-01 network-scripts]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@shuai-01 network-scripts]# systemctl start iptables
netfilter的五个表:(filter ,nat , mangle , raw , security)
这里主要了解filter和nat 。
filter表主要用于过滤包,包含三个链INPUT , OUTPUT , FORRWARD.
nat表主要用于网络地址转换,包含三个链PREROUTING , OUTPUT , POSTROUTING 。
当数据包进来,是本机数据包:PREROUTING , INPUT ,localhost , OUTPUT , POSTROUTING
不是本机数据包: PREROUTING , FORWARD , POSTROUTING
一个讲IPtables的五个表的文章:
http://blog.51cto.com/907832555/1944919
iptables相关的命令和用法:
查看iptables相关规则:
命令:iptables
选项:
-t 后接表名,表示选择那张表(默认是filter表)
-nvL 表示查看表的规则
-F 表示清空规则
-Z 表示把包的流量计数器置零[root@shuai-01 network-scripts]# iptables -nvL
防火墙的规则保存在/etc/sysconfig/iptables中
查看nat表的规则:
[root@shuai-01 network-scripts]# iptables -t nat -nvL Chain PREROUTING (policy ACCEPT 2 packets, 152 bytes) pkts bytes target prot opt in out source destination - 清空filter表的规则: [root@shuai-01 network-scripts]# iptables -F [root@shuai-01 network-scripts]# iptables -nvL Chain INPUT (policy ACCEPT 5 packets, 356 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 4 packets, 448 bytes) pkts bytes target prot opt in out source
重启标的规则:
[root@shuai-01 network-scripts]# service iptables restart Redirecting to /bin/systemctl restart iptables.service [root@shuai-01 network-scripts]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 24 1688 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 0 0 REJECT all -- * * 0.0.0.0/0
增加/删除一条规则
命令:iptables
选项:
-A/-D :表示增加/删除一条规则(后接INPUT,OUTPUT)
-I : 表示插入一条规则(后接INPUT,OUTPUT)
-P :表示预设策略
-p :表示指定协议(tcp , udp , icmp)
–sport :表示指定源端口,跟-p一起使用
–dport : 表示指定目标端口,跟-p一起使用
-s : 表示指定源IP
-d :表示指定目的IP
-j :后跟动作(ACCEPT表示允许包,DROP表示丢掉包,REJECT表示拒绝包)
-i :指定网卡增加一条规则
将来源10.12.11.13的22端口到目的12.11.13.14的80端口的tcp包丢掉[root@shuai-01 network-scripts]# iptables -A INPUT -s 10.12.11.13 -p tcp --sport 22 -d 12.11.13.14 --dport 80 -j DROP
插入一条规则 将来源1.1.1.1的所有数据包丢掉
[root@shuai-01 network-scripts]# iptables -I INPUT -s 1.1.1.1 -j DROP
添加一条规则 将来源2.2.2.2的到本机80端口的tcp包丢掉
[root@shuai-01 network-scripts]# iptables -A INPUT -s 2.2.2.2 -p tcp --dport 80 -j DROP
删除一条规则(将来源1.1.1.1的所有数据包丢掉)
[root@shuai-01 network-scripts]# iptables -D INPUT -s 1.1.1.1 -j DROP
按行删除规则
[root@shuai-01 network-scripts]# iptables -nvL --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 534 37016 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 6 0 0 DROP tcp -- * * 10.12.11.13 12.11.13.14 tcp spt:22 dpt:80 7 0 0 DROP tcp -- * * 2.2.2.2 0.0.0.0/0 tcp dpt:80 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 31 packets, 2932 bytes) num pkts bytes target prot opt in out source destination [root@shuai-01 network-scripts]# iptables -D INPUT 7 [root@shuai-01 network-scripts]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 605 41764 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited 0 0 DROP tcp -- * * 10.12.11.13 12.11.13.14