linux中配置防火墙的命令
-
在Linux中配置防火墙的命令有多种,下面列举一些常用的命令:
1. iptables命令:iptables是Linux操作系统中最常用的防火墙软件,它可以配置各种网络规则,过滤和转发数据包。
例如,使用以下命令可以查看当前的iptables规则:
“`
iptables -L
“`
使用以下命令可以添加一条规则来允许特定端口的进出流量:
“`
iptables -A INPUT -p tcp –dport-j ACCEPT
iptables -A OUTPUT -p tcp –sport-j ACCEPT
“`
这将允许TCP协议的特定端口的进出流量。2. firewalld命令:firewalld是CentOS7及其衍生发行版中默认的防火墙管理工具。
例如,使用以下命令可以启动firewalld服务:
“`
systemctl start firewalld
“`
使用以下命令可以查看当前的防火墙规则:
“`
firewall-cmd –list-all
“`
使用以下命令可以添加一条规则来允许特定端口的进出流量:
“`
firewall-cmd –zone=public –add-port=/tcp –permanent
“`
这将允许TCP协议的特定端口的进出流量,并且将该规则设置为永久生效。3. ufw命令:ufw是基于iptables的简化配置工具,适用于Ubuntu及其衍生发行版。
例如,使用以下命令可以启用ufw服务:
“`
ufw enable
“`
使用以下命令可以查看当前的防火墙规则:
“`
ufw status verbose
“`
使用以下命令可以添加一条规则来允许特定端口的进出流量:
“`
ufw allow/tcp
“`
这将允许TCP协议的特定端口的进出流量。以上是在Linux中配置防火墙的常用命令,根据具体的操作系统和防火墙软件的不同,使用的命令可能略有差异。请根据实际情况选择适合的命令进行配置。
2年前 -
在Linux中,配置防火墙有多种方法。下面是一些常用的命令和工具。
1. iptables命令:
iptables是Linux中专用的防火墙配置工具。可以使用它来设置防火墙规则。以下是一些常用的iptables命令示例:– 开启防火墙:
“`
sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT
“`– 关闭防火墙:
“`
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT DROP
“`– 允许特定端口的传入连接:
“`
sudo iptables -A INPUT -p tcp –dport-j ACCEPT
“`– 允许特定IP地址的传入连接:
“`
sudo iptables -A INPUT -s-j ACCEPT
“`– 拒绝特定IP地址的传入连接:
“`
sudo iptables -A INPUT -s-j DROP
“`– 显示当前的防火墙规则:
“`
sudo iptables -L
“`2. ufw命令:
ufw是一种简化了iptables命令的防火墙配置工具。以下是一些常用的ufw命令示例:– 开启防火墙:
“`
sudo ufw enable
“`– 关闭防火墙:
“`
sudo ufw disable
“`– 允许特定端口的传入连接:
“`
sudo ufw allow“` – 允许特定IP地址的传入连接:
“`
sudo ufw allow fromto any
“`– 拒绝特定IP地址的传入连接:
“`
sudo ufw deny fromto any
“`– 显示当前的防火墙规则:
“`
sudo ufw status
“`3. firewalld命令:
firewalld是CentOS和RHEL等发行版中使用的防火墙配置工具。以下是一些常用的firewalld命令示例:– 开启防火墙:
“`
sudo systemctl start firewalld
“`– 关闭防火墙:
“`
sudo systemctl stop firewalld
“`– 设置默认防火墙区域:
“`
sudo firewall-cmd –set-default-zone=
“`– 允许特定端口的传入连接:
“`
sudo firewall-cmd –add-port=/tcp –permanent
“`– 允许特定IP地址的传入连接:
“`
sudo firewall-cmd –add-source=/32 –permanent
“`– 拒绝特定IP地址的传入连接:
“`
sudo firewall-cmd –add-rich-rule=’rule family=”ipv4″ source address=”/32″ reject’ –permanent
“`– 重新加载防火墙规则:
“`
sudo firewall-cmd –reload
“`4. gufw命令:
gufw是一种图形化的ufw命令前端工具。可以使用它简化防火墙配置的过程。安装并打开gufw后,可以通过可视化界面设置防火墙规则。5. iptables-save和iptables-restore命令:
iptables-save命令用于保存当前的iptables规则到文件中,而iptables-restore命令可以从文件中恢复之前保存的规则。这对于备份和恢复防火墙规则非常有用。这些命令和工具提供了多种方式来配置Linux中的防火墙。可以根据具体的需求选择最合适的方法进行配置。
2年前 -
在Linux系统中,配置防火墙的命令可以通过iptables和firewalld来实现。下面分别介绍这两种方式的配置步骤。
使用iptables配置防火墙:
1. 查看当前防火墙状态:
“`shell
iptables -L
“`
2. 清空当前防火墙规则:
“`shell
iptables -F
“`
3. 设置默认策略,允许所有出站流量,拒绝所有入站流量:
“`shell
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
“`
4. 允许特定端口通过防火墙:
“`shell
iptables -A INPUT -p tcp –dport 22 -j ACCEPT # 允许SSH访问
iptables -A INPUT -p tcp –dport 80 -j ACCEPT # 允许HTTP访问
iptables -A INPUT -p tcp –dport 443 -j ACCEPT # 允许HTTPS访问
“`
5. 允许特定IP地址通过防火墙:
“`shell
iptables -A INPUT -s 192.168.1.100 -j ACCEPT # 允许特定IP访问
“`
6. 禁止特定IP地址通过防火墙:
“`shell
iptables -A INPUT -s 192.168.1.200 -j DROP # 禁止特定IP访问
“`
7. 保存设置并重启防火墙服务:
“`shell
service iptables save
service iptables restart
“`使用firewalld配置防火墙:
1. 查看当前防火墙状态:
“`shell
firewall-cmd –state
“`
2. 启用firewalld服务:
“`shell
systemctl enable firewalld
systemctl start firewalld
“`
3. 查看已加载的防火墙规则:
“`shell
firewall-cmd –list-all
“`
4. 允许特定端口通过防火墙:
“`shell
firewall-cmd –add-port=22/tcp –permanent # 允许SSH访问
firewall-cmd –add-port=80/tcp –permanent # 允许HTTP访问
firewall-cmd –add-port=443/tcp –permanent # 允许HTTPS访问
“`
5. 允许特定IP地址通过防火墙:
“`shell
firewall-cmd –add-source=192.168.1.100 –permanent # 允许特定IP访问
“`
6. 禁止特定IP地址通过防火墙:
“`shell
firewall-cmd –add-rich-rule=’rule family=”ipv4″ source address=”192.168.1.200″ reject’ –permanent # 禁止特定IP访问
“`
7. 重载防火墙规则:
“`shell
firewall-cmd –reload
“`
8. 关闭firewalld服务:
“`shell
systemctl stop firewalld
systemctl disable firewalld
“`使用iptables和firewalld都可以进行防火墙配置,具体选择哪一种方式取决于个人需求和操作习惯。
2年前