Skip to content

Commit 7c69bbd

Browse files
sunbryeCopilot
andauthored
Copilot CLI: Authentication section (#59753)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 4d23eb6 commit 7c69bbd

File tree

6 files changed

+380
-6
lines changed

6 files changed

+380
-6
lines changed

content/apps/oauth-apps/using-oauth-apps/privileged-oauth-apps.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ These {% data variables.product.prodname_oauth_apps %} are :
2323
* {% data variables.product.prodname_android %}
2424
* {% data variables.product.prodname_cli %}
2525
* {% data variables.product.prodname_github_codespaces %} for JetBrains
26+
* {% data variables.copilot.copilot_cli %}
2627
* {% data variables.product.prodname_desktop %}
2728
* {% data variables.product.prodname_education %}
2829
* github-importer-production <!-- markdownlint-disable-line GHD034 -->
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
---
2+
title: Authenticating GitHub Copilot CLI
3+
shortTitle: Authenticate Copilot CLI
4+
intro: Authenticate {% data variables.copilot.copilot_cli_short %} so that you can use {% data variables.product.prodname_copilot_short %} directly from the command line.
5+
product: '{% data reusables.gated-features.copilot-cli %}'
6+
versions:
7+
feature: copilot
8+
topics:
9+
- Copilot
10+
- CLI
11+
contentType: how-tos
12+
category:
13+
- Configure Copilot CLI
14+
---
15+
16+
## About authentication
17+
18+
{% data variables.copilot.copilot_cli %} supports three authentication methods. The method you use depends on whether you are working interactively or in an automated environment.
19+
20+
* **OAuth device flow**: The default and recommended method for interactive use. When you run `/login` in {% data variables.copilot.copilot_cli_short %}, the CLI generates a one-time code and directs you to authenticate in your browser. This is the simplest way to authenticate.
21+
* **Environment variables**: Recommended for CI/CD pipelines, containers, and non-interactive environments. You set a supported token as an environment variable (`COPILOT_GITHUB_TOKEN`, `GH_TOKEN`, or `GITHUB_TOKEN`), and the CLI uses it automatically without prompting.
22+
* **{% data variables.product.prodname_cli %} fallback**: If you have {% data variables.product.prodname_cli %} (`gh`) (note: the `gh` CLI, not `copilot`) installed and authenticated, {% data variables.copilot.copilot_cli_short %} can use its token automatically. This is the lowest priority method and activates only when no other credentials are found.
23+
24+
Once authenticated, {% data variables.copilot.copilot_cli_short %} remembers your login and automatically uses the token for all {% data variables.product.prodname_copilot_short %} API requests. You can log in with multiple accounts, and the CLI will remember the last-used account. Token lifetime and expiration depend on how the token was created on your account or organization settings.
25+
26+
### Supported token types
27+
28+
| Token type | Prefix | Supported | Notes |
29+
|----------------------------|---------------|-----------|--------------------------------------------------------|
30+
| OAuth token (device flow) | `gho_` | Yes | Default method via `copilot login` |
31+
| Fine-grained PAT | `github_pat_` | Yes | Must include required permissions **Copilot Requests** |
32+
| GitHub App user-to-server | `ghu_` | Yes | Via environment variable |
33+
| Classic PAT | `ghp_` | No | Not supported by {% data variables.copilot.copilot_cli_short %} |
34+
35+
### How Copilot CLI stores credentials
36+
37+
By default, the CLI stores your OAuth token in your operating system's keychain under the service name `copilot-cli`:
38+
39+
| Platform | Keychain |
40+
|---|---|
41+
| macOS | Keychain Access |
42+
| Windows | Credential Manager |
43+
| Linux | libsecret (GNOME Keyring, KWallet) |
44+
45+
If the system keychain is unavailable—for example, on a headless Linux server without `libsecret` installed—the CLI prompts you to store the token in a plaintext configuration file at `~/.copilot/config.json`.
46+
47+
When you run a command, {% data variables.copilot.copilot_cli_short %} checks for credentials in the following order:
48+
49+
1. `COPILOT_GITHUB_TOKEN` environment variable
50+
1. `GH_TOKEN` environment variable
51+
1. `GITHUB_TOKEN` environment variable
52+
1. OAuth token from the system keychain
53+
1. GitHub CLI (`gh auth token`) fallback
54+
55+
> [!NOTE]
56+
> An environment variable silently overrides a stored OAuth token. If you set `GH_TOKEN` for another tool, the CLI uses that token instead of the OAuth token from `copilot login`. To avoid unexpected behavior, unset environment variables you do not intend the CLI to use.
57+
58+
## Authenticating with OAuth
59+
60+
The OAuth device flow is the default authentication method for interactive use. You can authenticate by running `/login` from {% data variables.copilot.copilot_cli_short %} or `copilot login` from your terminal.
61+
62+
### Authenticate with `/login`
63+
64+
1. From {% data variables.copilot.copilot_cli_short %}, run `/login`.
65+
66+
```bash copy
67+
/login
68+
```
69+
70+
1. Select the account you want to authenticate with. For {% data variables.product.prodname_ghe_cloud %} with data residency, enter the hostname of your instance
71+
72+
```text
73+
What account do you want to log into?
74+
1. {% data variables.product.prodname_dotcom_the_website %}
75+
2. {% data variables.product.prodname_ghe_cloud %} with data residency (*.ghe.com)
76+
```
77+
78+
1. The CLI displays a one-time user code and automatically copies it to your clipboard and opens your browser.
79+
80+
```text
81+
Waiting for authorization...
82+
Enter one-time code: 1234-5678 at https://github.com/login/device
83+
Press any key to copy to clipboard and open browser...
84+
```
85+
86+
1. Navigate to the verification URL at `https://github.com/login/device` if your browser did not open automatically.
87+
1. Paste the one-time code in the field on the page.
88+
1. If your organization uses SAML SSO, click **Authorize** next to each organization you want to grant access to.
89+
1. Review the requested permissions and click **Authorize GitHub Copilot CLI**.
90+
1. Return to your terminal. The CLI displays a success message when authentication is complete.
91+
92+
```text
93+
Signed in successfully as Octocat. You can now use {% data variables.product.prodname_copilot_short %}.
94+
```
95+
96+
### Authenticate with `copilot login`
97+
98+
1. From the terminal, run `copilot login`. If you are using {% data variables.product.prodname_ghe_cloud %} with data residency, pass the hostname of your instance.
99+
100+
```bash copy
101+
copilot login
102+
```
103+
104+
For {% data variables.product.prodname_ghe_cloud %}:
105+
106+
```bash copy
107+
copilot login --host HOSTNAME
108+
```
109+
110+
The CLI displays a one-time user code and automatically copies it to your clipboard and opens your browser.
111+
112+
```text
113+
To authenticate, visit https://github.com/login/device and enter code 1234-5678.
114+
```
115+
116+
1. Navigate to the verification URL at `https://github.com/login/device` if your browser did not open automatically.
117+
1. Paste the one-time code in the field on the page.
118+
1. If your organization uses SAML SSO, click **Authorize** next to each organization you want to grant access to.
119+
1. Review the requested permissions and click **Authorize GitHub Copilot CLI**.
120+
1. Return to your terminal. The CLI displays a success message when authentication is complete.
121+
122+
```text
123+
Signed in successfully as Octocat.
124+
```
125+
126+
## Authenticating with environment variables
127+
128+
For non-interactive environments, you can authenticate by setting an environment variable with a supported token. This is ideal for CI/CD pipelines, containers, or headless servers.
129+
130+
{% data reusables.copilot.copilot-cli-pat-steps %}
131+
132+
## Authenticating with {% data variables.product.prodname_cli %}
133+
134+
If you have {% data variables.product.prodname_cli %} installed and authenticated, {% data variables.copilot.copilot_cli_short %} can use its token as a fallback. This method has the lowest priority and activates only when no environment variables are set and no stored token is found.
135+
136+
1. Verify that {% data variables.product.prodname_cli %} is authenticated.
137+
138+
```bash copy
139+
gh auth status
140+
```
141+
142+
If you use {% data variables.product.prodname_ghe_cloud %} with data residency, verify the correct hostname is authenticated.
143+
144+
```bash copy
145+
gh auth status --hostname HOSTNAME
146+
```
147+
148+
1. Run `copilot`. The Copilot CLI uses the {% data variables.product.prodname_cli %} token automatically.
149+
1. Run `/user` to verify your authenticated account in the CLI.
150+
151+
## Switching between accounts
152+
153+
{% data variables.copilot.copilot_cli_short %} supports multiple accounts. You can list available accounts and switch between them from within the CLI.
154+
To list available accounts, run `/user list` from the {% data variables.copilot.copilot_cli_short %} prompt.
155+
To switch to a different account, type `/user switch` on the prompt.
156+
157+
To add another account, run `copilot login` from a new terminal session, or run the login command from within the CLI and authorize with the other account.
158+
159+
## Signing out and removing credentials
160+
161+
To sign out, type `/logout` at the {% data variables.copilot.copilot_cli_short %} prompt. This removes the locally stored token but does not revoke it on {% data variables.product.github %}.
162+
163+
To revoke the OAuth app authorization on {% data variables.product.github %} and prevent it from being used elsewhere, follow these steps.
164+
165+
1. Navigate to **Settings** > **Applications** > **Authorized OAuth Apps**.
166+
1. Navigate to your settings page:
167+
1. In the upper-right corner of any page on {% data variables.product.prodname_dotcom %}, click your profile picture.
168+
1. Click **Settings**.
169+
1. In the left sidebar, click **Applications**.
170+
1. Under **Authorized OAuth Apps**, click {% octicon "kebab-horizontal" aria-label="The horizontal kebab icon" %} next to **GitHub CLI** to expand the menu and select **Revoke**.

content/copilot/how-tos/copilot-cli/set-up-copilot-cli/index.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ versions:
66
feature: copilot
77
children:
88
- /install-copilot-cli
9+
- /authenticate-copilot-cli
910
- /configure-copilot-cli
11+
- /troubleshoot-copilot-cli-auth
12+
1013
contentType: how-tos
11-
---
14+
---

content/copilot/how-tos/copilot-cli/set-up-copilot-cli/install-copilot-cli.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,13 @@ Download the executable for your platform, unpack it, and run.
116116

117117
## Authenticating with {% data variables.copilot.copilot_cli_short %}
118118

119-
On first launch, if you're not currently logged in to {% data variables.product.github %}, you'll be prompted to use the `/login` slash command. Enter this command and follow the on-screen instructions to authenticate.
119+
On first launch, if you're not currently logged in to {% data variables.product.github %}, you'll be prompted to use the `/login` slash command. Enter this command and follow the on-screen instructions to authenticate. For more information on the authentication process, see [AUTOTITLE](/copilot/how-tos/copilot-cli/set-up-copilot-cli/authenticate-copilot-cli).
120120

121121
### Authenticating with a {% data variables.product.pat_generic %}
122122

123123
You can also authenticate using a {% data variables.product.pat_v2 %} with the "{% data variables.product.prodname_copilot_short %} Requests" permission enabled.
124124

125-
1. Visit [{% data variables.product.pat_v2_caps_plural %}](https://github.com/settings/personal-access-tokens/new).
126-
1. Under "Permissions," click **Add permissions** and select **{% data variables.product.prodname_copilot_short %} Requests**.
127-
1. Click **Generate token**.
128-
1. Add the token to your environment using the `GH_TOKEN` or `GITHUB_TOKEN` environment variable (in order of precedence).
125+
{% data reusables.copilot.copilot-cli-pat-steps %}
129126

130127
## Next steps
131128

0 commit comments

Comments
 (0)