Linux学习(二十八)iptables (二) iptables规则语法

查看iptables规则:

[[email protected]0002 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
 1786  140K 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
    1    64 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
  122 10168 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)
 pkts bytes target     prot opt in     out     source               destination
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 1513 packets, 135K bytes)
 pkts bytes target     prot opt in     out     source               destination    

在这条命令中我们没有指定表名,那么它显示的 就是filter表的规则。现在我们还没有写任何的规则,那么它读取的就是默认的规则。我们可以在/etc/sysconfig/iptables中看到默认的规则。

vim /etc/sysconfig/iptables:

# sample configuration for iptables service
# you can edit this manually or use system-config-firewall
# please do not ask us to add additional ports/services to this default configuration
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

指定表:

[[email protected]0002 ~]# iptables -t nat -nvL
Chain PREROUTING (policy ACCEPT 49 packets, 4222 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 1 packets, 64 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 1 packets, 71 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 1 packets, 71 bytes)
 pkts bytes target     prot opt in     out     source               destination       

清空规则:

[[email protected]0002 ~]# iptables -F
[[email protected]-0002 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 43 packets, 3132 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 29 packets, 2516 bytes)
 pkts bytes target     prot opt in     out     source               destination         

清空规则后,如果不保存,重启后将恢复到原来的规则。

保存:

[[email protected]0002 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]
[[email protected]-0002 ~]# vim /etc/sysconfig/iptables

重启服务:

[[email protected]0002 ~]# service iptables restart
Redirecting to /bin/systemctl restart  iptables.service

将计数器清零:

[[email protected] ~]# iptables -nvL
Chain INPUT (policy ACCEPT 18M packets, 2965M 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 15M packets, 5501M bytes)
 pkts bytes target     prot opt in     out     source               destination
[[email protected] ~]# iptables -Z
[[email protected] ~]# iptables -nvL
Chain INPUT (policy ACCEPT 49 packets, 2984 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 33 packets, 2456 bytes)
 pkts bytes target     prot opt in     out     source               destination         

添加一条规则:

iptables -A INPUT -s 110.229.26.253 --dport 80 REJECT

这条规则的意思是把进入INPUT链的ip是110.229.26.253访问80端口的请求给拒绝。简而言之就是不让这个ip访问我们的80端口。

删除上面那条规则:

iptables -D INPUT -s 110.229.26.253 --dport 80 REJECT

除了这样删除之外,还有另一种删除方法:

首先得到这条规则的序号:

[[email protected] ~]# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 2462 packets, 554K bytes)
num   pkts bytes target     prot opt in     out     source               destination
1      277 22324 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            multiport dports 20,21,80

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

Chain OUTPUT (policy ACCEPT 2457 packets, 562K bytes)
num   pkts bytes target     prot opt in     out     source               destination     

然后根据序列号删除:

[[email protected] ~]# iptables -D INPUT 1
[[email protected] ~]# iptables -nvL
Chain INPUT (policy ACCEPT 48 packets, 3008 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 35 packets, 3614 bytes)
 pkts bytes target     prot opt in     out     source               destination         

除了用-A来添加规则,我们还可以用-I来添加规则,它的意思是,将规则插入到最前面:

