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

Adding a Security Group Rule

Scenarios

A security group consists of inbound and outbound rules. You can add security group rules to allow or deny the traffic to reach and leave the instances (such as ECSs) in the security group.

Security group rules allow or deny network traffic from specific sources over specific protocols or specific ports.

Precautions

  • Before configuring security group rules, you need to plan access policies for instances in the security group. For details about common security group rules, see Security Group Examples.
  • Add as fewer rules as possible. Notes and Constraints lists the constraints on the number of rules in a security group.
  • After allowing traffic over a port in a security group rule, ensure that the port used by the instance is opened. For details, see Verifying Security Group Rules.
  • 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 VPCs in different regions.

Adding Rules to a Security Group

  1. Log in to the management console.
  2. Click in the upper left corner and select the desired region and project.
  3. Click in the upper left corner and choose Networking > Virtual Private Cloud.

    The Virtual Private Cloud page is displayed.

  4. In the navigation pane on the left, choose Access Control > Security Groups.

    The security group list is displayed.

  5. Locate the target security group and click Manage Rules in the Operation column.

    The page for configuring security group rules is displayed.

  6. On the Inbound Rules tab, click Add Rule.

    The Add Inbound Rule dialog box is displayed.

  7. Configure required parameters.

    You can click to add more inbound rules.

    Figure 1 Add Inbound Rule
    Table 2 Inbound rule parameter description

    Parameter

    Description

    Example Value

    Priority

    The security group rule priority.

    The priority value ranges from 1 to 100. The default value is 1 and has the highest priority. The security group rule with a smaller value has a higher priority.

    1

    Action

    The value can be Allow or Deny.
    • If the Action is set to Allow, access from the source is allowed to ECSs in the security group over specified ports.
    • If the Action is set to Deny, access from the source is denied to ECSs in the security group over specified ports.

    Security group rules are matched by priority and then by action. Deny rules take precedence over allow rules. For more information, see How Traffic Matches Security Group Rules.

    Allow

    Type

    Source IP address version. You can select:
    • IPv4
    • IPv6

    IPv4

    Protocol & Port

    The network protocol used to match traffic in a security group rule. The protocol can be All, TCP, UDP, GRE, or ICMP.

    TCP

    Destination port used to match traffic in a security group rule. The value can be from 1 to 65535.

    Inbound rules control incoming traffic over specific ports to instances in the security group.

    Specify one of the following:
    • Individual port: Enter a port, such as 22.
    • Consecutive ports: Enter a port range, such as 22-30.
    • All ports: Leave it empty or enter 1-65535.

    22, or 22-30

    Source

    Source of the security group rule. The value can be an IP address or a security group, to allow access from IP addresses or instances in the security group.
    • IP address:
      • Single IP address: 192.168.10.10/32 (IPv4); 2002:50::44/128 (IPv6)
      • All IP addresses: 0.0.0.0/0 (IPv4); ::/0 (IPv6)
      • IP address range: 192.168.1.0/24 (IPv4); 2407:c080:802:469::/64 (IPv6)

    If the source is a security group, this rule will apply to all instances associated with the selected security group.

    192.168.0.0/24

    Description

    Supplementary information about the security group rule. This parameter is optional.

    The security group rule description can contain a maximum of 255 characters and cannot contain angle brackets (< or >).

    N/A

  8. Click OK.

    The inbound rule list is displayed.

  9. On the Outbound Rules tab, click Add Rule.

    The Add Outbound Rule dialog box is displayed.

  10. Configure required parameters.

    You can click to add more outbound rules.

    Figure 2 Add Outbound Rule
    Table 3 Outbound rule parameter description

    Parameter

    Description

    Example Value

    Priority

    The security group rule priority.

    The priority value ranges from 1 to 100. The default value is 1 and has the highest priority. The security group rule with a smaller value has a higher priority.

    1

    Action

    The value can be Allow or Deny.
    • If the Action is set to Allow, access from ECSs in the security group is allowed to the destination over specified ports.
    • If the Action is set to Deny, access from ECSs in the security group is denied to the destination over specified ports.

    Security group rules are matched by priority and then by action. Deny rules take precedence over allow rules. For more information, see How Traffic Matches Security Group Rules.

    Allow

    Type

    Destination IP address version. You can select:
    • IPv4
    • IPv6

    IPv4

    Protocol & Port

    The network protocol used to match traffic in a security group rule. The protocol can be All, TCP, UDP, GRE, or ICMP.

    TCP

    Destination port used to match traffic in a security group rule. The value can be from 1 to 65535.

    Outbound rules control outgoing traffic over specific ports from instances in the security group.

    Specify one of the following:
    • Individual port: Enter a port, such as 22.
    • Consecutive ports: Enter a port range, such as 22-30.
    • All ports: Leave it empty or enter 1-65535.

    22, or 22-30

    Destination

    Destination of the security group rule. The value can be an IP address or a security group, to allow access to IP addresses or instances in the security group.

    • IP address:
      • Single IP address: 192.168.10.10/32 (IPv4); 2002:50::44/128 (IPv6)
      • All IP addresses: 0.0.0.0/0 (IPv4); ::/0 (IPv6)
      • IP address range: 192.168.1.0/24 (IPv4); 2407:c080:802:469::/64 (IPv6)

    0.0.0.0/0

    Description

    Supplementary information about the security group rule. This parameter is optional.

    The security group rule description can contain a maximum of 255 characters and cannot contain angle brackets (< or >).

    N/A

  11. Click OK.

    The outbound rule list is displayed.

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 4 shows the rule.
Table 4 Security group rule

Direction

Protocol & Port

Source

Inbound

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 3 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 4 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.