【linux】linux防火墙相关操作命令

在Linux系统中,可以使用iptables或firewalld(取决于系统使用的防火墙管理工具)来开启/关闭特定的端口。以下是两种情况的示例命令:

iptables防火墙服务

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

iptables systemctl基本命令

shell 复制代码
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	#开机启动防火墙

iptables service基本命令

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

使用iptables

开启/关闭特定端口(例如,开启/关闭端口80):

shell 复制代码
#先查看防火墙端口规则
iptables -L -n
#或
iptables -L -n --line-number

#开启80
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#丢弃(开启中的)80
sudo iptables -A INPUT -p tcp --dport 80 -j DROP
#禁止80
sudo iptables -A INPUT -p tcp --dport 80 -j REJECT

#防火墙指定IP(允许/禁止)访问指定端口
#允许访问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

使用firewalld

开启/关闭特定端口(例如,关闭端口80):

shell 复制代码
#查看防火墙状态
firewall-cmd --state
#启动防火墙
systemctl start firewalld
#设置防火墙开机启动
systemctl enable firewalld
#暂停止防火墙
systemctl stop firewalld
#永久关闭防火墙
systemctl disable firewalld
#查看所有开放的端口
sudo firewall-cmd --zone=public --list-ports
#开启
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
#关闭
sudo firewall-cmd --permanent --zone=public --remove-port=80/tcp
#重载
sudo firewall-cmd --reload
相关推荐
梁辰兴1 分钟前
计算机网络基础:使用集线器的星型拓扑
服务器·网络·计算机网络·集线器·计算机网络基础·梁辰兴·星型拓扑
jimy12 分钟前
安卓里运行Linux
linux·运维·服务器
咕噜签名-铁蛋34 分钟前
PyTorch:深度学习框架的创新之路与技术实践
服务器
哟哟耶耶1 小时前
js-fetch流式实现中断重连
运维·服务器
爱凤的小光1 小时前
Linux清理磁盘技巧---个人笔记
linux·运维
若尘啊若辰1 小时前
安全通用要求之六安全管理制度
安全·网络安全·等保·等级保护·安全通用要求
TG:@yunlaoda360 云老大1 小时前
如何在华为云国际站代理商控制台进行SFS Turbo的性能与容量核查?
服务器·网络·数据库·华为云
TG:@yunlaoda360 云老大2 小时前
华为云国际站代理商的CBR主要有什么作用呢?
java·网络·华为云
耗同学一米八2 小时前
2026年河北省职业院校技能大赛中职组“网络建设与运维”赛项答案解析 1.系统安装
linux·服务器·centos
无锡耐特森2 小时前
ModbusTCP转CC-Link网关解锁直线电机性能上限
网络