0416日常运维2
日常运维
12 firewalld和netfilter 防火墙
-
selinux
临时关闭 setenforce 0
永久关闭 编辑 /etc/selinux/config 将SELINUX=enforcing改成SELINUX=disabled 重启后生效
gentenforce 获取当前SElinux的状态
netfilter 之前的centos版本(5、6)的防火墙
firewalld centos7的防火墙
iptables是一个工具,可以通过该命令去添加一些规则,开启或关闭某些端口
更换netfitet服务步骤:
systemctl stop firewlld 关闭firewalld服务
systemctl disable firewlld 禁止开机启动firewalld服务
yum install -y iptables-services 安装netfilter服务
systemctl enable iptables
systemctl start iptables 开启iptables服务
iptables -nvL 查看启动后自带的规则(默认规则)
13 netfilter5表5链介绍
-
netfilter的5个表:
1.filter表
filter表 是iptables的默认表,用于过滤包,如果你没有自定义表,那么就默认使用filter表
它具有3个内建链:
INPUT链 – 处理来自外部的数据
OUTPUT链 – 处理向外发送的数据
FORWARD链 – 将数据转发到本机的其他网卡设备上
2.nat表
nat表 用于网络地址转换
它也有3个内建链:
PREROUTING链 – 在包刚刚到达防火墙时改变它的目的地址(如果需要的话)
OUTPUT链 – 改变本机产生的包的目的地址
POSTROUTING链 – 在包即将离开防火墙时改变其源地址
3.mangle表
mangle表 用于给数据包做标记
Mangle表具有5个内建链:
PREROUTING链
INPUT链
FORWARD链
OUTPUT链
POSTROUTING链
4.raw表
raw表 可以实现不追踪某些数据包,默认系统的数据包都会被追踪,用于处理异常
它具有2个内建链:
PREROUTING链
OUTPUT链
5.security表
security表 用于强制访问控制(MAC)的网络规则,在CentOS6中没有该表
netfilter的5个链:
PREROUTING:数据包进入路由器之前
INPUT:通过路由表后,目的地为本机
FORWARD:通过路由表后,目的地不为本机
OUTPUT:由本级产生,向外转发
POSTROUTING:发送到网卡接口之前
14 iptables语法
iptables基本语法:
iptables -t nat -nvL 查看规则以及清除规则
[[email protected] ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (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 destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
[[email protected] ~]#
-t 选项后面跟表名,不加-t 选项默认指 filter表
-F 表示删除所有规则
-Z 把包以及流量计数器置零
增加/删除/插入一条规则
-A/-D/- 表示增加/删除/插入一条规则(-I可以插入到最前面,-A只能增加到最后面)
-p 指定协议,可以是tcp、udp、icmp或all
–dport 必须和-p一起使用,表示指定目标端口
–sport 必须和-p一起使用,表示指定源端口
-s 指定源ip(可以是一个ip段)
-d 指定目的ip(可以是一个ip段)
-j 后面跟动作,其中ACCEPT表示允许包,DROP表示丢掉包,REJECT表示拒绝包
-i 指定接收进来数据包的网卡
-o 指定发送出去数据包的网卡
–line-number 显示规则编号(可以根据编号来删除规则)
-P 默认规则,默认为ACCEPT所有包(可以更改为DROP,表示丢弃所有包,不过这是危险操作,尽量不要尝试)
15 iptables filter表小案例
示例:
iptables -I INPUT -S 1.1.1.1 -j DROP
表示插入一条规则,把来自1.1.1.1的数据包全部丢掉
iptables -D INPUT -S 1.1.1.1 -j DROP
表示删除把来自1.1.1.1的数据包全部丢掉这条规则(删除一条规则时,必须和插入或者增加的规则一致,除了-A、-D、-I的区别)
iptables -I INPUT -S 2.2.2.2 -p tcp --dport 80 -j DROP
表示把来自2.2.2.2并且是TCP协议到本机80端口的数据包丢掉(–dport/–sport必须和-p选项一起使用,否则会出错)
iptables -I OUTPUT -p tcp --dport 22 -d 10.0.1.14 -j DROP
表示把发送到10.0.1.14的22端口的数据包丢掉
iptables -A INPUT -s 192.168.1.0/24 -I eth0 -j ACCEPT
表示把来自192.168.1.0/24这个网段且作用在eth0上的包放行
iptables -nvL --line-numbers
表示查看iptables规则,显示规则编号
iptables -D INPUT 1
当不知规则内容时可用 -D 后面依次跟链名、规则num,删除对应num的规则
iptables -P INPUT ACCEPT
-P 表示预设策略,后面跟链名,策略内容为ACCEPT,或者是DROP(如果是远程服务器时,切勿执行DROP操作,否则远程连接就会被断开)
iptables -I INPUT -m iprange -src 192.168.179.0-192.168.179.255 -j DROP
-m 后面跟模块名字,iprange是一个模块名字,用来支持一个网段
–src-range 指定源ip范围
–dst-range 指定目标ip范围
iptables -I INPUT -m state --state NEW,RELATED,ESTABLISHED -j ACCEPT
–state 指定要匹配包的的状态
在iptables上有四种状态:NEW、ESTABLISHED、INVALID、RELATID
NEW:conntrack模块看到的某一连接的第一个包
ESTABLISHED:只要发送并接收到应答,连接就是EATABLISHED状态
RELATID:当一个连接和某个已处于EATABLISHED状态的连接有关系时,就可以认为是RELATID状态
INVALID:INVALID意味着这个包没有已知的流或连接与之关联,也可能是它包含的数据或包头有问题
iptables -I INPUT -p icmp --icmp-type 8 -j DROP
–icmp-type需要跟-p icmp一起使用,后面指定类型编号,8表示能在本机ping通其他机器,而其他机器无法ping通本机
16-18 iptables nat表的应用
A机器两块网卡ens33(192.168.179.128)、ens37(192.168.100.1),ens33可以上外网,ens37仅仅是内部网络,B机器只有ens34(192.168.100.100),和A机器ens37可以通信互联。
A机器配置:
[[email protected] ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.179.128 netmask 255.255.255.0 broadcast 192.168.179.255
inet6 fe80::b76:caa8:3d7c:71bc prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:e4:fc:a5 txqueuelen 1000 (Ethernet)
RX packets 313 bytes 28159 (27.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 264 bytes 33270 (32.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.20.130 netmask 255.255.255.0 broadcast 192.168.100.255
ether 00:0c:29:e4:fc:a5 txqueuelen 1000 (Ethernet)
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.1 netmask 255.255.255.0 broadcast 192.168.133.255
ether 00:0c:29:e4:fc:af txqueuelen 1000 (Ethernet)
RX packets 48 bytes 14388 (14.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 58 bytes 9360 (9.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
B机器配置:
ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.100 netmask 255.255.255.0 broadcast 192.168.133.255
ether 00:0c:29:e4:fc:af txqueuelen 1000 (Ethernet)
RX packets 48 bytes 14388 (14.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 58 bytes 9360 (9.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
需求:可以让B机器连接外网
A机器上打开路由转发 echo “1”>/proc/sys/net/ipv4/ip_forward
A上执行 iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE
B上设置网关为192.168.100.1
[[email protected] ~]# cat /proc/sys/net/ipv4/ip_forward
0
[[email protected] ~]# echo "1"> /proc/sys/net/ipv4/ip_forward
[[email protected] ~]# cat /proc/sys/net/ipv4/ip_forward
1
以上表示打开路由转发功能
[[email protected] ~]# iptables -t nat -A POSTROUTING -s 192.168.133.0/24 -o ens33 -j MASQUERADE
[[email protected] ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
42 13256 PREROUTING_direct all -- * * 0.0.0.0/0 0.0.0.0/0
42 13256 PREROUTING_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
42 13256 PREROUTING_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
Chain INPUT (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 destination
104 7831 OUTPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
104 7831 POSTROUTING_direct all -- * * 0.0.0.0/0 0.0.0.0/0
104 7831 POSTROUTING_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
104 7831 POSTROUTING_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 MASQUERADE all -- * ens33 192.168.100.0/24 0.0.0.0/0
iptables对nat表做IP转发操作,-o 指定发送出去数据包的网卡,这里就是ens33,MASQUERADE表示伪装
[[email protected] ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.20.2 0.0.0.0 UG 100 0 0 ens33
192.168.20.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 ens34
[[email protected] ~]# route add default gw 198.168.100.1
[[email protected] ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 ens34
0.0.0.0 192.168.20.2 0.0.0.0 UG 100 0 0 ens33
192.168.20.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 ens34
对B机器设置网关,route -n 查看网关 ,route add default gw 设置默认网关
[[email protected] ~]# ping 192.168.100.1
PING 192.168.100.2 (192.168.100.2) 56(84) bytes of data.
64 bytes from 192.168.100.2: icmp_seq=1 ttl=128 time=0.178 ms
64 bytes from 192.168.100.2: icmp_seq=2 ttl=128 time=0.284 ms
64 bytes from 192.168.100.2: icmp_seq=3 ttl=128 time=0.197 ms
64 bytes from 192.168.100.2: icmp_seq=4 ttl=128 time=0.216 ms
64 bytes from 192.168.100.2: icmp_seq=5 ttl=128 time=0.227 ms
64 bytes from 192.168.100.2: icmp_seq=6 ttl=128 time=0.166 ms
[[email protected]~]#vi /etc/resolv.conf
# Generated by NetworkManager
nameserver 119.29.29.29
给ens34设置DNS:119.29.29.29
[[email protected]~]#ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=1 ttl=128 time=36.10 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=2 ttl=128 time=34.81 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=3 ttl=128 time=34.07 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=4 ttl=128 time=35.81 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=5 ttl=128 time=39.24 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=6 ttl=128 time=34.4 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=7 ttl=128 time=34.27 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=8 ttl=128 time=35.25 ms
64 bytes from 14.215.177.38 (115.239.211.112): icmp_seq=9 ttl=128 time=35.89 ms
^C
--- www.a.shifen.com ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 14821ms
rtt min/avg/max/mdev = 34.07/36.770/34.458/33.038 ms
B可以ping通 www.baidu.com,实现B通过nat借助A来上网,需求满足
直播笔记
selinux教程 http://os.51cto.com/art/201209/355490.htm
selinux pdf电子书 http://pan.baidu.com/s/1jGGdExK
iptables应用在一个网段 http://www.aminglinux.com/bbs/thread-177-1-1.html
sant,dnat,masquerade http://www.aminglinux.com/bbs/thread-7255-1-1.html
iptables限制syn速率 http://www.aminglinux.com/bbs/thread-985-1-1.html http://jamyy.us.to/blog/2006/03/206.html