在谷歌云SSH-in-browser,修改root用户密码。

sudo -i
passwd root   

添加允许用户名和密码登录。

修改前备份文件

sudo cp /etc/ssh/sshd_config ~/sshd_config.bak

开启SSH用户密码登录

echo "PermitRootLogin yes" | sudo tee -a /etc/ssh/sshd_config
echo "PasswordAuthentication yes" | sudo tee -a /etc/ssh/sshd_config

重启服务,或者重启服务器生效。

sudo systemctl restart ssh

如果修改无效,检查是否有其他配置文件。

查看配置文件

ls -ls /etc/ssh/sshd_config.d/
total 8
50-cloudimg-settings.conf
60-cloudimg-settings.conf

有些 Ubuntu 镜像会在/etc/ssh/sshd_config.d/目录下存放覆盖配置(比如50-cloud-init.conf),这会导致你修改主文件无效。 运行命令删除冲突配置:

sudo rm -f /etc/ssh/sshd_config.d/*.conf

或者修改文件,建议修改。注销掉下面的内容即可。

PermitRootLogin yes
PasswordAuthentication yes

查看配置文件内容

sudo cat /etc/ssh/sshd_config.d/50-cloudimg-settings.conf 
# CLOUD_IMG: This file was created/modified by the Cloud Image build process

# Google Compute Engine times out connections after 10 minutes of inactivity.
# Keep alive ssh connections by sending a packet every 2 minutes.
ClientAliveInterval 120

# Prevent reverse DNS lookups.
UseDNS no

sudo cat /etc/ssh/sshd_config.d/60-cloudimg-settings.conf 
#PasswordAuthentication no    //注释后保存退出。          

重启服务,或者重启服务器生效。

sudo systemctl restart ssh