您现在的位置是:网站首页> 编程资料编程资料
linux iptables防火墙中的工作常用命令_linux shell_
2023-05-26
319人已围观
简介 linux iptables防火墙中的工作常用命令_linux shell_
linux iptables防火墙-工作常用命令
Linux之iptables防火墙基础
[推荐]Linux之iptables防火墙
参考URL:https://www.jb51.net/article/165883.htm
linux系统的防火墙:IP信息包过滤u系统,它实际上由两个组件netfilter和iptables组成。
主要工作在网络层,针对IP数据包,体现在对包内的IP地址、端口、协议等信息的处理上。
数据包到达防火墙时,规则表之间的优先顺序: 首先过滤raw表里面的规则其次依次过滤> mangle > nat > filter如果所有表都没有匹配到则表示放空

- 入站数据(来自外界的数据包,且目标地址是防火墙本机) : PREROUTING --> INPUT --> 本机的应用程序
- 出站数据(从防火墙本机向外部地址发送的数据包) :本机的应用程序–> OUTPUT --> POSTROUTING 网络型防火墙
- 转发数据(需要经过防火墙转发的数据包) : PREROUTING --> FORWARD --> POSTROUTING
iptables命令行配置方法
iptables [-t 表明] 管理选项 [链名] [匹配条件] [-j 控制类型]

查看iptables防火墙规则
iptables -nvL --line-number
-L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数
-n 不对ip地址进行反查,加上这个参数显示速度会快很多
-v 输出详细信息,包含通过该规则的数据包数量,总字节数及相应的网络接口
–-line-number 显示规则的序列号,这个参数在删除或修改规则时会用到
关闭某个端口(注意同时添加ip或接口的限制)\iptables 配置只能本地ip访问某端口
iptables -I INPUT -p tcp --dport 9527 -j DROPip6tables -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服务:
到此这篇关于linux iptables防火墙-工作常用命令的文章就介绍到这了,更多相关linux iptables命令内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关内容
- 深入了解Linux的文件权限_linux shell_
- Linux top命令详解_linux shell_
- Linux Shell脚本多命令执行逻辑的示例详解_linux shell_
- Linux使用iptables实现屏蔽ip地址的示例详解_linux shell_
- Linux实现文件定期本地备份/异地备份/删除备份的脚本_linux shell_
- 详解Linux中atime,mtime,ctime的使用场景_linux shell_
- 详解Linux定时任务Crontab的介绍与使用_linux shell_
- Linux学习之expect操作详解_linux shell_
- Shell执行脚本并输出日志文件的方法_linux shell_
- shell 循环命令详解_linux shell_
