Sub topic 6
Authorizing the Dev Hub from the CLI
This chapter covers how the Salesforce CLI (sf) gets permission to act on your Dev Hub org. You will log in through the browser, set the org as the default Dev Hub, check the stored credentials, and learn the failure modes. It assumes you have enabled Dev Hub in Setup and that sf --version runs in your terminal.
What authorization does
Enabling Dev Hub in Setup changes the org. Authorizing it from the CLI changes your machine. The CLI cannot create a scratch org until it holds a valid credential for the Dev Hub, because every sf org create scratch call is sent to the Dev Hub, which then provisions the new org.
Authorization uses OAuth 2.0. The CLI opens a browser, you sign in to the org, and you approve access for the Salesforce CLI connected app. Salesforce returns a refresh token, which the CLI stores locally. It exchanges that token for short-lived access tokens each time you run a command, so you do not retype a password.
The CLI stores the result as an authorized org, identified by the username you logged in with. An alias is a short name you choose that points to that username.
Logging in to the Dev Hub
Run the login command with two flags that matter for a Dev Hub.
sf --version
sf org login web --set-default-dev-hub --alias my-hubThe first line confirms the CLI is installed. The second does the work:
--set-default-dev-hubmarks this org as your default Dev Hub. Later commands such assf org create scratchuse it when you do not name a Dev Hub.--alias my-hubgives the org a name you can type instead of the username.
A browser window opens. Sign in with a user in the Dev Hub org, then click Allow on the approval screen. The terminal prints a success message that includes the username.
If you work with a sandbox or a My Domain login, add --instance-url. For example, use --instance-url https://mycompany.my.salesforce.com. For a Developer Edition org or production, the default login URL is usually enough.
Default Dev Hub versus default org
The CLI keeps two separate defaults, and mixing them up is a common source of confusion.
| Setting | Set by | Used by |
|---|---|---|
| Default Dev Hub | `--set-default-dev-hub` on login | Commands that create scratch orgs, snapshots, and similar Dev Hub operations |
| Default target org | `--set-default` on login or scratch org creation | Commands that work in an org, such as deploy, retrieve, and open |
The Dev Hub is the org that creates scratch orgs. The target org is the org you are working in. After you create a scratch org with --set-default, the target org becomes the scratch org, and the Dev Hub default stays my-hub. That is the setup you want.
You can see both defaults with the config commands.
sf config list
sf config get target-dev-hubYou can also set the Dev Hub default later, without logging in again.
sf config set target-dev-hub=my-hubAdd --global to store the setting for all projects. Without it, the value is saved in the current project's .sf/config.json, so it only applies when you run commands inside that project.
Verifying the authorization
After login, confirm that the CLI sees the org and that the credentials work.
sf org list
sf org display --target-org my-hubsf org list shows every org the CLI knows about. The Dev Hub appears in the non-scratch section, and the default Dev Hub is marked in the first column. sf org display prints the username, org ID, instance URL, and API version. It also prints an access token, so treat that output as sensitive and keep it out of logs and screenshots.
For a stronger test, run a command that hits the Dev Hub API.
sf limits api display --target-org my-hubIf this returns a table of limits, the token works and the Dev Hub is reachable. A later chapter explains how to read the scratch org entries in that table.
Where credentials are stored
The CLI writes authorization files to a .sfdx directory in your home folder. Each org has a JSON file named after its username. The refresh token inside is encrypted, but treat the whole directory as secret. Do not commit it, copy it into a shared drive, or paste its contents into a ticket.
Aliases live in ~/.sfdx/alias.json (or the equivalent location for your CLI version). You can add or change one without logging in again.
sf alias set [email protected]
sf alias listReplace [email protected] with the username shown by sf org list. Aliases are only local labels. Two developers can use different aliases for the same org, and removing an alias does not log you out.
Logging out and re-authorizing
To remove the stored credentials for an org, log out.
sf org logout --target-org my-hubThis deletes the local authorization. It does not disable the Dev Hub or delete any scratch orgs it created. Scratch orgs that were created from this Dev Hub also remain in the Dev Hub's records, so you can authorize again and continue to see them in the Active Scratch Orgs list in Setup.
To switch to another Dev Hub, log in again with --set-default-dev-hub and a different alias. The new login replaces the default. Authorizing the same username a second time updates the stored credentials, so it is the standard fix when a token has expired or been revoked.
Common failures
| Symptom | Likely cause | Fix |
|---|---|---|
| Browser shows `redirect_uri_mismatch` or never returns to the terminal | Local callback port blocked or in use | Free port 1717, or check firewall and VPN rules, then retry |
| Login succeeds but scratch org creation says the org is not a Dev Hub | You authorized an org where Dev Hub is not enabled | Enable Dev Hub in Setup in that org, then run the login again |
| `No default dev hub found` | Login ran without `--set-default-dev-hub` | Run `sf config set target-dev-hub=my-hub` |
| `expired access/refresh token` | Token revoked, password reset, or session policy expired it | Log in again with the same alias |
| `user is not authorized` or the login screen rejects approval | The user's profile blocks the connected app | Ask an administrator to grant access to the Salesforce CLI connected app |
| Login to the wrong org | The browser is signed in to a different org | Sign out in the browser or use a private window, then rerun login |
A useful habit is to check which org the browser is signed in to before you approve. The CLI authorizes whatever account you approve, not the one you intended.
Authorizing without a browser
The browser flow needs a person at a desktop. Automation servers have neither. A later chapter covers non-interactive authorization for CI in detail. In short, you use the JWT flow, where a certificate and a connected app replace the interactive approval, or you log in with a stored auth URL. Both authorize the same Dev Hub and produce the same kind of stored credential. Do not try to run sf org login web in a headless job.
Putting it together
A clean authorization sequence looks like this:
- Run
sf --versionto confirm the CLI works. - Run
sf org login web --set-default-dev-hub --alias my-hub. - Approve access in the browser, in the Dev Hub org.
- Run
sf org listand confirmmy-hubis present and marked as the default Dev Hub. - Run
sf limits api display --target-org my-hubto confirm the credential works against the API.
When all five steps succeed, the next chapters can create a project and a scratch org without further setup.
Exercise
You ran sf org login web --alias my-hub and approved access. Later, sf org create scratch fails with "No default dev hub found", even though sf org list shows my-hub. What went wrong, and what single command fixes it without logging in again?
Show answer
The login did not include --set-default-dev-hub, so no default Dev Hub was recorded. The org is authorized but not marked as the default. Run sf config set target-dev-hub=my-hub (add --global to apply it to all projects). Alternatively, pass --target-dev-hub my-hub on the create command.
Exercise
A teammate logs out with sf org logout --target-org my-hub. Does this delete the scratch orgs the Dev Hub created? How do they get back to work?
Show answer
No. Logging out removes only the local credentials. The Dev Hub and its scratch orgs are unchanged. The teammate runs sf org login web --set-default-dev-hub --alias my-hub again and approves access in the browser.