文章目录

  • linux iptables防火墙-工作常用命令
  • Linux之iptables防火墙基础
  • iptables命令行配置方法
  • 查看和删除iptables防火墙规则
  • 根据规则id删除某条规则
  • 使用nc查看某个tcp端口是否开放
  • 插入限制源ip为本地访问某个tcp端口到规则最前面
  • 关闭某个端口(注意同时添加ip或接口的限制)\iptables 配置只能本地ip访问某端口
  • 防火墙规则放开自己的ip,让自己的ip可以访问
  • iptables永久生效
  • 报错:The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.


linux iptables防火墙-工作常用命令

Linux之iptables防火墙基础

[推荐]Linux之iptables防火墙
参考URL:

linux系统的防火墙:IP信息包过滤u系统,它实际上由两个组件netfilter和iptables组成。
主要工作在网络层,针对IP数据包,体现在对包内的IP地址、端口、协议等信息的处理上。

数据包到达防火墙时,规则表之间的优先顺序: 首先过滤raw表里面的规则其次依次过滤> mangle > nat > filter如果所有表都没有匹配到则表示放空

IPtables 翻火墙 实验 iptables防火墙命令_IPtables 翻火墙 实验

  • 入站数据(来自外界的数据包,且目标地址是防火墙本机) : PREROUTING --> INPUT --> 本机的应用程序
  • 出站数据(从防火墙本机向外部地址发送的数据包) :本机的应用程序–> OUTPUT --> POSTROUTING 网络型防火墙
  • 转发数据(需要经过防火墙转发的数据包) : PREROUTING --> FORWARD --> POSTROUTING

iptables命令行配置方法

iptables [-t 表明] 管理选项 [链名] [匹配条件] [-j 控制类型]

IPtables 翻火墙 实验 iptables防火墙命令_linux_02

查看和删除iptables防火墙规则

iptables -nvL --line-number

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

根据规则id删除某条规则

如果之前添加的规则的编号为 1,您可以使用以下命令删除它:

iptables -L --line-numbers
iptables -D INPUT 1

使用nc查看某个tcp端口是否开放

nc -zv 192.168.1.3 8888

插入限制源ip为本地访问某个tcp端口到规则最前面

对于 iptables -I INPUT -p tcp --dport 8888 -j ACCEPT 命令,您需要将 替换为要插入规则的目标位置的规则编号。如果想要将新规则插入到开头,则可以使用1作为规则编号。

iptables -I INPUT 1 -p tcp --dport 8888 -s 127.0.0.1 -j ACCEPT

关闭某个端口(注意同时添加ip或接口的限制)\iptables 配置只能本地ip访问某端口

iptables -I INPUT -p tcp --dport 9527 -j DROP
ip6tables -I INPUT -p tcp --dport 9527 -j DROP

注意:工作中切记不要直接使用上面命令,除非在这个命令之前还有一条放过lo接口的防火墙规则。
一般情况,我们目的都是某个端口不让外网访问,这样操作,就127.0.0.1访问这个端口也被限制了。

一定要记得,添加ip或接口的条件~
例如:

iptables -I INPUT ! -i lo  -p tcp --dport 9527 -j DROP
ip6tables -I INPUT ! -i lo  -p tcp --dport 9527 -j DROP

iptables -I INPUT ! -d 127.0.0.1 -p tcp --dport 9527 -j DROP

iptables 是控制ipv4的,ip6tables 是控制ipv6的

注意 感叹号的位置,亲测可用.~

防火墙规则放开自己的ip,让自己的ip可以访问

iptables -I INPUT -p tcp -s 192.168.11.1 -j ACCEPT

开放某个tcp、某个udp。

iptables -I INPUT -p tcp --dport 9527 -j ACCEPT
iptables -I INPUT -p tcp --dport 9527 -j ACCEPT

iptables永久生效

第一种方法
执行命令service iptables save

第二种方法
执行命令iptables-save > xxx写入到一个文件,开机以后执行命令iptables-restore < xxx用来恢复。

报错:The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

问题描述:
执行命令service iptables save 报错The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

问题分析:
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.
服务命令只支持基本的LSB操作(启动、停止、重启、尝试重启、重载、强制重载、状态)。对于其他操作,请尝试使用systemctl。

centos新版本,firewalld 被引入代替 iptables 了,所以再使用 service iptables save 保存 iptables 规则时,就会出现上述的报错。
service命令只保留下了极少部分使用,大部分命令都要改用systemctl使用。

这是因为没有安装iptables服务,直接使用yum安装iptables服务即可.

解决方法:

1.systemctl stop firewalld --关闭防火墙
2.yum install iptables-services --安装或更新服务

yum install iptables-services

3.systemctl enable iptables --允许开机启动iptables
4.systemctl start iptables --启动iptables
5.service iptables save --保存设置
6.service iptables restart --重启iptables服务: