Usage
Description
Thehc auth login command authenticates you with Harness services by validating your credentials and storing them locally for future use. The command supports both interactive and non-interactive modes, making it suitable for both manual use and automation.
When you successfully log in, your credentials are saved to ~/.harness/auth.json with restricted permissions (0600) and validated against the Harness API.
Authentication Flow
- Credential Collection - Gather API URL, token, and account information
- Token Parsing - Extract account ID from token (format:
pat.{AccountID}.{Random}.{Random}) - Validation - Verify credentials by calling the Harness API
- Storage - Save authentication configuration to disk
- Session Setup - Configure global settings for the current CLI session
Flags
Harness API URL. Use this flag to connect to different Harness environments or self-hosted instances.Example values:
https://app.harness.io(SaaS)https://app3.harness.io(SaaS EU)https://your-domain.harness.io(Custom domain)
Your Harness API authentication token. This should be a Personal Access Token (PAT) with appropriate permissions.Token format:
pat.{AccountID}.{Random}.{Random}Harness Account ID. If not provided in non-interactive mode, the CLI automatically extracts it from your API token.The account ID is typically a string like
abc123def456.Organization ID for scoping your CLI session. When set, commands will default to this organization context.This is optional and can be useful when you primarily work within a specific organization.
Project ID for scoping your CLI session. When set, commands will default to this project context.This is optional and can be useful when you primarily work within a specific project.
Disable interactive prompts. When enabled, all required values must be provided via flags.This is essential for automation, CI/CD pipelines, and scripts where user input is not possible.
Interactive Mode
When you runhc auth login without required flags, the command enters interactive mode:
Interactive Prompts
- API URL - Press Enter to use the default (
https://app.harness.io) or enter a custom URL - API Token - Enter your token (input is hidden for security)
- Account ID - Automatically extracted and displayed
- Organization ID - Optional, press Enter to skip
- Project ID - Optional, press Enter to skip
Non-Interactive Mode
For automation and CI/CD environments, use the--non-interactive flag:
Environment Variables
For enhanced security in automation, use environment variables:Examples
First-Time Interactive Login
Quick Login with Token Only
CI/CD Pipeline (GitHub Actions)
.github/workflows/deploy.yml
Docker Container
Dockerfile
entrypoint.sh
Switching Between Accounts
Validation Process
The login command validates your credentials by:- Making an authenticated GET request to
/ng/api/accounts/{accountID} - Verifying the response status is 200 OK
- Parsing the account information from the response
Stored Configuration
After successful login, credentials are saved to~/.harness/auth.json:
The file is created with permissions
0600 (readable and writable only by the owner) for security.Error Handling
Missing Required Fields
Invalid Credentials
Network Issues
Token Format Issues
Best Practices
- Use Environment Variables - Store tokens in environment variables, not in scripts
- Scope Your Session - Use
--organd--projectflags to set default contexts - Validate After Login - Run
hc auth statusto confirm successful authentication - Rotate Tokens Regularly - Update your API tokens periodically for security
- Use Non-Interactive in CI/CD - Always use
--non-interactiveflag in automation - Keep Tokens Secret - Never log or print your API token
- One Account Per Session - Logout before switching accounts
Security Considerations
- File permissions are automatically set to
0600 - Configuration directory (
~/.harness) is created with0755permissions - API tokens are transmitted only over HTTPS
- Credentials are validated before being saved
- Password input is hidden in interactive mode
Related Commands
- hc auth status - Check authentication status
- hc auth logout - Remove saved credentials
Troubleshooting
Can’t Create Config Directory
If you see “Error creating config directory”:Timeout Errors
Validation requests timeout after 10 seconds. If you experience timeouts:- Check your network connection
- Verify firewall rules allow HTTPS to Harness
- Try using a different API URL if using a custom instance
API URL Issues
Ensure your API URL:- Uses HTTPS protocol
- Does not include trailing slashes
- Points to the correct Harness environment
- Includes the full domain name