Linux防火墙firewalld&iptables(2)iptables开放指定端口开放指定端口

一、CentOs6 iptables基本操作

bash 复制代码
# chkconfig --list | grep iptables	查看防火墙的服务
# chkconfig iptables off	永久关闭防火墙
#chkconfig iptables on	永久开启防火墙

# service status iptables 	查看防火墙状态
# service start iptables		启动防火墙
# service stop iptables		停止防火墙
# service restart iptables	重启防火墙

# /etc/init.d/iptables status		查看防火墙状态
# /etc/init.d/iptables start		启动防火墙
# /etc/init.d/iptables stop		停止防火墙
# /etc/init.d/iptables restart	重启防火墙

二、CentOs7 iptables基本操作

bash 复制代码
## 2.1 防火墙服务

#chkconfig --list | grep iptables	查看防火墙的服务
#chkconfig iptables off	永久关闭防火墙
#chkconfig iptables on	永久开启防火墙

## 2.2 systemctl基本命令

#systemctl status iptables.service	查看防火墙的状态
#systemctl stop iptables.service	停止防火墙
#systemctl start iptables.service	启动防火墙
#systemctl restart iptables.service	重启防火墙
#systemctl reload iptables.service	重载防火墙等价于restart
#systemctl disable iptables.service	禁止开机启动防火墙
#systemctl enable iptables.service	开机启动防火墙

## 2.3 service基本命令

#service iptables status    查看防火墙状态        
#service iptables start	启动防火墙
#service iptables stop	停止防火墙
#service iptables restart	重启防火墙
#service iptables try-restart	尝试重启防火墙
#service iptables reload	重载防火墙
#service iptables force-reload	强制重载防火墙

三、防火墙端口(开放/阻止)

3.1 查询防火墙开放端口

bash 复制代码
iptables -L -n
或
iptables -L -n --line-number

3.2 开放防火墙指定端口

3.2.1 允许所有服务器访问指定端口

例如放开常用端口22/80/8080/3306

bash 复制代码
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

--参数说明

参数 说明
-A 添加一条INPUT的规则
-p 指定是什么协议(TCP/UDP)
--dport 就是目标端口,当数据从外部进入服务器为目标端口
--sport 数据从服务器出去,则为数据原端口
-j 就是指定是ACCEPT接受或阻止DROP
-s 来源
bash 复制代码
重启服务
service iptables restart
或
systemctl restart iptables.service

保存配置

bash 复制代码
service iptables save
或
/etc/rc.d/init.d/iptables save

3.2.2 防火墙指定IP(允许/禁止)访问

bash 复制代码
# 允许ip访问
iptables -A INPUT -p tcp -s 192.168.xx.x -j ACCEPT
# 禁止ip访问
iptables -A INPUT -p tcp -s 192.168.xx.x -j DROP

--参数说明: -s 来源

3.2.3 防火墙指定IP(允许/禁止)访问指定端口

bash 复制代码
# 允许访问3306
iptables -A INPUT -s 192.168.xx.x -p tcp -m tcp --dport 3306 -j ACCEPT
# 禁止访问3306
iptables -A OUTPUT -s 192.168.xx.x -p tcp -m tcp --sport 3306 -j DROP

重启服务

bash 复制代码
service iptables restart
或
systemctl restart iptables.service

保存配置

bash 复制代码
service iptables save
或
/etc/rc.d/init.d/iptables save

四、删除防火墙端口

先查看防火墙端口规则

bash 复制代码
iptables -L -n
或
iptables -L -n --line-number

再根据序号删除指定端口的防火墙规则

bash 复制代码
iptables -D INPUT 1
iptables -D INPUT 2
iptables -D INPUT 3
iptables -D INPUT 4
相关推荐
运维小贺3 分钟前
各服务器厂商调整BIOS睿频教程
linux·运维·服务器·性能优化
网硕互联的小客服6 分钟前
如何排查服务器中已经存在的后门程序?
运维·服务器·github
人生匆匆7 分钟前
docker进入启动失败的容器
运维·docker·容器
Fanmeang9 分钟前
OSPF路由过滤
运维·网络·华为·ip·路由·ospf·路由过滤
特种加菲猫24 分钟前
指尖上的魔法:优雅高效的Linux命令手册
linux·笔记
★Orange★42 分钟前
Linux Kernel kfifo 实现和巧妙设计
linux·运维·算法
NetX行者1 小时前
FastMCP:用于构建MCP服务器的开源Python框架
服务器·python·开源
我是阿呆同学1 小时前
仿mudou库one thread oneloop式并发服务器
网络
bemyrunningdog1 小时前
Mock数据
linux·运维·ubuntu
是阿建吖!1 小时前
【Linux | 网络】网络编程套接字
linux·网络