iptables规则的查看、添加、删除和修改

1、查看

iptables -nvL --line-number (这个命令跟/etc/init.d/iptables status 输出差不多)
  • -L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数
  • -n 不对ip地址进行查,加上这个参数显示速度会快很多
  • -v 输出详细信息,包含通过该规则的数据包数量,总字节数及相应的网络接口
  • –line-number 显示规则的序列号,这个参数在删除或修改规则时会用到

2、添加
添加规则有两个参数:-A和-I。其中-A是添加到规则的末尾;-I可以插入到指定位置,没有指定位置的话默认插入到规则的首部
例如:
当前规则:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP all -- 192.168.1.1 0.0.0.0/0
2 DROP all -- 192.168.1.2 0.0.0.0/0
3 DROP all -- 192.168.1.4 0.0.0.0/0

添加一条规则到尾部:

[root@test ~]# iptables -A INPUT -s 192.168.1.5 -j DROP

再插入一条规则到第三行:

[root@test ~]# iptables -I INPUT 3 -s 192.168.1.3 -j DROP

查看:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP all -- 192.168.1.1 0.0.0.0/0
2 DROP all -- 192.168.1.2 0.0.0.0/0
3 DROP all -- 192.168.1.3 0.0.0.0/0
4 DROP all -- 192.168.1.4 0.0.0.0/0
5 DROP all -- 192.168.1.5 0.0.0.0/0

可以看到192.168.1.3插入到第三行,而原来的第三行192.168.1.4变成了第四行。

3、删除
删除用-D参数

删除之前添加的规则(iptables -A INPUT -s 192.168.1.5 -j DROP):

[root@test ~]# iptables -D INPUT -s 192.168.1.5 -j DROP

有时候有些规则太长,删除时要写一大串,既浪费时间又容易写错,这时我们可以先使用–line-number查看出该条规则的行号,再通过行号删除

