博客
关于我
iptables的增删改查
阅读量:652 次
发布时间:2019-03-15

本文共 2662 字,大约阅读时间需要 8 分钟。

iptables是自带的防火墙,功能强大,学习起来需要一段时间,下面是一些习iptables的时候的记录。如果iptables不熟悉的话可以用apf,是一款基于iptables的防火墙,挺好用的。

一,安装并启动防火墙

[root@ ~]# /etc/init.d/iptables start

当我们用iptables添加规则,保存后,这些规则以文件的形势存在磁盘上的,以为例,文件地址是/etc/sysconfig/iptables,我们可以通过命令的方式去添加,修改,删除规则,也可以直接修改/etc/sysconfig/iptables这个文件就行了。

二,添加防火墙规则

1,添加filter表

[root@linux ~]# iptables -A INPUT -p tcp -m tcp --dport 21 -j ACCEPT  //开放21端口

出口我都是开放的iptables -P OUTPUT ACCEPT,所以出口就没必要在去开放端口了。

2,添加nat表

[root@linux ~]# iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -j MASQUERADE

将源地址是 192.168.10.0/24 的数据包进行地址伪装

3,-A默认是插入到尾部的,可以-I来插入到指定位置

[root@linux ~]# iptables -I INPUT 3 -p tcp -m tcp --dport 20 -j ACCEPT[root@linux ~]# iptables -L -n --line-numberChain INPUT (policy DROP)num  target     prot opt source               destination1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/02    DROP       icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 83    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:20                //-I指定位置插的4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:225    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:806    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED7    DROP       all  --  0.0.0.0/0            0.0.0.0/0           state INVALID,NEW8    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:21                //-A默认插到最后Chain FORWARD (policy ACCEPT)num  target     prot opt source               destinationChain OUTPUT (policy ACCEPT)num  target     prot opt source               destination

三,查下iptable规则

1,查看filter表

[root@linux ~]# iptables -L -n --line-number |grep 21 //--line-number可以显示规则序号,在删除的时候比较方便5    ACCEPT     tcp  --  192.168.1.0/24       0.0.0.0/0           tcp dpt:21

如果不加-t的话,默认就是filter表,查看,添加,删除都是的

2,查看nat表

[root@linux ~]# iptables -t nat -vnL POSTROUTING --line-numberChain POSTROUTING (policy ACCEPT 38 packets, 2297 bytes)num   pkts bytes target     prot opt in     out     source               destination1        0     0 MASQUERADE  all  --  *      *       192.168.10.0/24      0.0.0.0/0

四,修改规则

[root@linux ~]# iptables -R INPUT 3 -j DROP    //将规则3改成DROP

五,删除iptables规则

[root@linux ~]# iptables -D INPUT 3  //删除input的第3条规则[root@linux ~]# iptables -t nat -D POSTROUTING 1  //删除nat表中postrouting的第一条规则[root@linux ~]# iptables -F INPUT   //清空 filter表INPUT所有规则[root@linux ~]# iptables -F    //清空所有规则[root@linux ~]# iptables -t nat -F POSTROUTING   //清空nat表POSTROUTING所有规则

六,设置默认规则

[root@linux ~]# iptables -P INPUT DROP  //设置filter表INPUT默认规则是 DROP

所有添加,删除,修改后都要保存起来,/etc/init.d/iptables save.上面只是一些最基本的操作,要想灵活运用,还要一定时间的实际操作。

转载地址:http://zdxmz.baihongyu.com/

你可能感兴趣的文章
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>