Centos7跟centos6版本的系统有很大区别,一般来说centos6是用iptables来查看防火墙状态,但是到了centos7版本,这个命令就被firewalld命令取代了,当然,有些centos7版本仍然可以用iptables命令。下面我们来看看centos7常用的几个firewalld命令。
一、使用firewalld
1、查看防火墙状态
systemctl status firewalld
2、启动
systemctl start firewalld
3、停止
systemctl disable firewalld
4、禁用
systemctl stop firewalld
5、查看所有打开的端口
firewall-cmd--zone=public --list-ports
6、添加开放端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
(--permanent永久生效,没有此参数重启后失效)
7、重新载入(更新防火墙规则)
firewall-cmd --reload
8、查看端口是否开放
firewall-cmd --zone=public --query-port=80/tcp
9、删除
firewall-cmd --zone=public --remove-port=80/tcp --permanent
二、使用iptables
切换到iptables首先应该关掉默认的firewalld,然后安装iptables服务。
1、停止并禁用 firewalld
systemctl stop firewald.service
systemctl disable firewald.service
2、安装iptables-services、iptables-devel
yum install -y iptables-services iptables-devel
3、启用并启动iptables
systemctl enable iptables.service
systemctl start iptables.service
4、查看iptables配置文件
vim /etc/sysconfig/iptables
5、开启转发功能
(1) 在 /etc/sysctl.conf 中添加 net.ipv4.ip_forward=1
vim /etc/sysctl.conf
输入i 进入编辑,添加net.ipv4.ip_forward=1
(2) sudo sysctl -p 同步内核参数
(3) 继续查看内核参数 less /proc/sys/net/ipv4/ip_forward 如果为1 则配置生效