Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 51Cloud · AWS

How to log in to AWS with SSO from the terminal

Long-lived access keys in ~/.aws/credentials are the number one way AWS accounts get hacked: they leak in Git, in screenshots, in old laptops. The modern way for people is IAM Identity Center (SSO): you log in in the browser, the CLI gets short-lived credentials, and nothing secret stays on disk for long. This how-to sets up SSO profiles for several accounts, logs in (also on a server without a browser), and fixes the usual errors.

  • 5 Lessons
  • 7 Commands

$ run as a normal user# run as root (or with sudo)

51.1

The quick answer

aws configure sso
aws sso login --profile prod
export AWS_PROFILE=prod
aws sts get-caller-identity
{
    "Account": "111122223333",
    "Arn": "arn:aws:sts::111122223333:assumed-role/AWSReservedSSO_ReadOnlyAccess_0123abcd/alice"
}

Your admin gives you two values: the start URL (https://acme.awsapps.com/start) and the SSO region.

51.2

Step by step

  1. Use AWS CLI version 2 (version 1 from apt/pip has no modern SSO):
aws --version
aws-cli/2.31.0 Python/3.13.7 Linux/6.12 exe/x86_64
  1. Create the profile with the wizard (aws configure sso) — or write it yourself. One sso-session and one profile per account and role:
~/.aws/config
[sso-session acme]
sso_start_url = https://acme.awsapps.com/start
sso_region = eu-central-1
sso_registration_scopes = sso:account:access
 
[profile prod-read]
sso_session = acme
sso_account_id = 111122223333
sso_role_name = ReadOnlyAccess
region = eu-central-1
 
[profile dev-admin]
sso_session = acme
sso_account_id = 444455556666
sso_role_name = AdministratorAccess
region = eu-central-1

Make the read-only role your daily default; use the admin profile only when you need it.

  1. Log in once per session — one login works for all profiles of the same sso-session:
aws sso login --sso-session acme
aws s3 ls --profile prod-read
aws ec2 describe-instances --profile dev-admin --query 'Reservations[].Instances[].InstanceId'
  1. On a server without a browser use the device code — open the URL on your laptop and type the code:
aws sso login --profile prod-read --use-device-code
  1. Tools that use the same login: Terraform, kubectl (EKS), SDKs and scripts read AWS_PROFILE — no keys needed:
export AWS_PROFILE=dev-admin
terraform plan
  1. Log out at the end of the day, or on a shared machine:
aws sso logout

51.3

Check that it worked

aws sts get-caller-identity shows the right account and an AWSReservedSSO_… role (not an IAM user), and ~/.aws/credentials has no long-lived keys any more:

grep -c aws_access_key_id ~/.aws/credentials 2>/dev/null || echo "no static keys"

51.4

Common problems

ProblemCauseFix
Error loading SSO Token: Token for … does not exist / has expiredNot logged in, or the session ended (1–12 h).aws sso login --profile NAME.
The config profile (prod) could not be foundWrong name, or AWS_PROFILE points to an old profile.aws configure list-profiles; echo $AWS_PROFILE.
Browser does not open on a serverNo GUI.--use-device-code.
AccessDenied on a commandThe role (permission set) does not allow it.Use the right profile, or ask for a better permission set — not a personal access key.
Old static keys still winAWS_ACCESS_KEY_ID env variables or [default] keys are set.env | grep AWS_; remove the keys, then deactivate them in IAM.
Wrong region in the outputNo region in the profile.Add region = to the profile, or --region.

51.5

Go further

1Why is SSO better than an access key in ~/.aws/credentials?

The credentials are short-lived and come from a browser login; there is no long-lived secret on disk to leak.

2How do you log in on a server that has no browser?

aws sso login --use-device-code and finish the login on another device.

Commands in this lesson

All commands in this chapter