Linux虚拟机如何设置防火墙

linux虚拟机中设置防火墙可以通过多种工具实现,其中iptables和firewalld是最常用的两种方法。以下是具体的设置步骤:

使用iptables设置防火墙

iptables是linux系统自带的强大防火墙工具,它基于Netfilter框架工作。以下是一些基本的iptables命令示例:

  • 查看当前防火墙规则

    iptables -L -n 
  • 添加规则:例如,禁止特定IP访问8080端口。

    iptables -A input -s 192.168.227.133 -p tcp --dport 8080 -j DROP 
  • 允许特定IP访问8080端口

    iptables -D input -s 192.168.227.133 -p tcp --dport 8080 -j DROP 
  • 保存iptables规则

    service iptables save 
  • 重启防火墙

    service iptables restart 

使用firewalld设置防火墙

firewalld是centos 7及以后版本中推荐的防火墙管理工具,它提供了更简单的管理界面和动态加载规则的能力。以下是使用firewalld的基本步骤:

  • 查看防火墙状态

    systemctl status firewalld 
  • 开启防火墙

    systemctl start firewalld 
  • 关闭防火墙

    systemctl stop firewalld 
  • 永久启用防火墙

    systemctl enable firewalld 
  • 查看所有区域

    firewall-cmd --get-zones 
  • 添加端口到特定区域:例如,永久开放443端口到public区域。

    firewall-cmd --zone=public --add-port=443/tcp --permanent 
  • 重新加载防火墙规则

    firewall-cmd --reload 

请注意,上述命令和步骤可能需要根据您的Linux发行版和具体需求进行调整。在执行任何防火墙配置之前,请确保您了解这些命令的作用,以免意外阻止自己的网络访问。

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享