如何判断AMI为Ubuntu或Amazon Linux及自动获取EC2实例SSH用户名
Great question—relying on root_device_name to guess the SSH username is definitely fragile, since AMI configurations can vary (even official ones might shift over time, and custom AMIs often break this pattern). Let's walk through more reliable approaches:
These methods avoid the pitfalls of root device name checks:
Parse AMI Names/Descriptions (Most Reliable for External Scripts)
Official AWS AMIs follow consistent naming conventions that clearly indicate the OS. Use the AWS CLI or SDK to fetch AMI details withaws ec2 describe-images --image-ids <YOUR-AMI-ID>, then look at theNameorDescriptionfield:- If the name includes
Amazon Linux(e.g.,amzn2-ami-hvm-2.0.20240320.0-x86_64-gp2oral2023-ami-2024.0.20240410.0-x86_64-kernel-6.1), the SSH username isec2-user. - If it includes
Ubuntu(e.g.,ubuntu-jammy-22.04-amd64-server-20240422), the username isubuntu.
This works for most custom AMIs too, since many admins retain OS identifiers in the AMI name.
- If the name includes
Check AMI
platform-detailsField
Thedescribe-imagesresponse also includes aplatform-detailsfield that explicitly states the OS version. For example:- Amazon Linux 2 returns
Amazon Linux 2 - Ubuntu 22.04 returns
Ubuntu Server 22.04 LTS
You can parse this string directly to map to the correct SSH username.
- Amazon Linux 2 returns
Fetch OS Info from Instance Metadata (Most Accurate Post-Launch)
If you can run commands on the instance after it starts, pull OS details directly from the system itself via instance metadata or local files:# Get OS name from instance metadata dynamic data curl http://169.254.169.254/latest/dynamic/instance-identity/document | grep "osName" # Or read the OS release file cat /etc/os-release | grep PRETTY_NAMEMatch the output to
ec2-user(Amazon Linux) orubuntu(Ubuntu) — this is foolproof because it comes directly from the running OS.
To reliably determine an AMI's OS, use these methods instead of root device checks:
Regex Matching on AMI Names
Use regular expressions to match standardized AMI name patterns:- Amazon Linux 2023:
^al2023-ami- - Amazon Linux 2:
^amzn2-ami- - Ubuntu:
^ubuntu-
This works for all official AMIs and most custom ones.
- Amazon Linux 2023:
AWS Marketplace Product Codes
For AMIs from the AWS Marketplace, theproduct-codesfield indescribe-imageswill include the vendor's account ID. For example, Canonical (Ubuntu) uses099720109477, while Amazon's official AMIs use their own internal codes. Note this only applies to Marketplace AMIs, not custom ones.Combine Virtualization & Root Device Type (Fallback)
Whileroot_device_namealone is unreliable, pairing it withvirtualization-type(e.g.,hvm) and AMI name hints can add extra validation — but this should be a last resort compared to the methods above.
Root device names like /dev/xvda or /dev/sda1 aren't tied to the OS. Custom Amazon Linux AMIs might use /dev/sda1, and some specialized Ubuntu AMIs could use /dev/xvda. This makes the check prone to false positives/negatives.
内容的提问来源于stack exchange,提问作者Yaroslav Bulatov