[[email protected] ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP
[[email protected] ~]# iptables -nvL
Chain INPUT (policy ACCEPT 71 packets, 4425 bytes)
 pkts bytes target     prot opt in     out     source               destination
   74  6216 DROP       icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 8

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

Chain OUTPUT (policy ACCEPT 55 packets, 11135 bytes)
 pkts bytes target     prot opt in     out     source               destination    

这条规则的作用是不让别人Ping你的机器。

看看前后Ping的状态:

#设置iptables之前[[email protected]0002 ~]# ping 101.200.168.135
PING 101.200.168.135 (101.200.168.135) 56(84) bytes of data.
64 bytes from 101.200.168.135: icmp_seq=1 ttl=128 time=16.1 ms
64 bytes from 101.200.168.135: icmp_seq=2 ttl=128 time=13.7 ms
64 bytes from 101.200.168.135: icmp_seq=3 ttl=128 time=13.2 ms
^C
--- 101.200.168.135 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 13.252/14.370/16.135/1.262 ms#设置之后
[[email protected]-0002 ~]# ping 101.200.168.135
PING 101.200.168.135 (101.200.168.135) 56(84) bytes of data.

设置链的默认状态:

[[email protected] ~]# iptables -P INPUT ACCEPT
[[email protected] ~]# iptables -nvL
Chain INPUT (policy ACCEPT 45 packets, 2732 bytes)
 pkts bytes target     prot opt in     out     source               destination
  212 17808 DROP       icmp --  *      *       0.0.0.0/0            0.0.0.0/0            icmptype 8

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

Chain OUTPUT (policy ACCEPT 31 packets, 3454 bytes)
 pkts bytes target     prot opt in     out     source               destination   
时间: 2024-08-05 03:42:08

Linux学习(二十八)iptables (二) iptables规则语法的相关文章

二十八条社会潜规则

今天登录简书逛了一会,无意中看到一篇有意思的文章,感觉有点道理,于是顺便摘抄了下来,与大家共勉. 二十八条社会潜规则:先学会不生气,再学会气死人 1.能在一定位置上的人,一定有他的过人之处,不管你多么讨厌他. 2.要想屏蔽某些人的朋友圈,最好把他同事微信分到一个组里,要屏蔽一起都屏蔽了. 3.不要总在旁人面前提你的朋友多牛逼,你要懂得,别人的成就与你无关. 4.朋友同事之间,帮忙是情分,不帮忙是本分,不要把别人对你的好,当作理所当然. 5.和同事拼单买东西叫外卖,一定把支付明细的截图发给每个人,

Linux学习(十八)软件安装与卸载(一)rpm和yum安装与卸载软件

一.Linux下安装软件的三种方式 在Linux下安装软件有三种方式:rpm安装,yum安装,源码包安装.接下来,我们对这三种安装方式一一进行讲解. 二.rpm安装 rpm安装类似于windows自带的"安装/卸载",通过rpm命令我们可以安装一些现成的二进制包.rpm安装的包的安装路径是固定的. 安装好光盘后,将它挂载到/mnt/,然后进到光盘中的Packages目录中,发现这里有很多包: [[email protected] ~]# df -h 文件系统 容量 已用 可用 已用%

Linux学习笔记<十八>——内核编译

内核由核心和内核模块两部分组成 核心:/boot/vmlinuz-version 内核模块(ko):/lib/modules/version/ 查看内核版本 uname -r 主版本号.次版本号(偶数表示稳定版本,奇数表示测试版本).修订版本号(修订的次数) 用户空间访问.监控内核是通过访问修改/proc,/sys目录下的文件(即设定内核的参数值)实现的 /proc/sys:此目录中的文件很多是可读写的 /sys:某些文件可写 设定内核参数值的方法: 1.echo VALUE > /proc/s

二十八、Linux下Vim工具常用命令

在linux下做开发,甚至是只做管理维护工作,也少不了Vim的使用.作为一个新手,我也是刚刚接触,本节将我日常使用或收集的Vim常用命令记录下来. 当然,直接在命令行上输入:vimtutor,就可以学习到Vim的所有命令了.Vim很强大,很多牛人在vim里集成很多插件什么的,但这里只介绍基本vim命令 移动命令 h "左 j "下 k "上 l "右 w "光标移动到下一个单词的首字符 a word forward b "光标移动到上一个单词的首

攻城狮在路上(叁)Linux(二十八)--- 打包命令:tar

首先介绍一下tar打包命令的基本格式,下面的三种之间不能混淆. tar [-j|-z] [cv] [-f 新文件名] file1 file2 ...; tar [-j|-z] [tv] [-f 新文件名]; <== 查看文件名 tar [-j|-z] [xv] [-f 新文件名] [-C 目录]; <== 解包 参数说明: -c:新建打包文件,搭配-v来查看过程中被打包的文件名 -t:查看打包文件的内容包含哪些文件名 -x:解包或解压缩功能.可搭配大写C来指定解压目录 ------------

【Unity 3D】学习笔记二十八:unity工具类

unity为开发者提供了很多方便开发的工具,他们都是由系统封装的一些功能和方法.比如说:实现时间的time类,获取随机数的Random.Range( )方法等等. 时间类 time类,主要用来获取当前的系统时间. using UnityEngine; using System.Collections; public class Script_04_13 : MonoBehaviour { void OnGUI() { GUILayout.Label("当前游戏时间:" + Time.t

angular学习笔记(二十八)-$http(6)-使用ngResource模块构建RESTful架构

ngResource模块是angular专门为RESTful架构而设计的一个模块,它提供了'$resource'模块,$resource模块是基于$http的一个封装.下面来看看它的详细用法 1.引入angular-resource.min.js文件 2.在模块中依赖ngResourece,在服务中注入$resource var HttpREST = angular.module('HttpREST',['ngResource']); HttpREST.factory('cardResource

winform学习日志(二十八)----------将汉字转化为拼音,正则表达式和得到汉字的Unicode编码

一:上图,不清楚的看代码注解,很详细了 二:具体代码 窗体代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Text.RegularExpressio

马哥学习笔记二十八——nginx反向代理,负载均衡,缓存,URL重写及读写分离

Nginx反向代理 Nginx通过proxy模块实现反向代理功能.在作为web反向代理服务器时,nginx负责接收客户请求,并能够根据URI.客户端参数或其它的处理逻辑将用户请求调度至上游服务器上(upstream server).nginx在实现反向代理功能时的最重要指令为proxy_pass,它能够将location定义的某URI代理至指定的上游服务器(组)上.如下面的示例中,location的/uri将被替换为上游服务器上的/newuri. location /uri { proxy_pa

javaweb学习总结(二十八)——JSTL标签库之核心标签【转】

原文地址:javaweb学习总结(二十八)——JSTL标签库之核心标签 一.JSTL标签库介绍 JSTL标签库的使用是为弥补html标签的不足,规范自定义标签的使用而诞生的.使用JSLT标签的目的就是不希望在jsp页面中出现java逻辑代码 二.JSTL标签库的分类 核心标签(用得最多) 国际化标签(I18N格式化标签) 数据库标签(SQL标签,很少使用) XML标签(几乎不用) JSTL函数(EL函数) 三.核心标签库使用说明 JSTL的核心标签库标签共13个,使用这些标签能够完成JSP页面的