在CentOS 7里边,除了iptables
,多了一个firealld。之前并不怎么关注防火墙,甚至直接把防火墙从开机启动里移除了,真是个愚蠢的行为啊,后来每次ssh连上VPS之后,发现ssh被恶意测试了好多次,于是我不仅修改了ssh的默认端口,就开启了防火墙。
因为我对防火墙的操作较少,我也并不关注过多的操作,我只存了两个命令
增加端口
firewall-cmd --permanent --zone=public --add-port=端口号/tcp
firewall-cmd --permanent --zone=public --add-port=端口号/udp
其实UDP的这个,我很少用
移除端口
firewall-cmd --permanent --zone=public --remove-port=端口号/tcp
firewall-cmd --permanent --zone=public --remove-port=端口号/udp
后来每次重装过VPS之后,重新配置防火墙的时候,发现开的端口过多,写了个脚本
一个很简单的脚本,分享一下把
#!/bin/bash
#This file just add or remove port of firewalld
#first edition 2017-3-20
#add loop when add ports and remove ports 2018-1-13
#author likilli
echo -e "请输入操作代码."
echo -e "1.添加防火墙端口 2.移除端口 3.列出已打开端口"
read -p "Plsease input: " choice
if [ "$choice" == "1" ]; then
echo -e "Input "quit" to terminate loop"
while [ "$port_add" != "quit" ]
do
read -p "Please input the Port number you want to add: " port_add
if [ "$port_add" == "quit" ]; then
service firewalld restart
exit 0;
else
firewall-cmd --permanent --zone=public --add-port=$port_add/tcp
fi
done
fi
if [ "$choice" == "2" ]; then
echo -e "Input "quit" to finish loop too"
while [ "$port_remove" != "quit" ]
do
read -p "Please input the Port number you want to remove: " port-_remove
if [ "$port_remove" == "quit" ]; then
service firewalld restart
exit 0;
else
firewall-cmd --permanent --zone=public --remove-port=$port_remove/tcp
fi
done
fi
if [ "$choice" == "3" ]; then
firewall-cmd --list-ports
exit 0
fi