Azure ubuntu 1804 가상 머신에서 "root" 사용자 대신 "xx" 사용자로 로그인하십시오. 문제를 해결하는 방법은 무엇입니까?

Azure ubuntu 1804 가상 머신에서 "root" 사용자 대신 "xx" 사용자로 로그인하십시오. 문제를 해결하는 방법은 무엇입니까?

루트 사용자와 SSH 공개 키 없이 Azure Ubuntu 1804 가상 머신을 만들었습니다.

그런 다음 스크립트를 사용해 보았습니다.루트 계정, sshpass 및 sudo 없이 루트 SSH 공개 키 로그인을 활성화하는 방법은 무엇입니까?

그런데 "root" 사용자 대신 "xx" 사용자로 로그인하십시오.라는 문제가 발생했습니다.

답변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/.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와 유사해야 함).

나는 이것이 안전하고 자동적인 방법이라고 생각합니다. 그렇다면 안전해야 합니다.루트 계정, sshpass 및 sudo 없이 루트 SSH 공개 키 로그인을 활성화하는 방법은 무엇입니까?

관련 정보