How to Fix AWS CLI 'Unable to Locate Credentials'
'Unable to locate credentials' means the AWS CLI looked for keys in a fixed list of places, in order, and found none, before sending anything to AWS. With the real AWS CLI 2.41.1 on an Ubuntu 24.04 server we show the list from its own debug log, then each common cause: never configured, keys saved under a profile the CLI doesn't read, a cron job without AWS_PROFILE, and sudo looking in root's home folder.
Twelve places, all empty
Your AWS command says Unable to locate credentials. Before it said that, the AWS CLI looked in twelve places for your keys.

Here's the error you searched for. Now the same command with debug turned on, showing only the lines where it names a place it checked. Twelve places, all empty. Usually the keys exist, and the CLI is looking under the wrong name, or in the wrong home folder. That's why a command can work when you type it, and fail at two in the morning.
aws s3 lsaws: [ERROR]: An error occurred (NoCredentials): Unable to locate credentials. You can configure credentials by running "aws login".
aws s3 ls --debug 2>&1 | grep -o 'credentials via: .*'credentials via: env credentials via: assume-role credentials via: assume-role-with-web-identity credentials via: sso credentials via: shared-credentials-file credentials via: login credentials via: custom-process credentials via: config-file credentials via: ec2-credentials-file credentials via: boto-config credentials via: container-role credentials via: iam-role
aws s3 ls --debug 2>&1 | grep -c 'credentials via'12
Where the CLI looks
Those twelve places come in four kinds. First, environment variables: settings your shell hands to every command. Then the two files in a folder called dot A W S, in your home folder. Third and fourth, only on a machine inside AWS: a role for the container, then a role for the server, permissions AWS gives the machine itself. The first place with keys wins. If none of them has keys, you get the error, and nothing is ever sent to AWS. A profile is a named set of keys in those files; unless you say otherwise, the CLI only reads the one called default. So two things decide what it finds in those files: which profile it reads, and whose home folder it's looking in.

The usual fix, and its trap
The usual fix first. Meet Ana. She runs the box-office server for a small theatre. She runs aws configure list. It shows what the CLI found, and where. Every line says not set. So she saves the theatre's keys with aws configure, under the name theatre. A key comes in two halves: an ID and a secret. One note: these are AWS's own example keys, and a local emulator stands in for S3. Stored keys are what scripts like Ana's need. If you only type commands yourself, aws login is the simpler way to sign in.
aws configure listNAME : VALUE : TYPE : LOCATION profile : <not set> : None : None access_key : <not set> : None : None secret_key : <not set> : None : None region : <not set> : None : None
aws configure --profile theatreTip: You can deliver temporary credentials to the AWS CLI using your AWS Console session by running the command 'aws login'. AWS Access Key ID [None]: AKIAIOSFODNN7EXAMPLE AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY Default region name [None]: eu-west-2 Default output format [None]: json
The keys are in the credentials file, under the name theatre. Guess first: does aws s3 ls work now? No. The same error.
cat ~/.aws/credentials[theatre] aws_access_key_id = AKIAIOSFODNN7EXAMPLE aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
aws s3 lsaws: [ERROR]: An error occurred (NoCredentials): Unable to locate credentials. You can configure credentials by running "aws login".
aws configure list says why: profile, not set. Without a name, the CLI reads the profile called default, and Ana has none. list profiles shows the one she does have. Name it with dash dash profile, and there's the theatre's bucket.
aws configure listNAME : VALUE : TYPE : LOCATION profile : <not set> : None : None access_key : <not set> : None : None secret_key : <not set> : None : None region : <not set> : None : None
aws configure list-profilestheatre
aws s3 ls --profile theatre2026-11-19 12:54:43 riverside-box-office
To stop typing that, set AWS_PROFILE. Spell it wrong, and you get a different error: the profile theater could not be found. That one names its problem. Ana adds the setting to a file called.bashrc, which every new terminal reads. She sets it in this terminal too, and configure list now says the profile came from the environment.
AWS_PROFILE=theater aws s3 lsaws: [ERROR]: The config profile (theater) could not be found
echo 'export AWS_PROFILE=theatre' >> ~/.bashrc
export AWS_PROFILE=theatre
aws configure listNAME : VALUE : TYPE : LOCATION profile : theatre : env : ['AWS_PROFILE', 'AWS_DEFAULT_PROFILE'] access_key : ****************MPLE : shared-credentials-file : secret_key : ****************EKEY : shared-credentials-file : region : eu-west-2 : config-file : ~/.aws/config
Environment variables come before the files, so a half-finished export wins too. Here an old test left something behind in this terminal: the key ID is set, but the secret isn't. The CLI stops at the environment and never reaches the profile: partial credentials found in env. Unset it, and the profile works again.
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
aws s3 lsaws: [ERROR]: Partial credentials found in env, missing: AWS_SECRET_ACCESS_KEY
unset AWS_ACCESS_KEY_ID
Works by hand, fails at 2 AM
Now the traps, where the keys exist but the CLI still can't find them. Think of looking for your house keys. You check the same pockets in the same order every time. Keys in the inside pocket while you only check the outside one: that's a profile the CLI doesn't read. Wearing someone else's coat: that's running a command as the admin, with sudo. Your own coat, but the note saying which pocket got left at home: that's a job a scheduler runs for you at night.