[root@test ~]# iptables -nv --line-number
iptables v1.4.7: no command specified
Try `iptables -h' or 'iptables --help' for more information.
[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP all -- 192.168.1.1 0.0.0.0/0
2 DROP all -- 192.168.1.2 0.0.0.0/0
3 DROP all -- 192.168.1.3 0.0.0.0/0

删除第二行规则

[root@test ~]# iptables -D INPUT 2

4、修改
修改使用-R参数

将第三行规则改为ACCEPT
先看下当前规则:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP all -- 192.168.1.1 0.0.0.0/0
2 DROP all -- 192.168.1.2 0.0.0.0/0
3 DROP all -- 192.168.1.5 0.0.0.0/0

修改:

[root@test ~]# iptables -R INPUT 3 -j ACCEPT

再查看下:

[root@test ~]# iptables -nL --line-number
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 DROP all -- 192.168.1.1 0.0.0.0/0
2 DROP all -- 192.168.1.2 0.0.0.0/0
3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0

在mysql配置中某些linux系统中防火墙默认是阻止3306端口的,我们要是想访问mysql数据库,我们需要这个端口,命令如下:

1 /sbin/iptables -I INPUT -p tcp --dport 3036 -j ACCEPT

我们需要保存我们的操作,命令如下:

1  /etc/rc.d/init.d/iptables save

此时我们可以查看端口的状态,命令如下:

1 /etc/init.d/iptables status

当然如果你打开其他端口也一样,只需要把这个端口号换乘你需要的端口号即可。

#关闭防火墙
/etc/init.d/iptables stop
service iptables stop # 停止服务
#查看防火墙信息
/etc/init.d/iptables status

#开放端口:8080
/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
#重启防火墙以便改动生效:(或者直接重启系统)
/etc/init.d/iptables restart
#将更改进行保存
/etc/rc.d/init.d/iptables save

另外直接在/etc/sysconfig/iptables中增加一行:
-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 8080 -j ACCEPT

#永久关闭防火墙
chkconfig –level 35 iptables off

我们可以直接编辑iptables文件

文件位置:

/etc/sysconfig/iptables

有次遇到这个问题:

nginx请求php,返回502 bad gateway.

2016/03/21 16:29:44 [error] 8898#0: *1 connect() failed (113: No route to host) while connecting to upstream, client: 14.120.192.32, server: localhost, request: "GET /phpinfo.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "xxx:81" 。

nginx请求php文件怎么出错了,nginx和php是同一台主机,php-fpm 9000不需要开放,同一台主机localhost请求怎么就报错了呢?

要把环回打开:

iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT
先把“回环”打开,以免有不必要的麻烦。

iptables -A INPUT -i eth+ -p icmp --icmp-type 8 -j ACCEPT
iptables -A OUTPUT -o eth+ -p icmp --icmp-type 0 -j ACCEPT
在所有网卡上打开ping功能,便于维护和检测。

参考:http://blog.chinaunix.net/uid-23078678-id-2554520.html

http://www.cnblogs.com/ccdc/archive/2012/04/24/2468048.html

iptables使用的更多相关文章

  1. iptables

    一.在服务器上打开 22.80.9011端口: iptables -A INPUT -p tcp --dport 9011 -j ACCEPT iptables -A OUTPUT -p tcp -- ...

  2. 浅谈iptables 入站 出站以及NAT实例

    --------------本文是自己工作上的笔记总结,适合的可以直接拿去用,不适合的,适当修改即可!--------------- iptbales默认ACCEPT策略,也称通策略,这种情况下可以做 ...

  3. Failed to stop iptables.service: Unit iptables.service not loaded.

    redhat 7 [root@lk0 ~]# service iptables stop Redirecting to /bin/systemctl stop iptables.service Fai ...

  4. CentOS7安装iptables防火墙

    CentOS7默认的防火墙不是iptables,而是firewalle. 安装iptable iptable-service #先检查是否安装了iptables service iptables st ...

  5. linux iptables常用命令之配置生产环境iptables及优化

    在了解iptables的详细原理之前,我们先来看下如何使用iptables,以终为始,有可能会让你对iptables了解更深 所以接下来我们以配置一个生产环境下的iptables为例来讲讲它的常用命令 ...

  6. CentOS系统配置 iptables防火墙

    阿里云CentOS系统配置iptables防火墙   虽说阿里云推出了云盾服务,但是自己再加一层防火墙总归是更安全些,下面是我在阿里云vps上配置防火墙的过程,目前只配置INPUT.OUTPUT和FO ...

  7. /etc/sysconfig/下找不到iptables文件解决方法

    时间:2014-12-19 01:17来源:csdn 作者:大智 举报 点击:5639次 本想做些防火墙策略.防火墙策略都是写在/etc/sysconfig/iptables文件里面的.可我发现我也没 ...

  8. docker通过iptables修改或新增镜像映射端口

    443 8088 22 端口是初始映射端口 [root@SERVER ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAM ...

  9. lnmp 预设iptables设置

    「LNMP」iptables初始配置   首先使用命令iptables -P INPUT ACCEPT允许所有连接,否则容易把自己关在外边.然后使用iptables -F;iptables -X;ip ...

  10. 关闭SELinux和iptables防火墙

    1.关闭SELinux: 编辑SELinux配置文件: [root@Redis selinux]# vim /etc/selinux/config 修改SELINUX配置项为disable SELIN ...

随机推荐

  1. Bootstrap页面布局7 - Bootstrap响应式布局的实用类

    在bootstrap-responsive.css这个CSS样式表中已经为我们设定好了几个实用的类: .visible-phone: 在智能手机设备上显示这个元素,在其他设备上隐藏该元素 .visib ...

  2. LR中Vugen的多进程与多线程(脚本命令行)

    Controller使用驱动程序(如mdrv.exe或r3vuser.exe)来运行Vuser.用户可以在Controller的run-time setting中选择Vuser的运行方式:多进程/多线 ...

  3. thinkphp的save方法失败

    如果用下面的方式更新数据时, $data['link_phone'] = I('post.link_phone'); $flag1 = $order->save ($data); $data一定 ...

  4. the core or essence of a computer

    COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The ALU is that part ...

  5. 6严格的HTML:遵循标准,合乎规范

    标准的网页加载速度更快,并且在其他一些冲浪设备(如电话和电视)上运行地更好.如果没有告诉浏览器使用的HTML版本,许多浏览器将使用"转换显示"模式,而这可能会导致在不同浏览器有不一 ...

  6. Intel Visual Fortran Compiler 11调用lapack库实现并行多处理计算

    采用fortran进行数值计算的朋友们都应该听说过大名鼎鼎的lapack库,我就不多做介绍了,在此,我只是介绍一个编译好的lapack二进制包ACML(AMD Core Math Library),并 ...

  7. openCV中IplImage的使用

    http://blog.csdn.net/welcome_xu/article/details/7650680 IplImage结构详细分析   IplImage 结构解读: typedef stru ...

  8. ASP.NET网页生成EXCEL并下载(利用DataGrid或GridView等)

    前几天要在后台查询数据库内容(用entity framework),将查询出来的信息(List或DataTable形式)转成EXCEL供用户下载.经过谷歌.百度搜索,终于搜出了一些代码.似乎可用了,结 ...

  9. 20145319 《java程序设计》课程总结

    20145319 <Java程序设计>课程总结 读书笔记链接总结 - 20145319 第一周学习总结 - 20145319 第二周学习总结 - 20145319 第三周学习总结 - 20 ...

  10. FW nexus docker

    原文地址: http://www.cnblogs.com/wzy5223/p/5410990.html Nexus 3.0 可以创建三种docker仓库: 1. docker (proxy)     ...