【Linux系统管理】常用防火墙配置命令

118次阅读

在 Linux 系统中,常见的防火墙工具有 iptables 和 firewalld;特别是在使用 CentOS 系统的服务器上,经常需要关闭 firewalld 或放行特定端口以确保网络连接正常,相比之下,Debian 系统虽然默认启用 iptables,但通常不会自动加载,这虽然简化了初次设置,但可能会忽视必要的安全措施。本文将详细介绍如何管理 Linux 系统中的防火墙,包括基本的开启、关闭和端口放行操作,以帮助用户加强系统安全。

【Linux 系统管理】常用防火墙配置命令

iptables 防火墙

1、iptables 防火墙通常默认加载在 Debian/Ubuntu 系统中,但也适用于其它大部分 Linux 系统;

service iptables status  #检查是否安装了 iptables
apt-get install -y iptables  #安装 iptables,centos 系统把 apt-get 命令改成 yum 即可
apt-get update iptables      #升级 iptables
apt-get install iptables-services -y   #安装 iptables-services
service iptables restart  #重启 iptables
service iptables start   #启动 iptables
service iptables stop    #停止 iptables
service iptables restart  #重启 iptables
chkconfig iptables on #开机自启
chkconfig iptables off #关闭开机自启 

2、UFW 是一个简化的、易于使用的 Linux 防火墙工具,旨在方便用户管理 iptables 防火墙规则;

ufw status   #查看 ufw 防火墙状态
apt-get install ufw   #安装 ufw
ufw enable   #启动 ufw
systemctl enable ufw  #开机自启
ufw disable   #停止 ufw
ufw allow <port>   #放行某个端口,将 <port> 替换为你要放行的具体端口号不含括号
ufw allow <port>/<protocol>   #放行某种协议,将 <port> 替换为端口号、将 <protocol> 替换为协议 / 如 tcp/udp
ufw allow <start-port>:<end-port>/<protocol>  ## 放行端口范围
ufw status numbered   #查看当前规则
ufw delete [rule_number]  #删除规则序号
ufw deny from 1.1.1.1   #阻止 1.1.1.1 地址的连接
ufw allow from [ip_address] to any port [port]/[protocol]  #允许特定 IP 通过特定端口协议 

firewalld 防火墙

firewalld 防火墙通常默认加载在 CentOS 系统中,但也适用于其它大部分 Linux 系统;

systemctl status firewalld.service  ## 查看 firewall 状态
yum -y install firewall*  ## 安装 firewall 命令
yum -y remove firewall*   ## 卸载 firewall 命令
systemctl start firewalld.service  ## 打开 firewall 命令
systemctl stop firewalld.service   ## 关闭 firewall 命令
systemctl restart firewalld.service  ## 重启 firewall
systemctl disable firewalld.service  ## 禁止 firewall 开机自启
systemctl enable firewalld.service  ## 允许 firewall 开机自启

setenforce 0    #临时关闭 SElinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config   #永久关闭 SElinux
正文完
 0
IT老司机
版权声明:本站原创文章,由 IT老司机 于2024-05-02发表,共计1603字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。