日期 |
版本 |
作者 |
说明 |
2021-12-25 |
V-1.0 |
孔留锋 |
整理防火墙常用命令。 |
2022-05-27 |
V-1.2 |
孔留锋 |
补充ufw命令。 |
iptables防火墙
简介
IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统。如果 Linux 系统连接到因特网或 LAN、服务器或连接 LAN 和因特网的代理服务器, 则该系统有利于在 Linux 系统上更好地控制 IP 信息包过滤和防火墙配置。
命令
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 33 34 35 36 37 38
| # 查看防火墙状态 service iptables status # 停止防火墙 service iptables stop # 启动防火墙 service iptables start # 重启防火墙 service iptables restart # 永久关闭防火墙 chkconfig iptables off # 永久关闭后重启 chkconfig iptables on
# 查看iptables规则: iptables -L # 清除iptables规则: iptables -F #允许某个IP地址通过防火墙: sudo iptables -A INPUT -s <IP address> -j ACCEPT #禁止某个IP地址通过防火墙: iptables -A INPUT -s <IP address> -j DROP #允许某个端口通过防火墙: iptables -A INPUT -p <protocol> --dport <port> -j ACCEPT # 禁止某个端口通过防火墙: iptables -A INPUT -p <protocol> --dport <port> -j DROP # 允许某个应用程序通过防火墙: iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# 其中,<IP address>表示IP地址,<protocol>表示协议类型(tcp或udp),<port>表示端口号。-A参数用于添加规则,-s参数用于指定源IP地址,-p参数用于指定协议类型,--dport参数用于指定目标端口,-j参数用于指定动作(ACCEPT或DROP)。
# 注意:iptables命令需要以root权限运行。建议在修改iptables规则前备份当前规则,以免误操作导致系统无法访问。
# 另外允许某个端口通过防火墙,可以更改配置文件,如下:开启80端口 # vim /etc/sysconfig/iptables # service iptables restart
|
firewall
简介
firewall是CentOS 7版本后的自带防火墙管理工具,与iptables不同,firewall是一款动态防火墙管理工具。所谓动态防火墙,是指firewall在运行时,任何规则的变更都不需要对防火墙规则列表进行重新加载,只需要将变更部分保存并更新运行即可。相对应的,当用户使用iptables添加规则时,如果想要让规则永久保存,需要执行命令serivce iptables save(注:该命令的执行需要安装iptables.serivces),才可以永久保存置配置文件中,并且在重启后,配置依旧存在。在整个过程中,iptables会对防火墙的规则列表重读一遍,加载到内核。
命令
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
| # 查看firewall服务状态 systemctl status firewalld # 出现Active: active (running)切高亮显示则表示是启动状态 # 出现Active: inactive (dead)灰色表示停止
#查看firewall的状态 firewall-cmd --state # 开启firewalld.service服务 service firewalld start # 重启、firewalld.service服务 service firewalld restart # 关闭firewalld.service服务 service firewalld stop # 查看防火墙规则 firewall-cmd --list-all
# 查询端口是否开放 firewall-cmd --query-port=8080/tcp # 开放8080端口,permanent:表示设置为持久 firewall-cmd --permanent --add-port=80/tcp # 移除端口 firewall-cmd --permanent --remove-port=8080/tcp #重启防火墙(修改配置后要重启防火墙) firewall-cmd --reload
|
ufw
简介
ufw(Uncomplicated Firewall)是Ubuntu系统中的一种简单防火墙配置工具。它允许管理员轻松地配置基本的防火墙规则,以保护系统免受来自互联网的攻击。
命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| #查看ufw状态: sudo ufw status #启用ufw: sudo ufw enable #禁用ufw: sudo ufw disable #允许某个端口通过防火墙: sudo ufw allow <port>/<protocol> #禁止某个端口通过防火墙: sudo ufw deny <port>/<protocol> #允许某个IP地址通过防火墙: sudo ufw allow from <IP address> #禁止某个IP地址通过防火墙: sudo ufw deny from <IP address> #删除某个端口规则: sudo ufw delete allow <port>/<protocol> #删除某个IP地址规则: sudo ufw delete allow from <IP address> #允许某个应用程序通过防火墙: sudo ufw allow <application name>
#其中,<port>表示端口号,<protocol>表示协议类型(tcp或udp),<IP address>表示IP地址。
|