太烦了,之前去开 lightsail 的机器,每次都不能使用密码登陆,然后在网上翻来翻去找到了这篇来自萌咖的文章。

将下面代码中的第一句中的 Vicer 改为自己将要设置的密码.
否则默认root密码为Vicer.
创建instance时,点击add launch script,将下列代码复制进去(注意更改 root 密码)如下图所示.

echo root:Vicer |sudo chpasswd root
sudo sed -i 's/^.*PermitRootLogin.*/PermitRootLogin yes/g' /etc/ssh/sshd_config;
sudo sed -i 's/^.*PasswordAuthentication.*/PasswordAuthentication yes/g' /etc/ssh/sshd_config;
sudo reboot

对于 EC2,GCE 等机器也可以使用.

如果是甲骨文的机器,只能使用 root + 密码登录 SSH,root + SSH 密匙登录仍然提示:Please login as the user “opc” rather than the user “root”.
无意中发现玄机其实在 /root/.ssh/authorized_keys 里面。打开这个文件,里面是这样子:

no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"opc\" rather than the user \"root\".';echo;sleep 10" ssh-rsa ooxxxxxx55524242442……此处省略 1024 万字 ……
ssh-ras XXX 为初始化实例时添加的 SSH 公钥,亲测,把 authorized_keys 多余的代码:

no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"opc\" rather than the user \"root\".';echo;sleep 10"
去掉,就可以愉快的使用 root + SSH 密匙登录。

P.S 如果不使用秘钥登录或者后面会使用秘钥登录,直接把 authorized_keys 全部删掉也是可以的

不能添加代码的机器,可以登录进 ssh,再执行上面的代码.
也可以开启 root 登陆.
以上代码在 Debian/Ubuntu 上通过测试.
其他发行版自行测试.

标签: none

添加新评论