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
相关推荐
上海云盾-小余1 小时前
流量攻击复盘:为什么 WAF 完好,业务依旧瘫痪
运维·服务器·网络
IT大白鼠1 小时前
Docker 私有仓库管理:Harbor 企业级仓库搭建与镜像全生命周期管理
运维·docker·容器
戴西软件1 小时前
戴西iDWS.3DViz Suite数据轻量化可视化软件,从传统桌面软件向云端协同的重大突破
大数据·运维·网络·人工智能·机器学习·3d
芯盾时代1 小时前
《金融业网络安全管理办法(征求意见稿)》全条款深度拆解(三)
网络·安全·网络安全
米饭不加菜1 小时前
ESP-01S通过TCP直接与手机建立通信
网络·tcp/ip·智能手机
闲云野鹤在人间2 小时前
OpenStack架构介绍和安装流程
linux·网络·架构·openstack
我命由我123453 小时前
Linux - Linux/POSIX 路径斜杠折叠规则
linux·运维·服务器·android studio·android jetpack·android-studio·android runtime
姚不倒3 小时前
Nginx 反向代理:5 种负载均衡策略与实战
运维·nginx·负载均衡
xx~t3 小时前
嵌入式——Linux软件编程——网络1
linux·c语言·网络·vscode·网络协议
Uncertainty!!3 小时前
Linux 主机无法完成校园网认证排查记录:从 DHCP、ARP 到 Portal 认证流程分析
linux·运维·服务器