linux防火墙常用命令
-
Linux防火墙是保护系统安全的重要组成部分,它可以过滤网络流量,限制访问,防止攻击和入侵。在Linux系统中,常用的防火墙是iptables和firewalld。下面是一些常用的Linux防火墙命令:
1. iptables命令:
– `iptables -L`: 列出当前防火墙规则。
– `iptables -F`: 清除所有防火墙规则。
– `iptables -A INPUT -p tcp –dport 端口号 -j ACCEPT`: 允许指定端口的TCP流量通过防火墙。
– `iptables -A OUTPUT -p tcp –dport 端口号 -j ACCEPT`: 允许指定端口的TCP流量从系统中流出。
– `iptables -A INPUT -s 源IP地址 -j DROP`: 阻止来自特定IP地址的流量进入系统。
– `iptables -A FORWARD -i eth0 -o eth1 -j ACCEPT`: 允许从eth0接口到eth1接口的数据包转发。
– `iptables -A OUTPUT -p icmp –icmp-type echo-request -j DROP`: 阻止系统发送ICMP echo请求(ping)。2. firewalld命令:
– `firewall-cmd –state`: 检查防火墙状态。
– `firewall-cmd –list-all`: 列出当前防火墙规则。
– `firewall-cmd –add-service=服务名 –permanent`: 允许指定服务通过防火墙。
– `firewall-cmd –remove-service=服务名 –permanent`: 移除指定服务的防火墙规则。
– `firewall-cmd –zone=public –add-port=端口号/tcp –permanent`: 允许指定端口的TCP流量通过防火墙。
– `firewall-cmd –reload`: 重新加载防火墙规则。以上只是一些常用的Linux防火墙命令,更多的命令和选项可以通过man命令查看其详细用法。防火墙的配置需要根据具体情况进行调整,确保系统安全并限制不必要的访问。
2年前 -
1. iptables:iptables是Linux系统中最常用的防火墙管理工具。它可以管理防火墙规则,通过过滤和转发网络数据包来实现网络安全。使用iptables可以配置防火墙规则,允许或阻止特定的网络连接。
2. ufw:ufw是一种简化配置iptables的工具,它的全称是Uncomplicated Firewall。ufw允许用户更容易地配置iptables规则,通过提供简单的命令行接口来管理防火墙。使用ufw可以轻松地添加、删除和管理防火墙规则。
3. firewalld:firewalld是一种动态防火墙管理工具,它提供了一种管理Linux防火墙规则的方法,可以根据应用程序或服务动态地调整防火墙规则。firewalld使用zones来管理防火墙规则,每个zone定义了一组防火墙规则。
4. nftables:nftables是Linux内核中的新一代防火墙管理工具,用于替代iptables。nftables提供了更灵活、更高效的防火墙规则配置和管理方式。和iptables类似,nftables可以通过添加和删除规则来管理防火墙。
5. systemctl:systemctl是Linux系统中用于管理系统服务的工具,也可以用于管理防火墙服务。通过systemctl命令,可以启动、停止、重启和查看防火墙服务的状态。可以使用systemctl来控制防火墙服务的运行,以确保防火墙处于活动状态。
总结起来,Linux系统中常用的防火墙命令包括iptables、ufw、firewalld、nftables和systemctl。使用这些命令可以配置、管理和控制防火墙规则,保护系统免受网络攻击。
2年前 -
Linux系统中,防火墙是保护网络安全的重要组成部分。常见的Linux防火墙软件有iptables、firewalld等。下面将介绍Linux防火墙的常用命令,包括iptables和firewalld的操作命令。
##### 一、iptables命令
1. 查看当前防火墙规则“`shell
iptables -L
“`2. 清空或重置防火墙规则
“`shell
iptables -F # 清空所有防火墙规则
iptables -X # 删除用户自定义的链
iptables -Z # 将计数器归零
iptables -t nat -F # 清空 NAT 表中的规则
iptables -t mangle -F # 清空 MANGLE 表中的规则
“`3. 设置默认策略
“`shell
iptables -P INPUT ACCEPT # 设置默认接受所有输入数据包
iptables -P INPUT DROP # 设置默认拒绝所有输入数据包
iptables -P FORWARD ACCEPT # 设置默认接受所有转发数据包
iptables -P FORWARD DROP # 设置默认拒绝所有转发数据包
iptables -P OUTPUT ACCEPT # 设置默认接受所有输出数据包
iptables -P OUTPUT DROP # 设置默认拒绝所有输出数据包
“`4. 添加规则
“`shell
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT # 允许来自192.168.1.0/24子网的数据包通过
iptables -A OUTPUT -d 192.168.1.0/24 -j ACCEPT # 允许发送到192.168.1.0/24子网的数据包
iptables -A FORWARD -i eth0 -d 192.168.1.0/24 -j DROP # 阻止eth0网卡接口发送到192.168.1.0/24子网的数据包
iptables -A INPUT -p tcp –dport 22 -j ACCEPT # 允许TCP协议的22端口通过
iptables -A INPUT -p udp –sport 53 -j ACCEPT # 允许UDP协议的53端口通过
“`5. 删除规则
“`shell
iptables -D INPUT -s 192.168.1.0/24 -j ACCEPT # 删除允许来自192.168.1.0/24子网的数据包通过的规则
iptables -D OUTPUT -d 192.168.1.0/24 -j ACCEPT # 删除允许发送到192.168.1.0/24子网的数据包的规则
iptables -D FORWARD -i eth0 -d 192.168.1.0/24 -j DROP # 删除阻止eth0网卡接口发送到192.168.1.0/24子网的数据包的规则
“`6. 保存和加载规则
“`shell
iptables-save > /etc/sysconfig/iptables # 保存当前防火墙规则到文件
iptables-restore < /etc/sysconfig/iptables # 加载防火墙规则文件```##### 二、firewalld命令firewalld是RHEL/CentOS 7中的一个动态防火墙管理工具,使用firewalld可以方便地管理防火墙规则。1. 启动、停止和重启防火墙```shellsystemctl start firewalld # 启动防火墙systemctl stop firewalld # 停止防火墙systemctl restart firewalld # 重启防火墙systemctl enable firewalld # 设置防火墙开机自启动systemctl disable firewalld # 取消防火墙开机自启动```2. 查看防火墙状态和配置```shellfirewall-cmd --state # 查看防火墙状态firewall-cmd --get-active-zones # 查看活动区域firewall-cmd --list-all # 查看防火墙规则```3. 添加和删除规则```shellfirewall-cmd --permanent --add-source=192.168.1.0/24 # 添加允许来自192.168.1.0/24子网的数据包通过的规则firewall-cmd --permanent --remove-source=192.168.1.0/24 # 删除允许来自192.168.1.0/24子网的数据包通过的规则firewall-cmd --permanent --add-port=22/tcp # 添加允许TCP协议的22端口通过的规则firewall-cmd --permanent --remove-port=22/tcp # 删除允许TCP协议的22端口通过的规则firewall-cmd --reload # 重新加载防火墙规则```4. 启用和禁用服务```shellfirewall-cmd --permanent --add-service=http # 启用HTTP服务firewall-cmd --permanent --remove-service=http # 禁用HTTP服务```5. 查看和添加区域```shellfirewall-cmd --get-default-zone # 查看默认区域firewall-cmd --get-zones # 查看所有区域firewall-cmd --zone=public --add-port=80/tcp # 在public区域添加允许TCP协议的80端口通过的规则```6. 查看和添加接口```shellfirewall-cmd --get-active-zones # 查看活动区域及接口firewall-cmd --zone=public --add-interface=eth0 # 在public区域添加eth0接口```7. 查看和添加协议```shellfirewall-cmd --permanent --get-services # 查看所有协议firewall-cmd --permanent --add-protocol=icmp # 启用ICMP协议firewall-cmd --permanent --remove-protocol=icmp # 禁用ICMP协议```8. 查看和添加源地址```shellfirewall-cmd --zone=public --list-sources # 查看public区域的源地址firewall-cmd --zone=public --add-source=192.168.1.0/24 # 在public区域添加允许来自192.168.1.0/24子网的数据包通过的规则```9. 查看和添加目标地址```shellfirewall-cmd --zone=public --list-destinations # 查看public区域的目标地址firewall-cmd --zone=public --add-destination=192.168.1.0/24 # 在public区域添加允许发送到192.168.1.0/24子网的数据包的规则```10. 清除规则和重置防火墙```shellfirewall-cmd --mostly clean # 清除所有规则并重置防火墙```以上就是Linux防火墙常用命令的介绍,通过这些命令可以方便地管理防火墙规则,提高网络安全性。在设置防火墙时,应根据实际需求和网络环境合理配置规则,同时定期审查和更新防火墙规则,以保护系统和网络的安全。2年前