如何修复“请在 azure ubuntu 1804 虚拟机上以用户“xx”而非用户“root”身份登录?

如何修复“请在 azure ubuntu 1804 虚拟机上以用户“xx”而非用户“root”身份登录?

我创建了一个没有 root 用户和 ssh 公钥的 azure ubuntu 1804 虚拟机。

然后我尝试使用脚本如何在没有 root 帐户、sshpass 和 sudo 的情况下启用 root ssh 公钥登录?

但我遇到了“请以用户“xx”而不是用户“root”登录的问题

答案1

有一个云初始化选项“disable_root_opts”。默认情况下,这是包含“请以 $USER 身份登录”的变量。

/lib/python3/dist-packages/cloudinit/config/schemas/schema-cloud-config-v1.json:2855:          "default": "``no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command=\"echo 'Please login as the user \\\"$USER\\\" rather than the user \\\"$DISABLE_USER\\\".';echo;sleep 10;exit 142\"``",
/lib/python3/dist-packages/cloudinit/config/schemas/schema-cloud-config-v1.json:2856:          "description": "Disable root login options.  If ``disable_root_opts`` is specified and contains the string ``$USER``, it will be replaced with the username of the default user. Default: ``no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command=\"echo 'Please login as the user \\\"$USER\\\" rather than the user \\\"$DISABLE_USER\\\".';echo;sleep 10;exit 142\"``"

https://cloudinit.readthedocs.io/en/latest/reference/modules.html#ssh

只需将该变量设置为空字符串,它就不会再在首次启动时将以下内容添加到 root 用户的 /root/.ssh/authorized_keys 中。

no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user \"ubuntu\" rather than the user \"root\".';echo;sleep 10;exit 142"

答案2

ssh -C -o StrictHostKeyChecking=no -o ConnectTimeout=10 xxxYourServerUsername@xxxYourServerIp 'sudo bash -c '"'"'umask 077 && mkdir -p .ssh && echo '"'"'"'"'"'"'"'"'xxxYourSshPublicKey'"'"'"'"'"'"'"'"' > /root/.ssh/authorized_keys'"'"''

将 xxxYourServerIp 替换为您的服务器 IP。将 xxxYourServerUsername 替换为您的服务器用户名。将 xxxYourSshPublicKey 替换为您的 SshPublicKey 内容(应类似于 ssh-rsa xxx root@xxx)

我认为这是安全且自动的方法,那么它应该是安全的如何在没有 root 帐户、sshpass 和 sudo 的情况下启用 root ssh 公钥登录?

相关内容