- 区分Linux、Ubuntu、Centos的防火墙:
Linux原始自带的防火墙工具iptables
Ubuntu的防火墙工具ufw
Centos的防火墙工具firewalld
-----------------------------------------------------------------------------------------------------
Ubuntu默认使用的是ufw作为防火墙
- Linux原始的防火墙工具iptables由于过于繁琐,所以ubuntu系统默认提供了一个基于iptable之上的防火墙工具ufw。而UFW支持图形界面操作,只需在命令行运行ufw命令即能看到一系列的操作。
-
安装防火墙:sudo apt-get install ufw (Ubuntu默认有)
ufw 即uncomplicated firewall的简称,简单防火墙。
- 防火墙目录在/etc/ufw目录:
查看当前防火墙状态:sudo ufw status (inactive状态是防火墙关闭状态 active是开启状态)
开启防火墙:sudo ufw enable
关闭防火墙:sudo wfw disable
Ubuntu中其他常用的防火墙命令
ufw default allow/deny 默认允许/拒绝
ufw allow/deny 20:允许/拒绝 访问20端口,20后可跟/tcp或/udp,表示tcp或udp封包。
ufw allow/deny servicename:ufw从/etc/services中找到对应service的端口,进行过滤。
ufw allow proto tcp from 10.0.1.0/10 to 本机ip port 25:允许自10.0.1.0/10的tcp封包访问本机的25端口。
ufw delete allow/deny 20:删除以前定义的"允许/拒绝访问20端口"的规则
打开或关闭某个端口,例如:
sudo ufw allow smtp 允许所有的外部IP访问本机的25/tcp (smtp)端口
sudo ufw allow 22/tcp 允许所有的外部IP访问本机的22/tcp (ssh)端口
sudo ufw allow 53 允许外部访问53端口(tcp/udp)
sudo ufw allow from 192.168.1.100 允许此IP访问所有的本机端口
sudo ufw allow proto udp 192.168.0.1 port 53 to 192.168.0.2 port 53
sudo ufw deny smtp 禁止外部访问smtp服务
-------------------------------------------------------------------------------------------------------
Centos默认使用的是firewall作为防火墙
firewalld的基本使用
启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld
开机禁用 : systemctl disable firewalld
开机启用 : systemctl enable firewalld
永久性生效,重启后不会复原
开启: chkconfig iptables on
关闭: chkconfig iptables off
即时生效,重启后复原
开启: service iptables start
关闭: service iptables stop
正确关闭步骤:
service iptables stop
chkconfig iptables off