防火墙
CentOS6中防火墙名称:iptables
CentOS7及以上中防火墙名称:firewalld
iptables
1、iptables服务启动/重启/关闭
#service iptables start/restart/stop
#/etc/init.d/iptables start/restart/stop
2、查看iptables状态(规则)
#service iptables status
3、查看iptables规则
#iptables -L [-n]
-L:列出规则
-n:将单词表达形式改成数字
4、常用防火墙规则
iptables -A/I INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT #允许本地回环接口(即运行本机访问本机)
iptables -A/I INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT #允许已建立的或相关连的通行
iptables -A/I OUTPUT -j ACCEPT #允许所有本机向外的访问
iptables -A/I INPUT -p tcp --dport 22 -j ACCEPT #允许访问22端口
iptables -A/I INPUT -p tcp --dport 80 -j ACCEPT #允许访问80端口
iptables -A/I INPUT -p tcp --dport 21 -j ACCEPT #允许ftp服务的21端口
iptables -A/I INPUT -p tcp --dport 20 -j ACCEPT #允许FTP服务的20端口
iptables -A/I INPUT -j reject #禁止其他未允许的规则访问
iptables -A/I FORWARD -j REJECT #禁止其他未允许的规则访问
-A:add,添加规则到最后面
-I:insert,插入规则到最前面
INPUT:进站请求【出站请求:OUTPUT】
-p:protocol,指定协议(icmp/tcp/udp)
–dport:指定端口号
-j:指定行为结果【允许:ACCEPT,拒绝:REJECT】
执行添加规则的命令后,需要保存和重启防火墙服务:
#/etc/init.d/iptables save
#/etc/init.d/iptables restart
firewalld
1、查看防火墙状态
systemctl status firewalld
firewall-cmd --state
2、开启/关闭防火墙
systemctl start firewalld #开启
systemctl stop firewalld #关闭
3、设置防火墙开机自启
systemctl enable firewalld
4、取消防火墙开机自启
systemctl disable firewalld
5、配置/移除防火墙规则(配置/移除后需更新防火墙规则后生效)
#配置
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=443/tcp --permanent
#移除
firewall-cmd --remove-port=80/tcp --permanent
firewall-cmd --remove-port=443/tcp --permanent
6、更新防火墙规则
firewall -cmd --reload
7、查看防火墙规则:
firewall -cmd --list-all