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

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

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

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

查看防火墙状态

在开启云服务器防火墙之前,我们需要先查看防火墙的状态,不同的云服务商提供的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

相关推荐

  • 如何使用SSH连接命令进行远程登录?

    SSH连接命令的基本格式是:ssh [选项] 用户名@主机名或IP地址,[选项]`是可选的参数,例如指定端口号、使用特定私钥等。

    2024-12-23
    06
  • 为什么Chrome浏览器无法打开网络连接?

    Chrome浏览器无法打开网页的原因及解决方法一、检查网络连接1、确保设备已连接到互联网: – 在Windows系统中,单击地址栏左侧的三个点图标,然后选择“网络”>“高级”>“DNS设置”,在此可以查看当前使用的DNS服务器,如果使用的是公共DNS服务器(例如Google DNS或OpenDNS……

    2024-12-22
    014
  • 为什么无法通过外网远程桌面连接到服务器?

    当遇到服务器外网远程桌面连接不上的问题时,可能涉及多个方面的因素,我们需要从网络、服务器设置、客户端配置等多个角度进行排查和解决,一、网络方面1、检查网络连接:确保服务器和客户端之间的网络连接正常,可以通过 ping 命令测试服务器的 IP 地址或域名是否可达,如果不可达,可能是网络线路故障、路由器配置问题或运……

    2024-12-21
    028
  • 为何服务器外网远程无法连接?

    服务器外网远程不上的问题可能由多种原因引起,包括但不限于网络配置错误、防火墙设置不当、DNS解析问题、服务器软件故障等,为了帮助您更好地理解和解决这一问题,本文将详细介绍可能导致服务器无法通过外网远程访问的原因及其相应的解决方案, 网络连接检查确保您的本地设备与互联网之间的物理连接是正常的,您可以尝试使用pin……

    2024-12-21
    017

发表回复

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

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