

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 中建立、設定和刪除 Amazon EC2 安全群組 AWS CLI
<a name="cli-services-ec2-sg"></a>

您可以為您的 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體建立基本上作為防火牆執行的安全群組，並使用規則來判斷哪些網路流量可以進入和離開。

使用 AWS Command Line Interface (AWS CLI) 建立安全群組、將規則新增至現有安全群組，以及刪除安全群組。

**注意**  
如需其他命令範例，請參閱[AWS CLI 參考指南](https://docs.aws.amazon.com/cli/latest/reference/index.html)。

**Topics**
+ [先決條件](#cli-services-ec2-sg-prereqs)
+ [建立安全群組](#creating-a-security-group)
+ [新增規則至安全群組](#configuring-a-security-group)
+ [刪除您的安全群組](#deleting-a-security-group)
+ [參考](#cli-services-ec2-sg-references)

## 先決條件
<a name="cli-services-ec2-sg-prereqs"></a>

若要執行 `ec2` 命令，您需要：
+ 安裝及設定 AWS CLI。如需詳細資訊，請參閱[安裝或更新至最新版本的 AWS CLI](getting-started-install.md)及[的身分驗證和存取憑證 AWS CLI](cli-chap-authentication.md)。
+ 設定 IAM 許可，以允許 Amazon EC2 存取。如需有關 Amazon EC2 IAM 許可的詳細資訊，請參閱《Amazon EC2 使用者指南》**中的 [Amazon EC2 IAM 政策](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-policies-for-amazon-ec2.html)。

## 建立安全群組
<a name="creating-a-security-group"></a>

您可以建立與虛擬私有雲端 (VPC) 相關聯的安全性群組。

下列 `[aws ec2 create-security-group](https://docs.aws.amazon.com/cli/latest/reference/ec2/create-security-group.html)` 範例顯示如何為指定的 VPC 建立安全群組。

```
$ aws ec2 create-security-group --group-name my-sg --description "My security group" --vpc-id vpc-1a2b3c4d
{
    "GroupId": "sg-903004f8"
}
```

若要檢視安全群組的初始資訊，請執行 `[aws ec2 describe-security-groups](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-security-groups.html)` 命令。您只能依 `vpc-id` 參考 EC2-VPC 安全群組，不能使用其名稱。

```
$ aws ec2 describe-security-groups --group-ids sg-903004f8
{
    "SecurityGroups": [
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": []
                }
            ],
            "Description": "My security group"
            "IpPermissions": [],
            "GroupName": "my-sg",
            "VpcId": "vpc-1a2b3c4d",
            "OwnerId": "123456789012",
            "GroupId": "sg-903004f8"
        }
    ]
}
```

## 新增規則至安全群組
<a name="configuring-a-security-group"></a>

當您執行 Amazon EC2 執行個體，您必須啟用安全群組中的規則，以允許傳入網路流量來連線到映像。

例如，如果您正在啟動 Windows 執行個體，則通常需新增規則以允許 TCP 連接埠 3389 上的對內流量來支援遠端桌面通訊協定 (RDP)。如果您正在啟動 Linux 執行個體，則通常必須新增規則以允許 TCP 連接埠 22 上的對內流量來支援 SSH 連線。

使用 `[aws ec2 authorize-security-group-ingress](https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-ingress.html)` 命令以新增規則至安全群組。此命令的一項必要參數為您電腦的公有 IP 地址或您電腦連接的網路 (採用地址範圍的形式)，使用 [CIDR](https://wikipedia.org/wiki/Classless_Inter-Domain_Routing) 符號。

**注意**  
我們提供下列服務：https：//[https://checkip.global.api.aws/](https://checkip.global.api.aws/)。若要尋找可協助您識別 IP 地址的其他服務，請使用瀏覽器來搜尋「*我的 IP 地址是什麼*」。如果您透過 ISP 或從防火牆後方使用動態 IP 地址來連線 (透過私有網路的 NAT 閘道)，則您的地址可能會定期變更。在這種情況下，您必須找出用戶端電腦所用 IP 地址的範圍。

以下範例顯示如何新增 RDP 的規則 (TCP 連接埠 3389) 到使用您的 IP 地址、ID 為 `sg-903004f8` 的 EC2-VPC 安全群組。

若要開始，請找出 IP 地址。

```
$ curl https://checkip.amazonaws.com
x.x.x.x
```

然後您就可以透過執行 `[aws ec2 authorize-security-group-ingress](https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-ingress.html)` 命令來將 IP 地址新增至安全群組。

```
$ aws ec2 authorize-security-group-ingress --group-id sg-903004f8 --protocol tcp --port 3389 --cidr x.x.x.x/x
```

以下命令會新增另一個規則，以啟用 SSH 連線到相同安全群組中的執行個體。

```
$ aws ec2 authorize-security-group-ingress --group-id sg-903004f8 --protocol tcp --port 22 --cidr x.x.x.x/x
```

若要檢視安全群組的任何變更，請執行 `[aws ec2 describe-security-groups](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-security-groups.html)` 命令。

```
$ aws ec2 describe-security-groups --group-ids sg-903004f8
{
    "SecurityGroups": [
        {
            "IpPermissionsEgress": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [
                        {
                            "CidrIp": "0.0.0.0/0"
                        }
                    ],
                    "UserIdGroupPairs": []
                }
            ],
            "Description": "My security group"
            "IpPermissions": [
                {
                    "ToPort": 22,
                    "IpProtocol": "tcp",
                    "IpRanges": [
                        {
                            "CidrIp": "x.x.x.x/x"
                        }
                    ]
                    "UserIdGroupPairs": [],
                    "FromPort": 22
                }
            ],
            "GroupName": "my-sg",
            "OwnerId": "123456789012",
            "GroupId": "sg-903004f8"
        }
    ]
}
```

## 刪除您的安全群組
<a name="deleting-a-security-group"></a>

若要刪除安全群組，請執行 `[aws ec2 delete-security-group](https://docs.aws.amazon.com/cli/latest/reference/ec2/delete-security-group.html)` 命令。

**注意**  
您不能刪除目前已經連接到環境的安全群組。

下列命令範例會刪除 EC2-VPC 安全群組。

```
$ aws ec2 delete-security-group --group-id sg-903004f8
```

## 參考
<a name="cli-services-ec2-sg-references"></a>

**AWS CLI 參考：**
+ `[aws ec2](https://docs.aws.amazon.com/cli/latest/reference/ec2/index.html)`
+ `[aws ec2 authorize-security-group-ingress](https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-ingress.html)`
+ `[aws ec2 create-security-group](https://docs.aws.amazon.com/cli/latest/reference/ec2/create-security-group.html)`
+ `[aws ec2 delete-security-group](https://docs.aws.amazon.com/cli/latest/reference/ec2/delete-security-group.html)`
+ `[aws ec2 describe-security-groups](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-security-groups.html)`

**其他參考：**
+ [Amazon Elastic Compute Cloud 文件](https://docs.aws.amazon.com/ec2/)
+ 若要檢視和貢獻 AWS SDK 和 AWS CLI 程式碼範例，請參閱 *GitHub* 上的[AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/)。