为什么双 sudo 修改权限?

kdb*_*man 4 linux sudo permissions amazon-web-services amazon-ec2

语境:

ssh使用默认的ec2-user. 我正在尝试列出默认 postgresql 目录的内容:

里面有什么?

$ ls /var/lib/pgsql9/

ls: cannot open directory /var/lib/pgsql9/: Permission denied
Run Code Online (Sandbox Code Playgroud)

好的。谁拥有这个?

$ ls -l /var/lib | grep pgsql
drwx------ 4 postgres postgres 4096 Jun 22 16:06 pgsql9
Run Code Online (Sandbox Code Playgroud)

啊。傻我。

$ sudo -u postgres ls /var/lib/pgsql9/

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 ec2-user:
Run Code Online (Sandbox Code Playgroud)

... 什么?ec2-user有密码吗?是时候用更大的枪了。

$ sudo sudo -u postgres ls /var/lib/pgsql9/
backups  data
Run Code Online (Sandbox Code Playgroud)

成功! 现在,去 superuser.com 看看发生了什么。

为什么sudo sudo行为与sudo?

Nat*_*n C 6

Amazon EC2 的构建方式与 Ubuntu 类似:没有root访问权限,一切都由它来完成sudo

您的sudo命令所做的是尝试模拟postgres,需要获得许可才能这样做。ec2-user没有这个权限,所以sudo会要求输入密码(这将失败,因为它没有密码)。

当您这样做时sudo sudo,您正在sudo以具有模拟其他用户权限的 root 身份调用第二个,因此该命令有效。由于sudo配置方式(sudoers 中的 NOPASSWD 行),它不需要密码即可以 root 身份执行。