Updated on 2025-08-01 GMT+08:00

Adding a Security Group Rule

Scenarios

A security group consists of inbound and outbound rules to control the traffic that is allowed to flow into or out of instances (such as ECSs) in the security group. Security group rules are commonly used to allow or deny network traffic from specific sources or over specific protocols, block certain ports, and define specific access permissions for instances.

You can add a security group rule using any of the following methods:
  • Adding Rules to a Security Group: You need to specify the action, priority, type, protocol, port, and source or destination of the security group rule as prompted.
  • Fast-Adding Multiple Security Group Rules: You can quickly add rules with common ports and protocols for remote logins, ping tests, common web services, and database services.
  • Allowing Common Ports with a Few Clicks: You can allow common ports with just a few clicks. This function can be used in the following scenarios:
    • Remote login to ECSs
    • Using the ping command to test ECS connectivity
    • ECSs functioning as web servers to provide website access services

Precautions

Before adding a security group rule, note the following:

  • A security group has inbound and outbound rules to control traffic that is allowed to reach or leave the instances associated with the security group. For details about the rules, see What Are Security Group Rules?
  • If an instance is associated with multiple security groups, the traffic matches security group rules by priority. For details about the matching sequence, see How Traffic Matches Security Group Rules.
  • The number of rules in a security group is limited. Keep only the rules you need. For details, see Constraints on Using Security Groups.
  • After allowing traffic over a port in a security group rule, you need to ensure that the port used by the instance is also opened. For details, see Verifying Security Group Rules.
  • If the source is set to 0.0.0.0/0 or ::/0, then the access from all external IP addresses are either allowed or denied, depending on if the action is Allow or Deny. If the access is allowed, exposing high-risk ports, such as port 22, 3389, or 8848, to the public network will leave your instances vulnerable to network intrusions, causing service interruptions, data leakage, or ransomware attacks. You should only configure known IP addresses for the security group rule.
  • By default, instances in the same security group can communicate with each other. If instances in the same security group cannot communicate with each other, possible causes are as follows:
    • The inbound rules for communications between these instances are deleted. Table 1 shows the inbound rules.
      Table 1 Inbound rules for communication between instances

      Direction

      Priority

      Action

      Type

      Protocol & Port

      Source/Destination

      Inbound

      1

      Allow

      IPv4

      All

      Source: current security group (Sg-A)

      Inbound

      1

      Allow

      IPv6

      All

      Source: current security group (Sg-A)

    • Different VPCs cannot communicate with each other. The instances belong to the same security group but different VPCs.

      You can use VPC peering connections to connect different VPCs.

Configuration Example

Before configuring security group rules, you need to plan access policies for instances in the security group.

  • If an instance needs to provide services for external systems, add an inbound rule to allow external requests to the instance.
  • If there are attacks to an instance from external networks, add an inbound rule to deny external requests that have security risks.
  • If an instance needs to access the Internet, add an outbound rule to allow requests from the instance to the Internet.
  • If you no longer need to control certain inbound or outbound traffic, you can delete the corresponding security group rules to simplify the rule configuration.
  • If you need to copy rules from other security groups, you can import and export security group rules or clone security groups. For details, see Migrating Security Groups Across Regions or Accounts.

Security Group Examples shows more security group rule configuration examples.

Adding Security Group Rules

Verifying Security Group Rules

After allowing traffic over a port in a security group rule, you need to ensure that the port used by the instance is also opened.

For example, if you have deployed a website on an ECS and want users to access your website through HTTP (80), you need to add an inbound rule to the ECS security group to allow access over the port. Table 7 shows the rule.
Table 7 Security group rule

Direction

Priority

Action

Type

Protocol & Port

Source

Inbound

1

Allow

IPv4

TCP: 80

IP address: 0.0.0.0/0

After adding the security group rule, perform the following operations to check whether the ECS port is opened and whether the rule is applied:
  1. Log in to the ECS and check whether the ECS port is opened.
    • Checking the port of a Linux server

      Run the following command to check whether TCP port 80 is being listened on:

      netstat -an | grep 80

      If the following figure is displayed, TCP port 80 is enabled.

      Figure 5 Command output for the Linux ECS
    • Checking the port of a Windows server
      1. Choose Start > Run. Type cmd to open the Command Prompt.
      2. Run the following command to check whether TCP port 80 is being listened on:

        netstat -an | findstr 80

        If the following figure is displayed, TCP port 80 is enabled.

        Figure 6 Command output for the Windows ECS
  2. Enter http://ECS EIP in the address box of the browser and press Enter.

    If the requested page can be accessed, the security group rule has taken effect.