Configure AccessKey environment variables on Linux, macOS, and Windows

更新时间:
复制 MD 格式

Store your Alibaba Cloud AccessKey pair in operating system environment variables to avoid hardcoding credentials. This topic covers Linux, macOS, and Windows.

Alibaba Cloud accounts have full resource access and pose a high security risk. We recommend that you create and use a Resource Access Management (RAM) user for resource access and daily O&M. Assign only the required permissions to the RAM user before calling API operations. To obtain AccessKey credentials, Create an AccessKey and Manage permissions for a RAM user. Never hardcode plaintext AccessKeys in your code. Use environment variables or other secure methods instead.

Configure on Linux and macOS

The following examples use ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. Replace these as needed, such as with OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.

Set environment variables with the export command:

Important

Variables set with export are temporary and valid only for the current session. To persist them, add the export command to your shell startup configuration file.

  • Configure the AccessKey ID and press Enter.

    # Replace yourAccessKeyID with your AccessKey ID.
    export ALIBABA_CLOUD_ACCESS_KEY_ID=yourAccessKeyID
  • Configure the AccessKey secret and press Enter.

    # Replace yourAccessKeySecret with your AccessKey secret.
    export ALIBABA_CLOUD_ACCESS_KEY_SECRET=yourAccessKeySecret
  • Verify the configuration.

    Run echo $ALIBABA_CLOUD_ACCESS_KEY_ID. If the correct AccessKey ID is returned, the configuration succeeded.

Configure on Windows

The following examples use ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. Replace these as needed, such as with OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.

Use GUI

  • Procedure

    To configure environment variables using the Windows 10 GUI:

    Right-click This PC and select Properties. Click Advanced system settings. On the Advanced tab, click Environment Variables. Click New in the User variables or System variables section, and configure the variables in the following table.

    Variable

    Example

    AccessKey ID

    • Variable name: ALIBABA_CLOUD_ACCESS_KEY_ID

    • Variable value: LTAI****************

    AccessKey Secret

    • Variable name: ALIBABA_CLOUD_ACCESS_KEY_SECRET

    • Variable value: yourAccessKeySecret

  • Verify the configuration.

    Press Win+R , enter cmd , and press Enter to open a command prompt. Run echo %ALIBABA_CLOUD_ACCESS_KEY_ID% and echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET%. If the correct values are returned, the configuration succeeded.

Use CMD

  • Procedure

    Open a command prompt as an administrator and run the following commands:

    setx ALIBABA_CLOUD_ACCESS_KEY_ID yourAccessKeyID /M
    setx ALIBABA_CLOUD_ACCESS_KEY_SECRET XXXXXXX /M

    /M sets a system variable. Omit it to set a user variable.

  • Verify the configuration.

    Open a new command prompt. Run echo %ALIBABA_CLOUD_ACCESS_KEY_ID% and echo %ALIBABA_CLOUD_ACCESS_KEY_SECRET%. If the correct values are returned, the configuration succeeded.

Use Windows PowerShell

Set environment variables for the current user. The changes apply to all new sessions.

[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'yourAccessKeyID', [System.EnvironmentVariableTarget]::User)
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'XXXXXXX', [System.EnvironmentVariableTarget]::User)

Set environment variables for all users. Run the following commands as an administrator.

[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_ID', 'yourAccessKeyID', [System.EnvironmentVariableTarget]::Machine)
[System.Environment]::SetEnvironmentVariable('ALIBABA_CLOUD_ACCESS_KEY_SECRET', 'XXXXXXX', [System.EnvironmentVariableTarget]::Machine)

Set temporary environment variables for the current session:

$env:ALIBABA_CLOUD_ACCESS_KEY_ID = "yourAccessKeyID"
$env:ALIBABA_CLOUD_ACCESS_KEY_SECRET = "XXXXXXX"

Run Get-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_ID and Get-ChildItem env:ALIBABA_CLOUD_ACCESS_KEY_SECRET in PowerShell. If the correct values are returned, the configuration succeeded.

Important

After configuring the environment variables, restart your IDE and related services to ensure the new settings take effect.