# aws ec2 describe-security-groups

AWS CLI commands · Updated 16 November 2026

**Short answer:** aws ec2 describe-security-groups lists security groups in a region with all their inbound (IpPermissions) and outbound (IpPermissionsEgress) rules. Use --filters such as Name=vpc-id or Name=ip-permission.cidr,Values=0.0.0.0/0 to narrow it, and --query to flatten the deeply nested rules.

## What the command does

Each security group in the output carries its ID, name, VPC, description, tags and two rule lists. Every rule has a protocol, a port range and one or more sources: IPv4 CIDRs in `IpRanges`, IPv6 in `Ipv6Ranges`, other groups in `UserIdGroupPairs`, and prefix lists in `PrefixListIds`. A protocol of `-1` means all traffic, in which case there are no port fields.

## Syntax

```
aws ec2 describe-security-groups \
  [--group-ids sg-0123456789abcdef0 ...] \
  [--filters "Name=vpc-id,Values=vpc-0123456789abcdef0"] \
  [--query ...] [--output table]
```

Prefer `--group-ids` or a `group-name` filter over `--group-names`: the latter only resolves names in the default VPC.

## Worked examples

**List groups in a VPC:**

```
aws ec2 describe-security-groups \
  --filters "Name=vpc-id,Values=vpc-0123456789abcdef0" \
  --query "SecurityGroups[].[GroupId,GroupName,Description]" --output table
```

**Read one group's inbound rules in a flat form:**

```
aws ec2 describe-security-groups --group-ids sg-0123456789abcdef0 \
  --query "SecurityGroups[0].IpPermissions[].{Proto:IpProtocol,From:FromPort,To:ToPort,CIDRs:join(',',IpRanges[].CidrIp),Groups:join(',',UserIdGroupPairs[].GroupId)}" \
  --output table
```

**Groups with any rule open to the whole internet:**

```
aws ec2 describe-security-groups \
  --filters "Name=ip-permission.cidr,Values=0.0.0.0/0" \
  --query "SecurityGroups[].[GroupId,GroupName]" --output text
```

**Groups with SSH open to the world**, checked inside a single rule so port and CIDR must match together:

```
aws ec2 describe-security-groups \
  --query 'SecurityGroups[?IpPermissions[?FromPort==`22` && IpRanges[?CidrIp==`0.0.0.0/0`]]].[GroupId,GroupName]' \
  --output text
```

**What is using this group?** Security groups attach to network interfaces, so ask for those:

```
aws ec2 describe-network-interfaces \
  --filters "Name=group-id,Values=sg-0123456789abcdef0" \
  --query "NetworkInterfaces[].[NetworkInterfaceId,InterfaceType,Description,Attachment.InstanceId]" \
  --output table
```

## Useful filters

| Filter | Matches |
|---|---|
| `vpc-id` | Groups in a VPC. |
| `group-name` | By name, in any VPC; wildcards allowed. |
| `ip-permission.cidr` | Any inbound rule with that IPv4 CIDR. |
| `ip-permission.from-port` / `to-port` | Any inbound rule with that port. |
| `ip-permission.group-id` | Groups that allow traffic from another group. |
| `egress.ip-permission.cidr` | Outbound rules with that CIDR. |
| `tag:<key>` | By tag value. |

## Gotchas

- **Rule filters are matched independently.** `ip-permission.from-port=22` plus `ip-permission.cidr=0.0.0.0/0` also matches a group with port 22 open to a private range and port 443 open to the world. Use the JMESPath form above when the pair must be on one rule.
- **Port ranges.** A rule of 0–65535 opens port 22 too, but ``FromPort==`22``` will not catch it. For audits, also look for `IpProtocol=='-1'` and wide ranges.
- **IPv6.** Open-to-world IPv6 rules use `::/0` in `Ipv6Ranges`; a CIDR filter on `0.0.0.0/0` misses them.
- **Rule IDs.** This command does not show `sgr-` rule IDs. Use `aws ec2 describe-security-group-rules` when you need them for revoke or modify calls.

## Questions

### How do I list all security groups with the CLI?

aws ec2 describe-security-groups --query "SecurityGroups[].[GroupId,GroupName,VpcId]" --output table lists every group in the current region. Repeat with --region for others.

### How do I find security groups that allow 0.0.0.0/0?

Use --filters "Name=ip-permission.cidr,Values=0.0.0.0/0" for inbound rules, and check Ipv6Ranges for ::/0 separately.

### How do I describe network interfaces for a security group?

Run aws ec2 describe-network-interfaces --filters "Name=group-id,Values=sg-0123456789abcdef0". Any interface listed is using the group, including Lambda, RDS and load balancer ENIs.

## When this happens away from your desk

An open port found during an incident is easier to judge with the right context in front of you. CloudConsole, a phone app for AWS, is built to show resources and their alarms on a phone, with guardrails for production.

[About the CloudConsole app](https://cloudconsole.dev/aws-console-app) · [Download on the App Store](https://apps.apple.com/app/cloudconsole-for-aws/id6835419710)

## Sources

- [AWS CLI Command Reference: ec2 describe-security-groups](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-security-groups.html)
- [DescribeSecurityGroups API reference](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeSecurityGroups.html)

## Related

- [aws ec2 authorize-security-group-ingress](https://cloudconsole.dev/cli/ec2-authorize-security-group-ingress)
- [aws ec2 revoke-security-group-ingress](https://cloudconsole.dev/cli/ec2-revoke-security-group-ingress)
- [aws ec2 describe-vpcs](https://cloudconsole.dev/cli/ec2-describe-vpcs)
- [ssh: connect to host ec2 port 22: Connection timed out](https://cloudconsole.dev/errors/ec2-ssh-connection-timed-out)
- [Could not connect to RDS: connection timed out](https://cloudconsole.dev/errors/rds-connection-timed-out)
- [aws ec2 create-image](https://cloudconsole.dev/cli/ec2-create-image)

---

Source: https://cloudconsole.dev/cli/ec2-describe-security-groups

CloudConsole is an independent iPhone and Android app for Amazon Web Services by Cloudconcur Ltd. It is not affiliated with Amazon Web Services.
