CentOS默认安装了firewalld,在 RHEL7 系统中,firewall 取代了 iptables。在 service 层面,两者是平级的,firewall 还是调用了 iptables 的 command,去执行内核的 netfilter。
firewalld 和 iptables service 之间最本质的不同是:
iptables service 在 /etc/sysconfig/iptables 中储存配置,而 firewalld 将配置储存在 /usr/lib/firewalld/ 和 /etc/firewalld/ 中的各种 XML 文件里。要注意,当 firewalld 在Red Hat Enterprise Linux上安装失败时, /etc/sysconfig/iptables 文件就不存在。
使用 iptables service,每一个单独更改意味着清除所有旧有的规则和从 /etc/sysconfig/iptables里读取所有新的规则,然而使用 firewalld 却不会再创建任何新的规则;仅仅运行规则中的不同之处。因此,firewalld 可以在运行时间内,改变设置而不丢失现行连接。
使用 iptables tool 与内核包过滤对话也是如此。
通过Zone的值指定
drop: 丢弃所有进入的包,而不给出任何响应 block: 拒绝所有外部发起的连接,允许内部发起的连接 public: 允许指定的进入连接 external: 同上,对伪装的进入连接,一般用于路由转发 dmz: 允许受限制的进入连接 work: 允许受信任的计算机被限制的进入连接,类似 workgroup home: 同上,类似 homegroup internal: 同上,范围针对所有互联网用户 trusted: 信任所有连接以下都是指在public的zone下的操作,不同的Zone只要改变Zone后面的值就可以 添加:
firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效)重新载入:
firewall-cmd --reload查看:
firewall-cmd --zone=public --query-port=80/tcp删除:
firewall-cmd --zone=public --remove-port=80/tcp --permanent以smtp服务为例, 添加到work zone 添加:
firewall-cmd --zone=work --add-service=smtp查看:
firewall-cmd --zone=work --query-service=smtp删除:
firewall-cmd --zone=work --remove-service=smtp查看:
firewall-cmd --zone=external --query-masquerade打开:
firewall-cmd --zone=external --add-masquerade关闭:
firewall-cmd --zone=external --remove-masquerade打开端口转发,首先需要打开IP地址伪装
firewall-cmd --zone=external --add-masquerade转发 tcp 22 端口至 3753:
firewall-cmd --zone=external --add-forward-port=22:porto=tcp:toport=3753转发端口数据至另一个IP的相同端口:
firewall-cmd --zone=external --add-forward-port=22:porto=tcp:toaddr=192.168.1.112转发端口数据至另一个IP的 3753 端口:
firewall-cmd --zone=external --add-forward-port=22:porto=tcp::toport=3753:toaddr=192.168.1.112systemctl是CentOS7的服务管理工具中主要的工具,它融合之前service和chkconfig的功能于一体。
启动一个服务:systemctl start firewalld.service 关闭一个服务:systemctl stop firewalld.service 重启一个服务:systemctl restart firewalld.service 显示一个服务的状态:systemctl status firewalld.service 在开机时启用一个服务:systemctl enable firewalld.service 在开机时禁用一个服务:systemctl disable firewalld.service 查看服务是否开机启动:systemctl is-enabled firewalld.service 查看已启动的服务列表:systemctl list-unit-files|grep enabled 查看启动失败的服务列表:systemctl --failed(ubuntu) 使用 iptables-persistent 1)安装iptables-persistent sudo apt-get install iptables-persistent 2)持久化规则 sudo netfilter-persistent save sudo netfilter-persistent reload