How to connect to GCP VM instance with password using SSH?
Asked Answered
L

2

7

I able to connect to my VM instance in GCP with a SSH key. I added user with the sudo useradd -m -s /bin/bash -G {groups} {new user name} command, and changed the password with th passwd {new user name} command.

I tried to connect to the VM instance with SSH but i received this error: Permission denied (publickey).

Please help me.

Linnea answered 30/12, 2019 at 16:4 Comment(2)
It's possible that sshd on your cloud instance is configured to prohibit password authentication.Porpoise
Here is a recipe to enable SSH password authentication ... serverpilot.io/docs/how-to-enable-ssh-password-authenticationLithuanian
L
31

By default, SSH login using a password (as opposed to keys) is disabled on newly created Linux Compute Engines. Fortunately, it can be enabled pretty quickly.

Login to the Linux environment and then edit the text file found at:

/etc/ssh/sshd_config

Look for the line which reads:

PasswordAuthentication no

and change it to

PasswordAuthentication yes

Save the file.

Finally, restart SSH using:

  1. If your OS is Ubuntu/Debian:
sudo service ssh restart
  1. If your OS is CentOS/RedHat:
sudo service sshd restart

At this point, you will now be able to login using SSH using a userid/password pair.
To set the password for $USER, do:

sudo passwd $USER

References:

Lithuanian answered 30/12, 2019 at 16:24 Comment(1)
Remember. If Google has deactivated by default the login/password authentication it's because it's weaker than SSH keys! Use with caution.Penick
G
2

As of today(16th August 2024), you need to do a little bit more than that to allow password authentication in the sshd_config file. In the sshd_config file, there is this import file:

Include /etc/ssh/sshd_config.d/*.conf

If you follow that import into the sshd_config.d directory, you find a file named 60-cloudimg-setttings.conf in which you will find the following:

PasswordAuthentication no

All you have to do is change the no to yes.

PasswordAuthentication yes

It seems GCP or whoever added that Include really didn't want people SSHing into the VMs with passwords and made an extra effort to make sure it is not easy to do so. (Took me almost an entire day trying to figure why it was never working simply by changing the sshd_config file's PasswordAuthentication no to PasswordAuthentication yes.

Gatto answered 16/8, 2024 at 15:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.