Post

Login to Rancher

How to authenticate to a Rancher server via the API and log in using the Rancher CLI.

The following script authenticates against the Rancher local provider API to obtain an API token, then uses that token to log in via the Rancher CLI. Replace the URL and credentials with your actual Rancher server details.

1
2
3
4
5
6
7
8
NAME="rancher.web.ui"
RANCHER_URL="https://$NAME:10443"

APITOKEN=$(curl -sk "${RANCHER_URL}/v3-public/localProviders/local?action=login" \
-H "content-type: application/json" \
--data-binary "{\"username\":\"admin\",\"password\":\"admin\"}" 2>/dev/null | jq -r .token 2>/dev/null)

rancher login -t "${APITOKEN}" "${RANCHER_URL}/v3"
This post is licensed under CC BY 4.0 by the author.