Updated on 2023-02-25 GMT+08:00

Security Group Overview

Security Group

A security group is a collection of access control rules for cloud resources, such as cloud servers, containers, and databases, that have the same security protection requirements and that are mutually trusted. After a security group is created, you can create various access rules for the security group, these rules will apply to all cloud resources added to this security group.

The system creates a default security group for each account. By default, the default security group rules:
  • Allow all outbound packets: Instances in the default security group can send requests to and receive responses from instances in other security groups.
  • Deny all inbound packets: Requests from instances in other security groups will be denied by the default security group.

Instances in the same security group can communicate with each other without adding additional rules.

If the default security group does not meet your requirements, you can modify security group rules or create a custom security group.

Both the default and custom security groups are free of charge.

Security Group Basics

  • You can associate instances, such as servers and extension NICs, with one or more security groups.

    You can change the security groups that are associated with instances, such as servers or extension NICs. By default, when you create an instance, it is associated with the default security group of its VPC unless you specify another security group.

  • You need to add security group rules to allow instances in the same security group to communicate with each other.
  • Security groups are stateful. If you send a request from your instance and the outbound traffic is allowed, the response traffic for that request is allowed to flow in regardless of inbound security group rules. Similarly, if inbound traffic is allowed, responses to allowed inbound traffic are allowed to flow out, regardless of outbound rules.

    Security groups use connection tracking to track traffic to and from instances that they contain and security group rules are applied based on the connection status of the traffic to determine whether to allow or deny traffic. If you add, modify, or delete a security group rule, or create or delete an instance in the security group, the connection tracking of all instances in the security group will be automatically cleared. In this case, the inbound or outbound traffic of the instance will be considered as new connections, which need to match the inbound or outbound security group rules to ensure that the rules take effect immediately and the security of incoming traffic.

    In addition, if the inbound or outbound traffic of an instance has no packets for a long time, the traffic will be considered as new connections after the connection tracking times out, and the connections need to match the outbound and inbound rules. The timeout period of connection tracking varies according to the protocol. The timeout period of a TCP connection in the established state is 600s, and the timeout period of an ICMP connection is 30s. For other protocols, if packets are received in both directions, the connection tracking timeout period is 180s. If one or more packets are received in one direction but no packet is received in the other direction, the connection tracking timeout period is 30s. For protocols other than TCP, UDP, and ICMP, only the IP address and protocol number are tracked.

If two ECSs are in the same security group but in different VPCs, the ECSs cannot communicate with each other. To enable communications between the ECSs, use a VPC peering connection to connect the two VPCs. For details about VPC connectivity, see Application Scenarios.

Security Group Rules

After you create a security group, you can add rules to the security group. A rule applies either to inbound traffic or outbound traffic. After you add cloud resources to the security group, they are protected by the rules of the group.

A security group rule consists of:

  • Source (inbound rule) or Destination (outbound rule): The value can be an IP address (such as 192.168.10.10/32), IP address range (such as 192.168.52.0/24), or a security group (such as sg-abc).
  • Protocol & Port: The value of ports can be individual ports (such as 22), consecutive ports (such as 22-30), ports and port ranges (such as 20,23-30), all ports (1-65535). The protocol can be TCP, UDP, HTTP, and others.
  • Source: The value can be a single IP address, an IP address group, or a security group.
  • Type: The value can be IPv4 or IPv6.
  • Description: Supplementary information about the security group rule.

Each security group has its default rules. For details, see Table 1. You can also customize security group rules. For details, see Adding a Security Group Rule.

Security Group Template

You can select one of the following security group templates provided by the system to quickly create a security group with default rules.

  • General-purpose web server: The security group that you create using this template is for general-purpose web servers and includes default rules that allow all inbound ICMP traffic and allow inbound traffic on ports 22, 80, 443, and 3389.
  • All ports open: The security group that you create using this template includes default rules that allow inbound traffic on any port. Note that allowing inbound traffic on any port poses security risks.
  • Custom: The security group that you create using this template includes default rules that deny inbound traffic on any port. You can add or modify security group rules as required.

Security Group Configuration Process

Figure 1 Process for configuring a security group

Security Group Constraints

  • By default, you can create a maximum of 100 security groups in your cloud account.
  • By default, you can add up to 50 security group rules to a security group.
  • By default, you can associate no more than five security groups with each ECS or extension NIC.
  • If a cloud server or an extension NIC is associated with multiple security groups, security group rules will be applied based on the following sequence: the first security group associated will take precedence over those associated later, then the rule with the highest priority in that security group will be applied first.
  • You can add a maximum of 20 instances to a security group at a time.
  • A security group can have no more than 6,000 instances associated, or performance will deteriorate.
  • Security group rules with certain configurations do not take effect for ECSs of certain specifications. Table 1 shows the details.
    Table 1 Scenarios that security group rules do not take effect

    Rule Configuration

    ECS Type

    Source or Destination is set to IP address group.

    The following x86 ECS types are not supported:
    • General computing (S1, C1, and C2 ECSs)
    • Memory-optimized (M1 ECSs)
    • High-performance computing (H1 ECSs)
    • Disk-intensive (D1 ECSs)
    • GPU-accelerated (G1 and G2 ECSs)
    • Large-memory (E1, E2, and ET2 ECSs)

    Port is set to non-consecutive ports.

    The following x86 ECS types are not supported:

    • General computing (S1, C1, and C2 ECSs)
    • Memory-optimized (M1 ECSs)
    • High-performance computing (H1 ECSs)
    • Disk-intensive (D1 ECSs)
    • GPU-accelerated (G1 and G2 ECSs)
    • Large-memory (E1, E2, and ET2 ECSs)

    All Kunpeng ECSs are not supported.

Suggestions

When using a security group:

  • Do not add all instances to the same security group if they have different isolation requirements.
  • It is not necessary that you create a security group for each instance. Instead, you can add instances with the same security requirements to the same security group.
When adding a security group rule:
  • Define simple security group rules. For example, if you add an instance to multiple security groups, the instance may comply with hundreds of security group rules, and a change to any rule may cause network disconnection for the instance.
  • Before you modify a security group and its rules, clone the security group and then modify the cloned security group to test communication and prevent adverse impact on running services. For details, see Cloning a Security Group.
  • When adding a security group rule for an instance, grant the minimum permissions possible. For example:
    • Open a specific port, for example, 22. It is not recommended that you open a port range, for example, 22-30.
    • It is not recommended that you enter 0.0.0.0/0, allowing traffic to or from all IP addresses.
  • A security group rule takes effect immediately for its associated ECSs after the rule is configured without ECS restart. Regardless of the inbound rules of a security group, the response traffic of the outbound traffic is allowed. If a security group rule does not take effect after being configured, see Why Do My Security Group Rules Not Take Effect?