GCP - sudo 在 GoogleCloudShell 中有效,但在我 ssh 进入 vm 时无效

Pat*_*atS 4 ssh google-cloud-platform

我创建了一个全新的 Google Compute Engine VM (Debian 9) 并使用 GoogleCloud shell 打开了一个 shell。我可以sudo从那个浏览器外壳窗口。

然后我将我的 ssh 密钥和 ssh 设置到虚拟机中。为了便于讨论,我的用户名是“user123”。我使用以下 SSH 连接到我的 GCP 虚拟机:

ssh user123@1.2.3.4

其中 1.2.3.4 是我的 GCP 外部 IP 地址。然后我登录了。

所以当用户user123GoogleCloudShell sudo工作,但通过登录时的sshuser123,我提示输入密码。

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for user123:
Run Code Online (Sandbox Code Playgroud)

自然,我不想或不需要设置密码,我需要获得 sudo 以允许此user123用户帐户进行 sudo。但它在登录 GoogleCloudShell 时工作......嗯......我会追踪它,但这是我的问题。

注意:由于我在我的GoogleCloudShell 中有 root (通过 sudo),我应该能够找到我需要做的事情并解决这个问题。

为了允许 sudo 在通过 ssh 登录时工作,我需要采取的下一步是什么?

Tim*_*wne 6

我给你2个答案。

首先,cloud shell 在幕后为您管理实例元数据,并允许以这种方式添加到项目中的所有用户使用无密码 sudo。使用 google 设置您的用户

https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys https://cloud.google.com/compute/docs/instances/managing-instance-access

通过项目控制台添加您想要的用户帐户。这会将该用户传播到您项目中的所有主机。

其次,您想要的是设置无密码sudo。您可以(使用 visudo)编辑 /etc/sudoers 或在 /etc/sudoers.d 下创建一个新文件并添加如下一行:

user123       ALL = (ALL) NOPASSWD: ALL
Run Code Online (Sandbox Code Playgroud)

您可以看到 google 是通过 google-sudoers 组中的组成员身份进行的

timmy@instance-1:~$ sudo cat /etc/sudoers.d/google_sudoers 
%google-sudoers ALL=(ALL:ALL) NOPASSWD:ALL
timmy@instance-1:~$ id
uid=1000(timmy) gid=1001(timmy) groups=1001(timmy),4(adm),30(dip),44(video),46(plugdev),1000(google-sudoers)
Run Code Online (Sandbox Code Playgroud)