使用視窗建立安全性群組 PowerShell - AWS Tools for PowerShell

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

使用視窗建立安全性群組 PowerShell

您可以使用 AWS Tools for PowerShell 建立和設定安全性群組。建立安全群組時,您需要指定該安全群組適用於 EC2-Classic 或 EC2-VPC。回應為安全群組的 ID。

如果您需要連接到您的執行個體,則必須設定安全群組以允許 SSH 流量 (Linux) 或 RDP 流量 (Windows)。

必要條件

您需要電腦的公有 IP 地址 (使用 CIDR 表示法)。您可以透過一項服務來取得本機電腦的公有 IP 地址。例如,Amazon 提供以下服務:http://checkip.amazonaws.com/https://checkip.amazonaws.com/。若要尋找其他能夠提供您的 IP 地址之服務,請搜尋字詞「what is my IP address」(我的 IP 地址為何)。如果您透過 ISP 或是從防火牆後方進行連線,不具備靜態 IP 地址,則需要找到您的用戶端電腦可以使用的 IP 地址範圍。

警告

若您指定 0.0.0.0/0,您便會啟用來自世界任何 IP 地址的流量。針對 SSH 和 RDP 通訊協定,您可能會將在測試環境中短暫進行此作業視為沒有問題,但用在生產環境則不安全。在生產環境中,請務必僅授權來自適當個別 IP 地址或地址範圍的存取。

建立適用於 EC2-Classic 的安全群組

警告

我們將於 2022 年 8 月 15 日淘汰 EC2-Classic。建議您從 EC2-Classic 遷移至 VPC。如需詳細資訊,請參閱 Amazon EC2 使用者指南或 Amazon EC2 使用者指南中的從 EC2 傳統版遷移到 VPC。也請參閱部落格文章 EC2-Classic 網路正在淘汰 - 本文介紹如何準備

以下範例會使用 New-EC2SecurityGroup Cmdlet 來建立 EC2-Classic 的安全群組。

PS > New-EC2SecurityGroup -GroupName myPSSecurityGroup -GroupDescription "EC2-Classic from PowerShell" sg-0a346530123456789

若要檢視安全群組的初始設定,請使用 Get-EC2SecurityGroup cmdlet。

PS > Get-EC2SecurityGroup -GroupNames myPSSecurityGroup Description : EC2-Classic from PowerShell GroupId : sg-0a346530123456789 GroupName : myPSSecurityGroup IpPermissions : {} IpPermissionsEgress : {Amazon.EC2.Model.IpPermission} OwnerId : 123456789012 Tags : {} VpcId : vpc-9668ddef

若要設定安全群組以允許 TCP 連接埠 22 (SSH) 和 TCP 連接埠 3389 上的對內流量,請使用 Grant-EC2SecurityGroupIngress cmdlet。例如,以下範例指令碼會示範如何啟用來自單一 IP 地址 203.0.113.25/32 的 SSH 流量。

$cidrBlocks = New-Object 'collections.generic.list[string]' $cidrBlocks.add("203.0.113.25/32") $ipPermissions = New-Object Amazon.EC2.Model.IpPermission $ipPermissions.IpProtocol = "tcp" $ipPermissions.FromPort = 22 $ipPermissions.ToPort = 22 ipPermissions.IpRanges = $cidrBlocks Grant-EC2SecurityGroupIngress -GroupName myPSSecurityGroup -IpPermissions $ipPermissions

若要驗證安全群組已更新,請再次執行 Get-EC2SecurityGroup Cmdlet。請注意,您無法指定 EC2-Classic 的傳出規則。

PS > Get-EC2SecurityGroup -GroupNames myPSSecurityGroup OwnerId : 123456789012 GroupName : myPSSecurityGroup GroupId : sg-0a346530123456789 Description : EC2-Classic from PowerShell IpPermissions : {Amazon.EC2.Model.IpPermission} IpPermissionsEgress : {} VpcId : Tags : {}

若要查看安全群組規則,請使用 IpPermissions 屬性。

PS > (Get-EC2SecurityGroup -GroupNames myPSSecurityGroup).IpPermissions IpProtocol : tcp FromPort : 22 ToPort : 22 UserIdGroupPairs : {} IpRanges : {203.0.113.25/32}

建立適用於 EC2-VPC 的安全群組

以下 New-EC2SecurityGroup 範例會新增 -VpcId 參數來建立指定 VPC 的安全群組。

PS > $groupid = New-EC2SecurityGroup ` -VpcId "vpc-da0013b3" ` -GroupName "myPSSecurityGroup" ` -GroupDescription "EC2-VPC from PowerShell"

若要檢視安全群組的初始設定,請使用 Get-EC2SecurityGroup cmdlet。在預設情況下,適用於 VPC 的安全群組包含允許所有對外流量的規則。請注意,EC2-VPC 的安全群組不能按名稱引用。

PS > Get-EC2SecurityGroup -GroupId sg-5d293231 OwnerId : 123456789012 GroupName : myPSSecurityGroup GroupId : sg-5d293231 Description : EC2-VPC from PowerShell IpPermissions : {} IpPermissionsEgress : {Amazon.EC2.Model.IpPermission} VpcId : vpc-da0013b3 Tags : {}

若要定義 TCP 連接埠 22 (SSH) 及 TCP 連接埠 3389 上傳入流量的許可,請使用 New-Object Cmdlet。以下範例指令碼會定義單一 IP 地址 203.0.113.25/32 在 TCP 連接埠 22 和 3389 的許可。

$ip1 = new-object Amazon.EC2.Model.IpPermission $ip1.IpProtocol = "tcp" $ip1.FromPort = 22 $ip1.ToPort = 22 $ip1.IpRanges.Add("203.0.113.25/32") $ip2 = new-object Amazon.EC2.Model.IpPermission $ip2.IpProtocol = "tcp" $ip2.FromPort = 3389 $ip2.ToPort = 3389 $ip2.IpRanges.Add("203.0.113.25/32") Grant-EC2SecurityGroupIngress -GroupId $groupid -IpPermissions @( $ip1, $ip2 )

若要驗證安全群組是否已更新,請再次使用 Get-EC2SecurityGroup cmdlet。

PS > Get-EC2SecurityGroup -GroupIds sg-5d293231 OwnerId : 123456789012 GroupName : myPSSecurityGroup GroupId : sg-5d293231 Description : EC2-VPC from PowerShell IpPermissions : {Amazon.EC2.Model.IpPermission} IpPermissionsEgress : {Amazon.EC2.Model.IpPermission} VpcId : vpc-da0013b3 Tags : {}

若要檢視傳入規則,您可以從先前命令傳回的集合物件中擷取 IpPermissions 屬性。

PS > (Get-EC2SecurityGroup -GroupIds sg-5d293231).IpPermissions IpProtocol : tcp FromPort : 22 ToPort : 22 UserIdGroupPairs : {} IpRanges : {203.0.113.25/32} IpProtocol : tcp FromPort : 3389 ToPort : 3389 UserIdGroupPairs : {} IpRanges : {203.0.113.25/32}