Now the nightly job. Ana uses cron, the Linux scheduler that runs commands at set times. Every night it runs a script that copies the ticket sales to an S3 bucket. When she runs it herself, it works. At two in the morning, from cron, the same line fails with this same error. Same server, same user, same keys. So why can't cron find them?

Here's that script: one line that copies tonight's sales file to the bucket. She runs it, and it uploads.
cat backup.sh#!/bin/bash # Riverside Theatre: copy tonight's box-office sales to the backup bucket. aws s3 cp /srv/boxoffice/sales.csv s3://riverside-box-office/sales/sales-$(date +%F).csv
./backup.shupload: ../../srv/boxoffice/sales.csv to s3://riverside-box-office/sales/sales-2026-11-19.csv
Then Ana gives it to cron, set to run a minute from now, standing in for two A M. A second line writes down every setting cron hands the job, to look at later. A minute later, the log, and in the middle, the same words: Unable to locate credentials. Upload failed.
crontab -l56 12 * * * /home/ana/backup.sh >> /home/ana/backup.log 2>&1 56 12 * * * env > /home/ana/cron-env.txt
cat backup.logException ignored in: <s3transfer.crt.BotocoreCRTCredentialsWrapper object at 0xffff8ca7d010> Traceback (most recent call last): File "awscli/s3transfer/crt.py", line 1194, in __call__ File "awscli/s3transfer/crt.py", line 1204, in _get_credentials botocore.exceptions.NoCredentialsError: Unable to locate credentials upload failed: ../../srv/boxoffice/sales.csv to s3://riverside-box-office/sales/sales-2026-11-19.csv AWS_AUTH_CREDENTIALS_PROVIDER_DELEGATE_FAILURE: Valid credentials could not be sourced by the delegate provider
Cron gives the job a tiny environment. HOME is there, so the files would be found. AWS_PROFILE isn't. Ana set it in.bashrc, and cron never reads.bashrc. So the job asks for the default profile, which doesn't exist.
grep -E '^(HOME|AWS_)' cron-env.txtHOME=/home/ana
grep -n AWS_PROFILE ~/.bashrc118:export AWS_PROFILE=theatre
The fix goes where the job runs. A line at the top of the cron tab sets AWS_PROFILE for every job below it.

