Authenticating with the Yandex Cloud CLI on a server with no GUI
Warning
To get authenticated in the CLI with a Yandex account, federated account, or local user account, you need a browser and a GUI.
If using the CLI on a server without a GUI, the recommended option is to authenticate with a service account.
If you cannot use a service account, authenticate via an SSH tunnel or X11 forwarding.
This subsection discusses the following methods for authentication on a server with no GUI:
Authentication via X11 forwarding
This example describes X11 forwarding
-
If connecting to a VM from macOS or Windows, install and run an X server, e.g., XQuartz
for macOS or VcXsrv for Windows. Linux has this feature available by default. -
Connect to the server over SSH:
ssh -i <key_file> <username>@<server_public_IP_address> -
Open
/etc/ssh/sshd_config:sudo nano /etc/ssh/sshd_config -
Uncomment these lines:
X11Forwarding yes X11DisplayOffset 10 X11UseLocalhost yes -
Install the X server packages:
sudo apt get update sudo apt-get install xauth xorg -
Disconnect from the server and reconnect to your X server using the
-Xflag:ssh -X -i <key_file> <username>@<server_public_IP_address> -
Optionally, make sure X11 forwarding works correctly by running this command:
xeyesAn Xeyes window will open; close it.
-
Install the Chromium
browser and make sure you can start it:sudo apt update && sudo apt install chromium-browser export XAUTHORITY=$HOME/.Xauthority chromiumA browser window will open; close it.
-
Install the CLI and an X11 integration package:
curl -sSL https://storage.yandexcloud.net/yandexcloud-yc/install.sh | bash exec -l $SHELL sudo apt install xdg-utils -
Start the authentication process:
Yandex accountFederated accountLocal user accountyc init --username=<email_address>For more information, see Authentication in the Yandex Cloud CLI with a Yandex account.
yc init --federation-id=<federation_ID>For more information, see Authenticating with the Yandex Cloud CLI as a federated user.
yc init --userpool-id=<user_pool_ID>For more information, see Authenticating in the Yandex Cloud CLI as a local user.
A browser window with the authentication page will now open.
-
Complete the CLI configuration.
Example of authenticating over an SSH tunnel
To authenticate to a Linux VM, follow these steps:
-
Connect to the VM over SSH.
-
On your VM, create a file named
/usr/local/bin/xdg-openwith the following contents:#!/bin/sh echo $* > /dev/tty -
Assign permissions to execute the file:
sudo chmod +x /usr/local/bin/xdg-open -
Install the CLI:
curl -sSL https://storage.yandexcloud.net/yandexcloud-yc/install.sh | bash exec -l $SHELL -
Start the authentication process:
Yandex accountFederated accountLocal user accountyc init --username=<email_address>For more information, see Authentication in the Yandex Cloud CLI with a Yandex account.
yc init --federation-id=<federation_ID>For more information, see Authenticating with the Yandex Cloud CLI as a federated user.
yc init --userpool-id=<user_pool_ID>For more information, see Authenticating in the Yandex Cloud CLI as a local user.
-
Select the profile you want to set up authentication for or create a new one:
Welcome! This command will take you through the configuration process. Pick desired action: [1] Re-initialize this profile 'default' with new settings [2] Create a new profile -
The CLI prompts you to continue authentication in the browser. Press Enter to continue:
Yandex accountFederated accountLocal user accountYou are going to be authenticated via username '<email_address>'. Authentication web site will be opened. After your successful authentication, you will be redirected to 'https://console.yandex.cloud'. Press 'enter' to continue...You are going to be authenticated via federation-id 'aje1f0hsgds3a********'. Your federation authentication web site will be opened. After your successful authentication, you will be redirected to 'https://console.yandex.cloud'. Press 'enter' to continue...You are going to be authenticated via userpool-id 'ek0auknfc0mh********'. Your userpool authentication web site will be opened. After your successful authentication, you will be redirected to cloud console. Press 'enter' to continue... -
Once you press Enter, you will get a URL that looks like this:
https://auth.yandex.cloud/oauth/authorize?client_id=yc.oauth.public-sdk&code_challenge=y22kspX4VrKLmdg9hGr_Bwgte_a3RXtw1En********&code_challenge_method=S256&redirect_uri=http%3A%2F%2F127.0.0.1%3A42121%2Fauth%2Fcallback&response_type=code&scope=openid&state=aExf0z********&yc_federation_hint=federation-idSave this URL. You will need it for browser authentication. You will also need the port which you can find in the
redirect_uriquery parameter following the127.0.0.1IP address. In our example, it is42121. -
On your local computer, open a new terminal window and run the command to set up an SSH tunnel, specifying the port obtained in the previous step, username, and VM IP address:
ssh -N -L <port>:127.0.0.1:<port> <username>@<VM_IP_address>After the connection is established, the command returns no result and remains active for as long as the SSH tunnel is operating. Do not close this terminal window until the CLI setup is complete.
-
Use your local computer's browser to open the authentication URL you got earlier.
-
Return to the VM SSH session terminal window where the
yc initcommand is active. If authentication is successful, the CLI will go to the next profile configuration step. -
Complete the CLI configuration.
-
In the local terminal window, terminate the SSH tunnel by pressing Ctrl + C.