

# 更改 Amazon EC2 实例的安全组
<a name="changing-security-group"></a>

您可以在启动 Amazon EC2 实例时指定安全组。启动实例后，即可添加或删除安全组。您也可以随时添加、删除或编辑关联安全组的安全组规则。

安全组与网络接口关联。添加或删除安全组会更改与主网络接口关联的安全组。您还可以更改与任何辅助网络接口关联的安全组。有关更多信息，请参阅 [修改网络接口属性](modify-network-interface-attributes.md)。

**Topics**
+ [添加或删除安全组](#add-remove-instance-security-groups)
+ [配置安全组规则](#add-remove-security-group-rules)

## 添加或删除安全组
<a name="add-remove-instance-security-groups"></a>

启动实例后，即可在关联的安全组列表中添加或删除安全组。当您将多个安全组与一个实例相关联时，将有效汇总每个安全组的规则，以创建一组规则。Amazon EC2 使用这组规则确定是否允许流量。

**要求**
+ 实例必须处于 `running` 或 `stopped` 状态。

------
#### [ Console ]

**更改实例的安全组**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在导航窗格中，选择 **Instances (实例)**。

1. 选择您的实例，依次选择 **Actions**（操作）、**Security**（安全）和 **Change security groups**（更改安全组）。

1. 对于 **Associated security groups (关联的安全组)**，从列表中选择一个安全组，然后选择 **Add security group (添加安全组)**。

   要删除已关联的安全组，请为该安全组选择 **Remove (删除)**。

1. 选择**保存**。

------
#### [ AWS CLI ]

**更改实例的安全组**  
使用以下 [modify-instance-attribute](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-instance-attribute.html) 命令。

```
aws ec2 modify-instance-attribute \
    --instance-id i-1234567890abcdef0 \
    --groups sg-1234567890abcdef0
```

------
#### [ PowerShell ]

**更改实例的安全组**  
使用 [Edit-EC2InstanceAttribute](https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-EC2InstanceAttribute.html) cmdlet。

```
Edit-EC2InstanceAttribute `
    -InstanceId i-1234567890abcdef0 `
    -Group sg-1234567890abcdef0
```

------

## 配置安全组规则
<a name="add-remove-security-group-rules"></a>

创建安全组后，即可添加、更新或删除安全组的规则。在添加、更新或删除规则时，更改将自动应用于与安全组关联的资源。

有关可以添加到安全组的规则示例，请参阅[针对不同使用案例的安全组规则](security-group-rules-reference.md)。

**所需的权限**  
在开始之前，请确保您拥有所需权限。有关更多信息，请参阅 [示例：使用安全组](iam-policies-ec2-console.md#ex-security-groups)。

**协议和端口**
+ 使用控制台，当您选择预定义的类型时，将为您指定**协议**和**端口范围**。要输入端口范围，必须选择以下自定义类型之一：**自定义 TCP** 或**自定义 UDP**。
+ 使用 AWS CLI，您可以使用 `--protocol` 和 `--port` 选项添加带有单个端口的单个规则。要添加多个规则或具有端口范围的规则，请改用 `--ip-permissions` 选项。

**来源和目标**
+ 使用控制台，您可以将以下内容指定为入站规则的源或出站规则的目标：
  + **自定义**：IPv4 CIDR 数据块、IPv6 CIDR 数据块、安全组或前缀列表。
  + **Anywhere-IPv4** – 0.0.0.0/0 IPv4 CIDR 数据块。
  + **Anywhere-IPv6** – ::/0 IPv6 CIDR 数据块。
  + **我的 IP** – 本地计算机的公有 IPv4 地址。
+ 使用 AWS CLI，您可以使用 `--cidr` 选项指定 IPv4 CIDR 数据块，也可以使用 `--source-group` 选项指定安全组。要指定前缀列表或 IPv6 CIDR 数据块，请使用 `--ip-permissions` 选项。

**警告**  
如果要为端口 22（SSH）或 3389（RDP）添加入站规则，强烈建议仅授权特定 IP 地址或特定范围内的 IP 地址访问实例。如果选择 **Anywhere-IPv4**，则允许来自所有 IPv4 地址的流量使用指定协议访问实例。如果选择 **Anywhere-IPv6**，则允许来自所有 IPv6 地址的流量使用指定协议访问实例。

------
#### [ Console ]

**配置安全组规则**

1. 通过以下网址打开 Amazon EC2 控制台：[https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)。

1. 在导航窗格中，选择 **Security Groups**（安全组）。

1. 选择安全组。

1. 要编辑入站规则，请从**操作**或**入站规则**选项卡中选择**编辑入站规则**。

   1. 要添加规则，请选择**添加规则**，再输入规则的类型、协议、端口和来源。

      如果类型为 TCP 或 UDP，则必须输入允许的端口范围。对于自定义 ICMP，您必须从 **Protocol**（协议）中选择 ICMP 类型名称，并从 **Port range**（端口范围）中选择代码名称（如果适用）。对于任何其他类型，则会为您配置协议和端口范围。

   1. 要更新规则，请根据需要更改规则的协议、描述和来源。但是，您无法更改来源类型。例如，若来源是 IPv4 CIDR 数据块，则无法指定 IPv6 CIDR 数据块、前缀列表或安全组。

   1. 要删除规则，请选择规则的**删除**按钮。

1. 要编辑出站规则，请从**操作**或**出站规则**选项卡中选择**编辑出站规则**。

   1. 要添加规则，请选择**添加规则**，再输入规则的类型、协议、端口和目标。您也可以输入可选描述。

      如果类型为 TCP 或 UDP，则必须输入允许的端口范围。对于自定义 ICMP，您必须从 **Protocol**（协议）中选择 ICMP 类型名称，并从 **Port range**（端口范围）中选择代码名称（如果适用）。对于任何其他类型，则会为您配置协议和端口范围。

   1. 要更新规则，请根据需要更改规则的协议、描述和来源。但是，您无法更改来源类型。例如，若来源是 IPv4 CIDR 数据块，则无法指定 IPv6 CIDR 数据块、前缀列表或安全组。

   1. 要删除规则，请选择规则的**删除**按钮。

1. 选择**保存规则**。

------
#### [ AWS CLI ]

**添加安全组规则**  
使用 [authorize-security-group-ingress](https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-ingress.html) 命令添加入站规则。以下示例会允许来自指定前缀列表中的 CIDR 数据块的入站 SSH 流量。

```
aws ec2 authorize-security-group-ingress \
    --group-id sg-1234567890abcdef0 \
    --ip-permissions 'IpProtocol=tcp,FromPort=22,ToPort=22,PrefixListIds=[{PrefixListId=pl-f8a6439156EXAMPLE}]'
```

使用 [authorize-security-group-egress](https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-egress.html) 命令添加出站规则。以下示例会允许端口 80 上指向具有指定安全组的实例的出站 TCP 流量。

```
aws ec2 authorize-security-group-egress \
    --group-id sg-1234567890abcdef0 \
    --ip-permissions 'IpProtocol=tcp,FromPort=80,ToPort=80,UserIdGroupPairs=[{GroupId=sg-0aad1c26bb6EXAMPLE}]'
```

**移除安全组规则**  
使用以下 [revoke-security-group-ingress](https://docs.aws.amazon.com/cli/latest/reference/ec2/revoke-security-group-ingress.html) 命令移除入站规则。

```
aws ec2 revoke-security-group-egress \
    --group id sg-1234567890abcdef0 \
    --security-group-rule-ids sgr-09ed298024EXAMPLE
```

使用以下 [revoke-security-group-egress](https://docs.aws.amazon.com/cli/latest/reference/ec2/revoke-security-group-egress.html) 命令移除出站规则。

```
aws ec2 revoke-security-group-ingress \
    --group id sg-1234567890abcdef0 \
    --security-group-rule-ids sgr-0352250c1aEXAMPLE
```

**修改安全组规则**  
使用 [modify-security-group-rules](https://docs.aws.amazon.com/cli/latest/reference/ec2/modify-security-group-rules.html) 命令。以下示例会更改指定安全组规则的 IPv4 CIDR 数据块。

```
aws ec2 modify-security-group-rules \
    --group id sg-1234567890abcdef0 \
    --security-group-rules 'SecurityGroupRuleId=sgr-09ed298024EXAMPLE,SecurityGroupRule={IpProtocol=tcp,FromPort=80,ToPort=80,CidrIpv4=0.0.0.0/0}'
```

------
#### [ PowerShell ]

**添加安全组规则**  
使用 [Grant-EC2SecurityGroupIngress](https://docs.aws.amazon.com/powershell/latest/reference/items/Grant-EC2SecurityGroupIngress.html) cmdlet 添加入站规则。以下示例会允许来自指定前缀列表中的 CIDR 数据块的入站 SSH 流量。

```
$plid = New-Object -TypeName Amazon.EC2.Model.PrefixListId
$plid.Id = "pl-f8a6439156EXAMPLE"
Grant-EC2SecurityGroupIngress `
    -GroupId sg-1234567890abcdef0 `
    -IpPermission @{IpProtocol="tcp"; FromPort=22; ToPort=22; PrefixListIds=$plid}
```

使用 [Grant-EC2SecurityGroupEgress](https://docs.aws.amazon.com/powershell/latest/reference/items/Grant-EC2SecurityGroupEgress.html) cmdlet 添加出站规则。以下示例会允许端口 80 上指向具有指定安全组的实例的出站 TCP 流量。

```
$uigp = New-Object -TypeName Amazon.EC2.Model.UserIdGroupPair
$uigp.GroupId = "sg-0aad1c26bb6EXAMPLE"
Grant-EC2SecurityGroupEgress `
    -GroupId sg-1234567890abcdef0 `
    -IpPermission @{IpProtocol="tcp"; FromPort=80; ToPort=80; UserIdGroupPairs=$uigp}
```

**移除安全组规则**  
使用 [Revoke-EC2SecurityGroupIngress](https://docs.aws.amazon.com/powershell/latest/reference/items/Revoke-EC2SecurityGroupIngress.html) cmdlet 移除入站规则。

```
Revoke-EC2SecurityGroupIngress `
    -GroupId sg-1234567890abcdef0 `
    -SecurityGroupRuleId sgr-09ed298024EXAMPLE
```

使用 [Revoke-EC2SecurityGroupEgress](https://docs.aws.amazon.com/powershell/latest/reference/items/Revoke-EC2SecurityGroupEgress.html) cmdlet 移除出站规则。

```
Revoke-EC2SecurityGroupEgress `
    -GroupId sg-1234567890abcdef0 `
    -SecurityGroupRuleId sgr-0352250c1aEXAMPLE
```

**修改安全组规则**  
使用 [Edit-EC2SecurityGroupRule](https://docs.aws.amazon.com/powershell/latest/reference/items/Edit-EC2SecurityGroupRule.html) cmdlet。以下示例会更改指定安全组规则的 IPv4 CIDR 数据块。

```
$sgrr = New-Object -TypeName Amazon.EC2.Model.SecurityGroupRuleRequest
$sgrr.IpProtocol = "tcp"
$sgrr.FromPort = 80
$sgrr.ToPort = 80
$sgrr.CidrIpv4 = "0.0.0.0/0"
$sgr = New-Object -TypeName Amazon.EC2.Model.SecurityGroupRuleUpdate
$sgr.SecurityGroupRuleId = "sgr-09ed298024EXAMPLE"
$sgr.SecurityGroupRule = $sgrr
Edit-EC2SecurityGroupRule  `
    -GroupId sg-1234567890abcdef0 `
    -SecurityGroupRule $sgr
```

------