无论是 Windows 系统还是 Linux 系统都有防火墙,防火墙能够防护电脑免受外界攻击,保护电脑安全。本篇介绍如何在 Ubuntu/Debian/CentOS 上安装和使用防火墙。
Ubuntu/Debian 上防火墙
安装防火墙
1 2
| sudo apt update sudo apt install ufw
|
防火墙使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| sudo ufw enable
sudo ufw status
sudo ufw allow 22
sudo ufw allow 22/tcp
sudo ufw allow 7000:7070/tcp
sudo ufw allow from xxx.xxx.xxx.xxx
sudo ufw allow from xxx.xxx.xxx.xxx to any port 22
sudo ufw allow from 192.168.1.0/24 to any port 3306
sudo ufw allow in on eth0 to any port 3306
sudo ufw delete allow 8888
sudo ufw deny from 192.168.1.0/24
sudo ufw deny from 192.168.1.0/24 to any port 80
sudo ufw status numbered
sudo ufw delete 10
sudo ufw disable
sudo ufw reset
|
注意,开启防火墙后,注意打开指定的SSH连接端口,不然重启后将无法使用SSH连接
CentOS 上防火墙
安装防火墙
1
| sudo yum install firewalld
|
使用防火墙
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| sudo firewall-cmd --state
sudo systemctl start firewalld.service
sudo systemctl stop firewalld.service
sudo systemctl enable firewalld.service
sudo systemctl disable firewalld.service
sudo systemctl list-unit-file | grep firewalld sudo systemctl is-enabled firewalld
sudo firewall-cmd --zone=public --add-port=22/tcp --permanent
sudo firewall-cmd --zone=public --remove-port=80/tcp --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --query-port=22/tcp
|
参考连接
- 如何在 Debian 10 上使用 UFW 来设置防火墙
- [745]Debian打开关闭防火墙
- CentOS7查看和关闭防火墙
- How To Set Up a Firewall Using FirewallD on CentOS 7