iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分。可以直接配置,也可以通过许多前端和图形界面配置。
使用root用户登录,运行以下命令:
#先检查是否安装了iptables
service iptables status
#安装iptables
yum install -y iptables
#升级iptables
yum update iptables
#安装iptables-services
yum install iptables-services
使用root用户登录,运行以下命令:
#开机自启动iptables服务
systemctl enable iptables.service
#启动服务
systemctl start iptables.service
#重启服务
systemctl restart iptables.service
#查看状态
systemctl status iptables.service
使用root用户登录,运行以下命令:
#停止firewalld服务
systemctl stop firewalld
#禁用firewalld服务
systemctl mask firewalld
使用root用户登录,运行以下命令:
#查看iptables现有规则
iptables -L -n
#先允许所有,不然有可能会杯具
iptables -P INPUT ACCEPT
#清空所有默认规则
iptables -F
#清空所有自定义规则
iptables -X
使用root用户登录,运行以下命令:
#开放22端口
iptables -A INPUT -p tcp –dport 22 -j ACCEPT
使用root用户登录,运行以下命令:
#保存上述规则
service iptables save
centos 7版本以后默认使用firewalld,所以使用firewalld是趋势,要及时跟上脚步。
使用root用户登录,运行以下命令:
#启动
systemctl start firewalld
#查看状态
systemctl status firewalld 或者 firewall-cmd –state
#停止
systemctl disable firewalld
#禁用
systemctl stop firewalld
使用root用户登录,运行以下命令:
#开放端口
firewall-cmd –zone=public –add-port=8080/tcp –permanent
#禁用端口
firewall-cmd –zone=public –remove-port=80/tcp –permanent
使用root用户登录,运行以下命令:
#查看所有打开的端口
firewall-cmd –zone=public –list-all
#更新防火墙规则
firewall-cmd –reload