Next minute, the log ends with an upload, and the sales file is in the bucket.
crontab -lAWS_PROFILE=theatre 57 12 * * * /home/ana/backup.sh >> /home/ana/backup.log 2>&1
tail -n 1 backup.logupload: ../../srv/boxoffice/sales.csv to s3://riverside-box-office/sales/sales-2026-11-19.csv
aws s3 ls s3://riverside-box-office/sales/2026-11-19 12:57:02 104 sales-2026-11-19.csv
Fails with sudo
One more copy: the ticket system's own log, which only root, the machine's admin account, can read. The CLI skips it: not readable. So Ana puts sudo in front, and the keys are gone again.
aws s3 cp /var/log/boxoffice.log s3://riverside-box-office/logs/warning: Skipping file /var/log/boxoffice.log. File/Directory is not readable.
sudo aws s3 cp /var/log/boxoffice.log s3://riverside-box-office/logs/Exception ignored in: <s3transfer.crt.BotocoreCRTCredentialsWrapper object at 0xffff7d6cd010> Traceback (most recent call last): File "awscli/s3transfer/crt.py", line 1194, in __call__ File "awscli/s3transfer/crt.py", line 1204, in _get_credentials botocore.exceptions.NoCredentialsError: Unable to locate credentials upload failed: ../../var/log/boxoffice.log to s3://riverside-box-office/logs/boxoffice.log AWS_AUTH_CREDENTIALS_PROVIDER_DELEGATE_FAILURE: Valid credentials could not be sourced by the delegate provider
sudo runs the command as root, with root's home folder and none of Ana's variables. Someone else's coat. Here's the fix: use sudo just to read the file, and let Ana's own CLI do the upload. The vertical bar is a pipe; it passes the file along. The dash tells aws s3 cp to take its input from that pipe. And the log is in the bucket.
sudo env | grep -E '^(HOME|AWS_)'HOME=/root
sudo cat /var/log/boxoffice.log | aws s3 cp - s3://riverside-box-office/logs/boxoffice.log
aws s3 ls s3://riverside-box-office/logs/2026-11-19 12:57:10 118 boxoffice.log
On a server in AWS
Keys saved on a server are a risk: anyone who gets into the server gets the keys. Inside AWS, you skip them. Attach a role to the server. The CLI's last stop is the metadata service, a special address inside the server. It hands out short-lived keys that change by themselves. Ana's server isn't in AWS, so when the CLI tries that address, nothing answers.

Before you trust a script
A backup that fails at two in the morning can go unnoticed until the day you need it. So add two lines to the top of a script like Ana's. First, set dash e, which stops the script at the first command that goes wrong. Then a command that asks AWS who it is: aws sts get caller identity prints the account and the user its keys belong to. Our emulator says all zeros; real AWS shows your own. With no keys, that line fails, and the script stops before it copies anything.
aws sts get-caller-identity{ "UserId": "000000000000", "Account": "000000000000", "Arn": "arn:aws:iam::000000000000:root" }
Recap
Never set up? Run aws configure. Keys saved under a profile name? Set AWS_PROFILE, and in cron, set it in the cron tab. Partial credentials? Unset the stray key. With sudo, keep the CLI running as you. On AWS, use a role.

If your backups are landing in the bucket again, subscribing brings more fixes, shown on real machines.
Every error in this video came from the real AWS CLI, and every command and the lab files are linked in the description. Thanks for watching Cloud Explainers.
Sources
- AWS CLI User Guide, Authentication and access credentials (the order the CLI looks for credentials)
- AWS CLI User Guide, Configuration and credential file settings (named profiles, [default])
- AWS CLI User Guide, Environment variables (AWS_PROFILE)
- AWS CLI User Guide, Console credentials (aws login)
- Amazon EC2 User Guide, IAM roles for Amazon EC2 (instance profiles, credentials from instance metadata)
- crontab(5) (HOME and SHELL from /etc/passwd; NAME=value lines)
- sudoers(5) (env_reset; HOME of the target user)
Demos: a container on our own Mac standing in for a theatre's box-office server, with Floci, a local AWS emulator, standing in for S3; the theatre is made up and the keys are AWS's own documentation examples. Every error was the real AWS CLI's. Watch the full video on YouTube.