开放之前可以查看下哪些端口开放了,使用命令netstat -ntpl
[root@localhost ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1110/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1190/master
tcp 0 0 :::22 ::😗 LISTEN 1110/sshd
tcp 0 0 ::1:25 ::😗 LISTEN 1190/master
[root@localhost ~]#
如果要开放MySQL数据库端口3306,使用下面的命令
1
2
3
/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables restart
执行过程登录
[root@localhost ~]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
[root@localhost ~]# /etc/rc.d/init.d/iptables save
iptables:将防火墙规则保存到 /etc/sysconfig/iptables: [确定]
[root@localhost ~]# /etc/init.d/iptables restart
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
iptables:应用防火墙规则: [确定]
[root@localhost ~]#
如果要关闭端口,使用下面的命令
1
2
3
4
/sbin/iptables -I INPUT -p tcp --dport 3306 -j DROP
/sbin/iptables -I OUTPUT -p tcp --sport 3306 -j DROP
/etc/rc.d/init.d/iptables save
/etc/init.d/iptables restart
CentOS迷你版没有firewall。需要安装后才能使用。
安装 yum install firewalld
systemctl start firewalld # 启动,
systemctl enable firewalld # 开机启动
systemctl stop firewalld # 关闭
systemctl disable firewalld # 取消开机启动
开放端口和关闭端口
1
2
3
firewall-cmd --zone=public --add-port=8090/tcp --permanent
firewall-cmd --zone=public --remove-port=8090/tcp --permanent
使配置生效
1