Linux基础(九):防火墙iptables、firewalld

防火墙

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fthyc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值