文章目录
1.架构图
1、用户在浏览器输入域名
2、浏览器拿着域名去DNS服务器解析
3、DNS服务器将解析后的IP返回给浏览器
4、浏览器根据IP去访问真实服务器
5、访问真实服务器的防火墙
6、请求通过防火墙后通过内网交换机传给负载均衡
7、负载均衡将请求平均分配给后端的web服务器
8、web服务器判断是静态请求还是动态请求
9、如果是静态请求,web服务器会去文件服务器获取数据
10、如果是动态请求,web服务器会去数据库或者缓存获取数据
11、数据从数据库或者NFS服务器返回给web服务器,web服务器将数据返回给负载均衡,
负载均衡通过防火墙将数据传给浏览器
1、管理人员连接跳板机或者vpn
2、通过跳板机或者vpn连接内网服务器
3、通过zabbix监控查看服务器状态
4、如果有问题则连接响应的机器解决问题
5、日常巡检,查看服务器配置
6、管理备份和备份数据
7、日志收集和整理展示
2.优化文件
# 1.vi init.sh 编辑文件
"""
将下面文件cv过来
:wq
"""
# 2.添加可执行权限
[root@localhost ~]# chmod +x init.sh
# 3. 执行程序
[root@localhost ~]# ./init.sh
# 4.建立一个快照
#!/bin/bash
# 备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# 修改 yum源 & repo源
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 更新操作系统
yum update -y
# 安装重用软件
yum -y install python-setuptools python-pip gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel zipqunzip ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssh openssl-develnss_ldap openldap openldap-devel openldap-clients openldap-servers libxslt-devel libevent-devel ntp libtool-ltdl bison libtool vim-enhanced python wget lsof iptraf strace lrzsz kernel-devel kernel-headers pam-devel tcl tk cmake ncurses-devel bisonsetuptool popt-devel net-snmp screen perl-devel pcre-devel net-snmp screen tcpdump rsync sysstat man iptables sudo libconfiggit bind-utils tmux elinks numactl iftop bwm-ng net-tools expect
# 临时关闭 setenforce
setenforce 0
# 永久关闭 setenforce
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
# 关闭防火墙
systemctl disable --now firewalld
echo '#Timing synchronization time' >>/var/spool/cron/root
# 每隔两小时更新一次时间
echo '0 */2 * * * /usr/sbin/ntpdate ntp1.aliyun.com &>/dev/null' >>/var/spool/cron/root
# 修改系统内核参数
cat >>/etc/sysctl.conf <<EOF
net.ipv4.tcp_fin_timeout = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.core.somaxconn = 16384
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_orphans = 16384
net.ipv4.ip_forward = 1
EOF
# 让参数生效
sysctl -p
# 优化 sshd
sed -i 's/#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
sed -i 's/GSSAPIAuthentication yes/GSSAPIAuthentication no/g' /etc/ssh/sshd_config
systemctl restart sshd
# 禁用不常用的软件
systemctl disable abrt-ccpp.service
systemctl disable abrt-oops.service
systemctl disable abrt-vmcore.service
systemctl disable abrt-xorg.service
systemctl disable abrtd.service
systemctl disable accounts-daemon.service
systemctl disable atd.service
systemctl disable auditd.service
systemctl disable autovt@.service
systemctl disable avahi-daemon.service
systemctl disable bluetooth.service
systemctl disable cups.service
systemctl disable dbus-org.bluez.service
systemctl disable dbus-org.fedoraproject.FirewallD1.service
systemctl disable dbus-org.freedesktop.Avahi.service
systemctl disable dbus-org.freedesktop.ModemManager1.service
systemctl disable dbus-org.freedesktop.NetworkManager.service
systemctl disable dbus-org.freedesktop.nm-dispatcher.service
systemctl disable display-manager.service
systemctl disable dmraid-activation.service
systemctl disable firewalld.service
systemctl disable gdm.service
systemctl disable irqbalance.service
systemctl disable iscsi.service
systemctl disable ksm.service
systemctl disable ksmtuned.service
systemctl disable libstoragemgmt.service
systemctl disable libvirtd.service
systemctl disable lvm2-monitor.service
systemctl disable mcelog.service
systemctl disable mdmonitor.service
systemctl disable microcode.service
systemctl disable ModemManager.service
systemctl disable multipathd.service
systemctl disable NetworkManager-dispatcher.service
systemctl disable NetworkManager.service
systemctl disable postfix.service
systemctl disable qemu-guest-agent.service
systemctl disable rngd.service
systemctl disable rtkit-daemon.service
systemctl disable smartd.service
systemctl disable spice-vdagentd.service
systemctl disable systemd-readahead-collect.service
systemctl disable systemd-readahead-drop.service
systemctl disable systemd-readahead-replay.service
systemctl disable tuned.service
systemctl disable vgauthd.service
systemctl disable vmtoolsd.service
# 开机提示
cat >/etc/motd <<EOF
#********************************************************************#
# WelCome To Login #
# Author:kid21 #
# WX:Python_21 #
# Date:2021-12-24 #
# Copyright©:2021 All rightsreserved #
#********************************************************************#
EOF
3.修改主机名脚本
执行xx脚本
xx x1 x2
x1参数的值 被 $1 获取
x2参数的值 被 $2获取
# 编辑脚本
#! /bin/bash
# 修改ip 当前主机ip 192.168.15.100
sed -i "s/.100/.$1/g" /etc/sysconfig/network-scripts/ifcfg-eth[01]
# 修改主机名
hostnamectl set-hostname $2
# 立刻生效
systemctl restart network
# 域名解析
# echo "172.168.1.$1" >> /etc/hosts
# 添加可执行权限
[root@localhost ~]# chmod +x checkip_name.sh
# 查看
[root@localhost ~]# grep 'IPADDR' /etc/sysconfig/network-scripts/ifcfg-eth[01]
/etc/sysconfig/network-scripts/ifcfg-eth0:IPADDR=192.168.15.100
/etc/sysconfig/network-scripts/ifcfg-eth1:IPADDR=172.16.1.100
# 测试
[root@localhost ~]# ./checkip_name.sh 101 kid
# 立刻生效 重新连接xshell
Connecting to 192.168.15.101:22...
Connection established.
To escape to local shell, press Ctrl+Alt+].
WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Fri Dec 24 22:34:30 2021 from 192.168.15.1
#********************************************************************#
# WelCome To Login #
# Author:kid21 #
# WX:Python_21 #
# Date:2021-12-24 #
# Copyright©:2021 All rightsreserved #
#********************************************************************#
# 检查
[root@kid ~]# grep 'IPADDR' /etc/sysconfig/network-scripts/ifcfg-eth[01]
/etc/sysconfig/network-scripts/ifcfg-eth0:IPADDR=192.168.15.101
/etc/sysconfig/network-scripts/ifcfg-eth1:IPADDR=172.16.1.101
# 在修改回来
[root@kid ~] sed 's/.101/.100/' /etc/sysconfig/network-scripts/ifcfg-eth[01]
4.搭建网络集群架构
需要11台虚机机
克隆10台主机,开机后修改主机名称和ip
4.1关闭虚机机
4.2克隆主机
根据自己的情况选择
我就现第一个了
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-s1rutipi-1640703727211)(https://s2.loli.net/2021/12/24/vAE1ZRm4S2GIfXk.png)]
4.3修改名称选择路径
选择存放的路径
D:\Linux\集群架构\lb01
D:\Linux\集群架构\lb00
....
4.4拖到到文件夹中
新建文件夹将将创建的虚机拖进去.
# 负载均衡
1. lb01
2. lb02
# web服务器
3. web01
4. web02
5. web03
# 数据库
6. db01
# 静态文佳
7. nfs
# 备份
8. backup
# 缓存
9. cache
# 普罗米修斯监控
10. prometheus
# 跳板机
11. m01
4.5完成克隆
4.6启动虚拟机
* 电脑不行就不要全部打开需要那台就开那台 *
全部选择选择开机(占用3.3G)
4.7修改信息
修改每台主机的ip和主机名称
./checkip_name 5 lb01
./checkip_name 6 lb02
...
# 负载均衡
1. lb01 5
2. lb02 6
# web服务器
3. web01 7
4. web02 8
5. web03 9
# 数据库
6. db01 61
# 静态文佳
7. nfs 31
# 备份
8. backup 51
# 缓存
9. cache 41
# 普罗米修斯监控
10. prometheus 71
# 跳板机
11. m01 81
4.8xshell连接
1.新建一个文件夹
2.命名
3.双击后进入文佳夹
4.新建会话
5.绑定ip点连接
6.输入登入用户名称
7.输入用户密码
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nO3lJXTE-1640703727225)(C:\Users\13600\AppData\Roaming\Typora\typora-user-images\image-20211227184925549.png)]
8.登入成功
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KFkOomE0-1640703727225)(C:\Users\13600\AppData\Roaming\Typora\typora-user-images\image-20211227184933495.png)]
9.重复上面的过程把剩下的主机都绑定好.
5.Iptables介绍
5.1Iptables的作用及种类
防火墙作用:防止别人恶意访问。
防火墙种类:
1.硬件防火墙
F5
2.软件防火墙
iptables
firewalld
3.安全组
5.2执行流程
用户 ---> 调用iptables ---> ip_tables内核模块 ---> Netfilter(系统安全框架) ---> 过滤请求
5.3是包过滤防火墙
包:在数据传输过程,并不是一次性传输完成的;而是将数据分成若干个数据包,一点一点的传输。
包过滤防火墙:过滤数据包的防火墙。
通过系统安全框架,过滤数据包。
5.4四表五链的概念
1、四个表,
具备某种功能的集合叫做表。
filter: 负责做过滤功能呢 INPUT、OUTPUT、FORWARD
nat: 网络地址转换 PREROUTING、INPUT、OUTPUT、POSTROUTING
mangle: 负责修改数据包内容 PREROUTING、INPUT、OUTPUT、POSTROUTING、FORWARD
raw: 负责数据包跟踪 PREROUTING、OUTPUT
2.五条链,运行在那些地方
2.1 PREROUTING: 主机外报文进入位置,允许的表mangle, nat
(目标地址转换,把本机地址转换为真正的目标机地址,通常指响应报文)
2.2 INPUT:报文进入本机用户空间位置,允许的表filter, mangle
2.3 OUTPUT:报文从本机用户空间出去的位置,允许filter, mangle, nat
2.4 FOWARD:报文经过路由并且发觉不是本机决定转发但还不知道从哪个网卡出去,允许filter, mangle
2.5 POSTROUTING:报文经过路由被转发出去,允许mangle,nat(源地址转换,把原始地址转换为转发主机出口网卡地址)
流入本机:PREROUTING --> INPUT --> PROCESS(进程)
经过本机:PREROUTING --> FORWARD --> POSTROUTING
从本机流出:PROCESS(进程) --> OUTPUT --> POSTROUTING
5.5 ptables流程图
filter : INPUT、 FORWARD、OUTPUT 、
nat : PREROUTING 、INPUT、 OUTPUT、 POSTROUTING
raw : PREROUTING、 OUTPUT
mangle : PREROUTING INPUT FORWARD OUTPUT POSTROUTING
6.Iptables下载
# 关闭防火墙
[root@m01 ~]# systemctl disable --now firewalld
# 1.先安装Iptables
[root@m01 ~]# yum install -y iptables*
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
...
# 2.启动服务
[root@m01 ~]# systemctl start iptables
# 3.查看
[root@m01 ~]# iptables -v -L
# 查看服务是否启动
[root@m01 ~]# systemctl status iptables
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
Active: active (exited)
# 激活状态
7.Iptables命令
格式:iptables -t 表名 选项 链名称 条件 动作
参数:
-V 版本
-t: 指定操作的表
-L, --list 列出当前的规则
-v 显示数据包和数据包大小
-n 不反解地址
-A, --append 追加一条规则到链中
-I, --insert 插入一条规则,插入到顶部
-F, --flush 清空
-Z, --zero 清空计数器( 包数量 、包大小)
-D, --delete 删除链中的规则
-R, --replace 修改
-S, --list-rules 列出所有的规则
-N, --new-chain 创建一个自定义 链
-X, --delete-chain 删除一个自定义链
-P, --policy 指定链的默认策略
7.1版本
[root@m01 ~]# iptables -t filter -V
iptables v1.4.21
7.2查看表
[root@m01 ~]# iptables -v -L -t filter
# 查看 filter 表
iptables -v -L -t filter
7.2不反解地址
[root@m01 ~]# iptables -t filter -L -v -n
* 0.0.0.0/0 表示所有的地址都可以连接
8.Iptables动作
ACCEPT 将数据包放行,进行完此处理动作后,将不再比对其它规则,直接跳往下一个规则链。
REJECT 拦阻该数据包,并传送数据包通知对方。(数据包来了,然后阻拦,在通知数据包被拦截了)
DROP 丢弃包不予处理,进行完此处理动作后,将不再比对其它规则,直接中断过滤程序。
REDIRECT 将包重新导向到另一个端口,进行完此处理动作后,将会继续比对其它规则。
[root@m01 ~]# iptables -t filter -L
规则从上到在依次匹配.
9.Iptables基本的条件匹配
TCP(http)
UDP
ICMP(ping 网址)
ALL 所有
10. 源地址/目标地址
-s 源地址:发送请求的地址
-d 目标地址 : 访问的地址
11.源端口/目标端口
--sport 源端口:发送请求的端口
--dport 目标端口:访问的端口
12.网卡/模块/协议/ 动作
-i : 进来的网卡
-o : 出去的网卡
-m : 指定模块
-j : 转发动作
-p :指定协议
13.案例
写规则的时候:
先确定表,表是有具体作用的 链决定什么时候执行 选择协议 设置断开 设设置动作
13.1清空规则
# 0.清空规则和包 iptables -F,默认的表示filter表.
[root@m01 ~]# iptables -t filter -F
[root@m01 ~]# iptables -t filter -Z
[root@m01 ~]# iptables -t filter -L -v
Chain INPUT (policy ACCEPT 138 packets, 8260 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 73 packets, 5580 bytes)
pkts bytes target prot opt in out source destination
13.2案例1
只允许22端口可以访问,其他端口全部无法访问。
添加规则1:
iptables -t filter 选择 filter表
-A INPUT 指定规则
-p TCP 指定协议
-- dport 22 指定端口号
-j ACCEPT 允许通过
添加规则2:
iptables -t filter
-A INPUT
-p TCP
-j DROP
[root@m01 ~]# iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
[root@m01 ~]# iptables -t filter -L -v -n
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
10 596 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 7 packets, 736 bytes)
pkts bytes target prot opt in out source destination
# 安装 nginx服务器
[root@m01 ~]# yum install -y nginx
# 启动服务
[root@m01 ~]# systemctl start nginx
进入浏览器输入ip地址进行检测
# 查看本机端口占用的命令:
[root@m01 ~]# netstat -nutlp
现在的情况是,22端口可以访问
192.168.15.81:80
80端口也可以访问nginx服务器
只添加了第一个规则,只是设置通过22端口访问怎么处理,没有说其他端口访问因该怎么取处理.
# 添加第二条规则
iptables -t filter -A INPUT -p TCP -j DROP
# xshell通过22端口连接的能正常使用
# 无法访问80端口了
13.2案例二
清理filter的规则! iptables -F ,默认的表示filter表.
只允许22, 80端口可以访问,其他端口全部无法访问。
iptables -t filter -A INPUT -p TCP --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p TCP --dport 80 -j ACCEPT
iptables -t filter -A INPUT -p TCP -j DROP
13.3案例三
清理filter的规则!
只有192.168.15.81通过22端口连接到本机,但是其他的不行
主机a 访问 192.168.15.81:22 连接到主机
开启普罗米斯这个主机去连接 mo1
所有的虚机机都可以通过局域王来相互连接,
172.16.1.81
# 通过局域网去连接m01主机 exit退出登入
[root@prometheus ~]# ssh root@172.16.1.81
The authenticity of host '172.16.1.81 (172.16.1.81)' can't be established.
ECDSA key fingerprint is SHA256:9Y7lXh4ptnVyiMKaEtfMXkWbtMPxwejGvwLl7vvsGJk.
ECDSA key fingerprint is MD5:25:74:fb:37:6c:07:f9:5c:ee:ed:a0:00:ed:07:9c:bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.81' (ECDSA) to the list of known hosts.
root@172.16.1.81's password:
Last login: Mon Dec 27 23:54:14 2021 from 192.168.15.1
#********************************************************************#
# WelCome To Login #
# Author:kid21 #
# WX:Python_21 #
# Date:2021-12-24 #
# Copyright©:2021 All rightsreserved #
#********************************************************************#
[root@m01 ~]#
规则1:
iptables -t filter 选择 filter表
-A INPUT 指定规则
-p TCP 指定协议
-d 192.168.15.81 设置目标地址 访问的地址 (只能通过192.168.15.81访问这台主机)
-- dport 22 指定端口号
-j ACCEPT 允许通过
规则2: 其他所有的都拒绝
iptables -t filter -A INPUT -p TCP -j DROP
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -d 192.168.15.81 --dport 22 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -j DROP
[root@m01 ~]# iptables -t filter -L -v -n
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
24 1472 ACCEPT tcp -- * * 0.0.0.0/0 192.168.15.81 tcp dpt:22
0 0 DROP tcp -- * * 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 5 packets, 612 bytes)
pkts bytes target prot opt in out source destination
# 设置之后 xshell被卡死,重连之后也无法在连接上了
[root@prometheus ~]# ssh root@172.16.1.81
ssh: connect to host 172.16.1.81 port 22: Connection timed out
13.4案例四
清理filter的规则! iptables -F
只允许192.168.15.71主机能够通过22端口访问,其他的不行。
主机192.168.15.71 通过22端口访问 其他的不行。
规则1:
iptables -t filter -A INPUT -p TCP -s 192.168.15.71 --dport 22 -j ACCEPT
(iptables -t filter -A INPUT -p TCP -s 192.168.15.71 -d 192.168.15.81 --dport 22 -j ACCEPT
源端口,目标端口都设置.
)
规则2:
iptables -t filter -A INPUT -p TCP -j DROP
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -s 192.168.15.71 --dport 22 -j ACCEPT
[root@m01 ~]# iptables -t filter -A INPUT -p TCP -j DROP
"""
xshell 卡死了
"""
# 71的主机可以连接 exit退出登入
[root@prometheus ~]# ssh root@192.168.15.81
# 局域网是通过 172.16.1.71 去访问,是无法连接的
[root@prometheus ~]# ssh root@172.16.1.81
13.5案例五
要求192.168.15.71对外部不可见(只有它不能访问)
iptables -t filter -A INPUT -p TCP -d 192.168.15.71 -j DROP
13.6案例六
要求使用eth0网卡的所有请求全部拒绝
iptables -t filter -A INPUT -p TCP -i eth0 -j DROP
-i 设置进入的网卡,
192不能使用
172局域网可以使用
13.7案例七
使用172.16.1.71登录进来的窗口,不返回信息.
iptables -t filter -I OUTPUT -p TCP -o eth1 -j DROP
-I OUTPUT 插入到顶部 OUTPUT 出去的信息
-i 设置出去的网卡, eth1
可以放回进入,没有信息放回.
13.8案例八
要求访问服务器的8080端口转发至80端口
iptables -t nat -A PREROUTING -p TCP --dport 8080 -j REDIRECT --to-port 80
nat表负责网络地址转换
PREROUTING 链 报文经过路由被转发出去
-j REDIRECT 将包重新导向到另一个端口
--to-port 80 给80端口
[root@m01 ~]# iptables -t nat -A PREROUTING -p TCP --dport 8080 -j REDIRECT --to-port 80
[root@m01 ~]# iptables -t nat -L -v -n
Chain PREROUTING (policy ACCEPT 46 packets, 2418 bytes)
pkts bytes target prot opt in out source destination
30 1560 REDIRECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 redir ports 80
# 8080转80
...
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xRth7pgN-1640703727232)(C:\Users\13600\AppData\Roaming\Typora\typora-user-images\image-20211228195001984.png)]
13.9案例九
要求只允许windows通过ssh连接192.168.15.81,其他的拒绝
查看电脑windows电脑的 ip
iptables -t filter -I INPUT -p TCP -s 192.168.15.1 -d 192.168.15.81 --dport 22 -j ACCEPT
iptables -t filter -I INPUT -p TCP --dport 22 -j DROP
# 另一台主机无法连接上 m01主机
[root@prometheus ~]# ssh root@172.16.1.81
ssh: connect to host 172.16.1.81 port 22: Connection timed out
[root@prometheus ~]# ssh root@192.168.15.81
ssh: connect to host 192.168.15.81 port 22: Connection timed out
14.拓展模块
拓展iptables的功能的。
-m : 指定模块
1.连续匹配多个端口(multiport)
--dports : 指定多个端口(不同端口之间以逗号分割,连续的端口使用冒号分割)。
2.指定一段连续的ip地址范围(iprange)
--src-range from[-to]: 源地址范围
--dst-range from[-to] 目标地址范围
3.匹配指定字符串(string)
--string pattern # 指定要匹配的字符串
--algo {bm|kmp} # 匹配的查询算法
4.根据时间段匹配报文(time)
--timestart hh:mm[:ss] # 开始时间
--timestop hh:mm[:ss] # 结束时间
--monthdays day[,day...] # 指定一个月的某一天
--weekdays day[,day...] # 指定周 还是 周天
5.禁ping, 默认本机无法ping别人 、别人无法ping自己
--icmp-type {type[/code]|typename}
echo-request (8) 只请求,不回应
echo-reply (0) 回应
6.限制链接数,并发连接数(connlimit)
--connlimit-upto n # 如果现有连接数小于或等于 n 则 匹配
--connlimit-above n # 如果现有连接数大于n 则匹配
7.针对 报文速率 进行限制。 秒、分钟、小时、天。
--limit rate[/second|/minute|/hour|/day] # 报文数量
--limit-burst number # 报文数量(默认:5)
15.案例
15.1案例一
要求将22,80,443以及30000-50000之间所有的端口向外暴露,其他端口拒绝
iptables -t filter -A INPUT -p TCP -m multiport --dports 22,80,443,30000:50000 -j ACCEPT
iptables -f filter -A INPUT -p TCP -j DROP
-m multiport --dports 22,80,443,30000:50000
指定模块 设置多个端口 连续多个端口
15.2案例二
要求192.168.15.1 - 192.168.15.10之间的所有IP能够连接192.168.15.81,其他拒绝
iptables -t filter -A INPUT -p TCP -m iprange --src-range 192.168.15.1-192.168.15.10 -j ACCEPT
iptables -f filter -A INPUT -p TCP -j DROP
-m iprange:指定模块一段连续的ip地址范围.
--src-range:源地址范围
15.3案例三
清空规则
要求访问数据包中包含hello word的数据不允许通过。
iptables -t filter -A INPUT -p TCP -m string --string "hello word" --algo kmp -j DROP
-m string 设置模块
--string "hello word" 指定要匹配的字符串
--algo kmp 设置算法
# 进入到服务器的html文件夹中
[root@m01 ~]# cd /usr/share/nginx/html/
[root@m01 html]# ll
-rw-r--r-- 1 root root 3650 Oct 19 07:55 404.html
-rw-r--r-- 1 root root 3693 Oct 19 07:55 50x.html
...
# 全部删除
[root@m01 html]# rm -rf ./*
# 写一个带有hello word的文件
[root@m01 html]# echo "hello word" >> index.html
# 写一个带有hello的文件
[root@m01 html]# echo "hello" >> demo.html
浏览器中输入
http://192.168.15.81/index.html
http://192.168.15.81/demo.html
可以进行访问
也可以在终端输入curl命令
# 访问
[root@prometheus ~]# curl http://192.168.15.81/index.html
hello word
[root@prometheus ~]# curl http://192.168.15.81/demo.html
hello
# 添加规则
[root@m01 html]# iptables -t filter -A INPUT -p TCP -m string --string "hello word" --algo kmp -j DROP
[root@m01 html]# iptables -t filter -L -v -n
Chain INPUT (policy ACCEPT 98 packets, 6736 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 STRING match "hello word" ALGO name kmp TO 65535
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 79 packets, 6684 bytes)
pkts bytes target prot opt in out source destination
[root@m01 html]# curl http://192.168.15.81/demo.html
hello
[root@m01 html]# curl http://192.168.15.81/index.html
# 无法访问了
# 页面的无法阻止啊....流浪器还是能访问...
15.4案例四
要求每天的12到13之间,不允许访问
iptables -t filter -A INPUT -p TCP -m time --timestart 4:00 --timestop 5:00 -j DROP
-m time 时间模块
--timestart 4:00 开始时间 12 - 8
--timestop 5:00 结束时间 13 - 8
上面的时间是UTC时间 (东0区,我们东8区 需要减去8个小时)
15.5案例五
要求别人不能ping本机,但是本机可以ping别人
iptables -t filter -A INPUT -p ICMP -m icmp --icmp-type "echo-request" -j DROP
-p ICMP ping的协议
--icmp-type "echo-request" 只请求,不回应
[root@m01 html]# iptables -F
[root@m01 html]# iptables -t filter -A INPUT -p ICMP -m icmp --icmp-type "echo-request" -j DROP
[root@m01 html]# ping www.baidu.com
PING www.a.shifen.com (112.80.248.75) 56(84) bytes of data.
...
# 另一台主机无法ping
[root@prometheus ~]# ping 192.168.15.81
PING 192.168.15.81 (192.168.15.81) 56(84) bytes of data.
15.6案例六
要求主机连接最多有2个,只能有两个主机访问.
iptables -t filter -A INPUT -p TCP --dport 22 -m connlimit --connlimit-above 2 -j DROP
-m connlimit 限制链接模块
--connlimit-above 2 并发连接数大于2个触发, 使用xshell去测试...
15.7案例七
要求限制速率在500k/s左右,
* 无法准确的控制,不精准的
iptables -t filter -A OUPUT -p TCP -m limit --limit 500k/s -j ACCEPT
iptables -t filter -A OUPUT -p TCP -j DROP
# 生成一个大的文件
[root@m01 ~]# dd if=/dev/zero of=1.txt bs=100M count=10
10+0 records in
10+0 records out
1048576000 bytes (1.0 GB) copied, 5.57425 s, 188 MB/s
# 传输文件到.71的主机去
[root@m01 ~]# scp 1.txt root@192.168.15.71:/root
The authenticity of host '192.168.15.71 (192.168.15.71)' can't be established.
ECDSA key fingerprint is SHA256:9Y7lXh4ptnVyiMKaEtfMXkWbtMPxwejGvwLl7vvsGJk.
ECDSA key fingerprint is MD5:25:74:fb:37:6c:07:f9:5c:ee:ed:a0:00:ed:07:9c:bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.15.71' (ECDSA) to the list of known hosts.
root@192.168.15.71's password:
1.txt 100% 1000MB 53.7MB/s 00:18
# 53.7MB/s 的速度
# 设置规则
[root@m01 ~]# iptables -t filter -A OUTPUT -p TCP -m limit --limit 500/s -j ACCEPT
[root@m01 ~]#iptables -t filter -A OUTPUT -p TCP -j DROP
[root@m01 ~]# iptables -L -v -n
Chain INPUT (policy ACCEPT 208 packets, 14337 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 20 packets, 1680 bytes)
pkts bytes target prot opt in out source destination
5156 123M ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 500/sec burst 5
145 1188K DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0
# 再次传输被限速了,不准确的...
[root@m01 ~]# scp 1.txt root@192.168.15.71:/root
root@192.168.15.71's password:
1.txt 0% 2976KB 1.0MB/s
15.8案例八
只允许192.168.15.0网段的IP连接本机,用两种方式实现。
iptables -t filter -A INPUT -p tcp -m iprange --src-range 192.168.15.1-192.168.15.254 -j ACCEPT
iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
# 192网段都是通过 eth0 连接
iptables -t filter -A INPUT -p tcp -i eth0 --dport 22 -j ACCEPT
iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
15.9案例九
求本机流出的数据中包含“元旦快乐”
iptables -t filter -A OUTPUT -p tcp --dport 80 -m string --string "元旦快乐" --algo kmp -j ACCEPT
iptables -t filter -A OUTPUT -p tcp --dport 80 -j DROP