You can use the following script to open ports 3306 and 8080 in batches, and output all opened ports:
#!/bin/bash
# 批量开放端口
for port in 3306 8080; do
firewall-cmd --zone=public --add-port=$port/tcp --permanent
done
# 重载防火墙配置
firewall-cmd --reload
# 查看已经开放的端口
echo "已经开放的端口:"
firewall-cmd --list-ports
The script uses for
a loop to open ports 3306 and 8080 in batches, and uses firewall-cmd
commands to configure firewall rules. Among them, --zone=public
the open port is a public area, --add-port=$port/tcp
and the open port number is a variable $port
, --permanent
which means that it is permanently effective, and the firewall configuration needs to be reloaded to take effect. Finally, use firewall-cmd --list-ports
the command to output the ports that have been opened.