云服务器的防火墙如何开启

云服务器的防火墙如何开启

随着互联网的快速发展,越来越多的企业和个人开始使用云服务器来部署和运行自己的应用,云服务器具有弹性扩展、安全可靠、成本低廉等优点,但同时也存在一定的安全隐患,为了保证云服务器的安全性,我们需要对云服务器进行防火墙设置,本文将详细介绍如何开启云服务器的防火墙。

云服务器的防火墙如何开启

查看防火墙状态

在开启云服务器防火墙之前,我们需要先查看防火墙的状态,不同的云服务商提供的API可能不同,以下是一些常见云服务商的API示例:

1、阿里云:

curl https://api.aliyun.com/v1/instances/{instanceId}/security-groups | json_pp

2、腾讯云:

curl https://cvm.qq.com/cgi-bin/cloudconsole/getinstancedetail?domain=xxxx&project=xxxxx&region=xxxxx&instanceid=xxxxx

3、百度云:

curl http://bcs.baidubce.com/api/v1/instance?instance_id={instanceId} | jq '.Instance'

4、华为云:

云服务器的防火墙如何开启

curl https://ecs.cn-north-4.myhuaweicloud.com/v1/{project_id}/servers/{server_id} | jq '.Server.SecurityGroups'

添加防火墙规则

在确认防火墙已经开启的情况下,我们需要添加防火墙规则,以下是一个简单的示例,用于允许SSH端口(22)的通信:

1、登录到云服务器,使用以下命令安装iptables(以Ubuntu为例):

sudo apt-get update
sudo apt-get install iptables

2、创建一个新的iptables链,并允许SSH端口的通信:

sudo iptables -N SSH_PORT_BLOCKING
sudo iptables -A INPUT -p tcp --dport 22 -j SSH_PORT_BLOCKING

3、允许已建立的连接和相关的数据包通过防火墙:

sudo iptables -A SSH_PORT_BLOCKING -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A SSH_PORT_BLOCKING -j RETURN

保存防火墙规则

为了让防火墙规则在服务器重启后依然生效,我们需要将这些规则保存到配置文件中,以下是一个简单的示例,将防火墙规则保存到/etc/sysconfig/iptables文件中:

云服务器的防火墙如何开启

1、备份当前的iptables规则:

sudo cp /etc/sysconfig/iptables /etc/sysconfig/iptables.bak

2、将新的防火墙规则追加到/etc/sysconfig/iptables文件中:

sudo sh -c 'cat << EOT >> /etc/sysconfig/iptables && cat /etc/sysconfig/iptables > /etc/sysconfig/iptables.bak' << EOT
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [0:0]
#Allow SSH port (TCP port 22) traffic to pass through firewall. This is necessary because the default policy of Ubuntu does not allow incoming connections on port 22. This rule will be added only when a new instance is created or when an existing instance is rebooted for the first time after being created. The following rules are necessary to ensure that established connections and related data packets can also pass through the firewall. These rules will be removed when this script is executed again in the future. See the comments at the bottom of this file for more details about these rules. This script was generated by the "Enable SSH Access" script provided with the Cloud Server Manager (CSM). Please DO NOT edit this file manually!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"$IPTABLES_FILTER"    -I FORWARD    -p tcp --dport 22    -j REJECT --reject-with icmp-host-prohibited       # Allow incoming SSH connection requests on port 22 (TCP port) to pass through the firewall. This is necessary because the default policy of Ubuntu does not allow incoming connections on port 22. This rule will be added only when a new instance is created or when an existing instance is rebooted for the first time after being created。 The following rules are necessary to ensure that established connections and related data packets can also pass through the firewall. These rules will be removed when this script is executed again in the future. See the comments at the bottom of this file for more details about these rules. This script was generated by the "Enable SSH Access" script provided with the Cloud Server Manager (CSM). Please DO NOT edit this file manually!!!!!!!!!!!!!!!!!!!!!!!!"$IPTABLES_FILTER"    -I FORWARD    -m state --state ESTABLISHED,RELATED    -j ACCEPT                # Allow established and related TCP connections to pass through the firewall. This is necessary because the default policy of Ubuntu does not allow incoming connections on port 22 if there are already established connections on that port. This rule will be added only when a new instance is created or when an existing instance is rebooted for thefirst time after being created. The following rules are necessary to ensure that established connections and related data packets can also pass through the firewall. These rules will be removed when this script is executed again in the future. See the comments at the bottom of this file for more details about these rules. This script was generated by the "Enable SSH Access" script provided with the Cloud Server Manager (CSM). Please DO NOT edit this file manually!"$IPTABLES_FILTER"    COMMIT                 # End of filter rules                   EOT' && cat /etc/sysconfig/iptables > /etc/sysconfig/iptables.bak && systemctl restart netfilter-persistent && systemctl enable netfilter-persistent && service netfilter-persistent restart || true' 

原创文章,作者:酷盾叔,如若转载,请注明出处:https://www.kdun.com/ask/93845.html

本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。

(0)
酷盾叔订阅
上一篇 2023-12-14 17:30
下一篇 2023-12-14 17:32

相关推荐

  • 如何实现服务器的密钥登录?

    服务器密钥登录是使用公钥加密技术进行身份验证的安全登录方式。

    2024-11-24
    07
  • 福建300g高防虚拟主机如何抵御攻击?

    福建300g高防虚拟主机的攻击方式福建300g高防虚拟主机是一种具有较强抗攻击能力的服务器,但黑客们仍然能够找到漏洞进行攻击,以下是几种常见的攻击方式及其防御方法:一、DDoS攻击1、定义:DDoS(分布式拒绝服务)攻击通过向目标服务器发送大量请求来消耗其资源,使其无法响应正常请求,2、防御措施:使用高防IP……

    2024-11-24
    06
  • 福建100g高防服务器如何有效抵御攻击?

    福建100G高防服务器攻击指南背景与目标在当今信息化时代,网络安全成为各大企业关注的重点,DDoS攻击由于其破坏力大、实施相对简单,被广泛使用,本文将详细介绍如何对福建地区的100G高防服务器进行有效攻击,旨在帮助安全研究人员了解攻击手段,从而更好地进行防御准备,攻击类型概述DDoS攻击定义:DDoS(分布式拒……

    2024-11-23
    06
  • 魔力象限防火墙的产品定位是什么?

    魔力象限防火墙定位为网络边界的守护者,通过双向控制保护网络安全,适应本地、混合云及公共云环境。

    2024-11-22
    026

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

产品购买 QQ咨询 微信咨询 SEO优化
分享本页
返回顶部
云产品限时秒杀。精选云产品高防服务器,20M大带宽限量抢购 >>点击进入