Updated on 2024-04-11 GMT+08:00

Security Groups and Security Group Rules

Security Groups

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 configure access rules that will apply to all cloud resources added to this security group.

Security Group Rules

  • A security group has inbound and outbound rules to control traffic that's allowed to reach or leave the instances associated with the security group.
    • Inbound rules: control traffic to the instances in a security group.
    • Outbound rules: control traffic from the instances in a security group for accessing external networks.
  • You can specify protocol, port, source or destination for a security group rule. The following describes key information about a security group.
    • Type: IPv4 or IPv6.
    • Protocol & Port: network protocol type and port range.
      • Network protocol: The protocol can be TCP, UDP, ICMP, or GRE.
      • Port range: The value ranges from 1 to 65535.
    • Source or Destination: source address of traffic in the inbound direction or destination address of traffic in the outbound direction.

How Security Groups Work

  • 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. If an inbound rule is modified, the modified rule immediately takes effect for the existing traffic. Changes to outbound security group rules do not affect existing persistent connections and take effect only for new connections.

    If you add, modify, or delete a security group rule, or add or remove an instance to or from a security group, the inbound connections of all instances in the security group will be automatically cleared.

    • The existing inbound persistent connections will be disconnected. All the new connections will match the new rules.
    • The existing outbound persistent connections will not be disconnected, and the original rule will still be applied. All the new connections will match the new rules.

After a persistent connection is disconnected, new connections will not be established immediately until the timeout period of connection tracking expires. For example, after an ICMP persistent connection is disconnected, a new connection will be established and a new rule will apply when the timeout period (30s) expires.

  • The timeout period of connection tracking varies by protocol. The timeout period of a TCP connection in the established state is 600s, and that of an ICMP connection is 30s. For other protocols, if packets are received in both inbound and outbound directions, the connection tracking timeout period is 180s. If packets are received only in one direction, the connection tracking timeout period is 30s.
  • The timeout period of TCP connections varies by connection status. The timeout period of a TCP connection in the established state is 600s, and that of a TCP connection in the FIN-WAIT state is 30s.

Security Group Constraints

  • By default, you can add up to 50 security group rules to a security group.
  • By default, you can add an ECS or extension NIC to up to five security groups. In such a case, the rules of all the selected security groups are aggregated to take effect.
  • A security group can have no more than 6,000 instances associated, or its performance will deteriorate.