Google Instance ssh using `gcompute`, different user name

Viewed 1658

I am able to connect to my instance using gcloud compute ssh instance-1 --zone us-east1-b.

Once I log-in, I see this: wendy@instance-1. And when I ls, I don't see my files.

If I ssh using the online webpageenter image description here

I seethe following:

randy@instance-1. And when I ls, I do see my files.

Ok. Both are under the same google email account, and has the same Internal IP: 10.142.0.2

My question is, how do I log-in using gcloud command, into randy@instance-1?

5 Answers

You can also specify a user when using the gcloud compute ssh command:

gcloud compute ssh [USER@]INSTANCE

If you omit a user, the default one will be used, which is from the environment.

Open a command prompt / shell. The execute the following command to list the users that are configured for the CLI:

gcloud auth list

The account that is the current account has the asterisk in the left column. If that is the wrong account, or to just make sure, re-authenticate:

gcloud auth login.

Use the correct credentials to login to Google Accounts.

Now you will be able to gcloud compute ssh and be logged in with the correct user credentials.

I wrote an article on Google account configurations that goes deeper into how multiple credentials are managed by the CLI and SDK.

Understanding Gcloud Configurations

My understanding is that Google automatically creates and adds a user to the newly created VMs using your Gmail name & surname as username in the name_surname format. You can see this in google cloud console -> compute engine -> metadata -> ssh keys

Google manages the private key for this account so I'm not aware of any way of connecting to this account other than the cloud console.

However you can still access to your files after you login with gcloud command. Just do sudo su and you will become root user. Then you can copy files from /home/wendy to /home/randy.

you do have two option Fisrt is to authenticate with the right user account by running the gcloud command

gcloud auth login

And use the "randy" user account in this case, then use the gcloud command normally to ssh.

gcloud compute ssh instance-1 --zone us-east1-b

Second option is to SSH directly with the appropriate username from this documentation by running the gcloud command below.

gcloud compute ssh [USER@]INSTANCE [--zone=ZONE]

One thing, you could easily recover or copy your files by switching to the root user (you need just to run the command line "sudo su -" or "sudo -i")

If you're already logged in, and maybe don't have the option to choose a username during login (e.g. when using the iOS app), you can also use

sudo su [username]

to switch the username from an existing session.

Related