使用 sudo 存取複製文件

使用 sudo 存取複製文件

我如何為其他使用者複製文件或資料夾。新文件或資料夾必須有他的名字。

我有 cp 指令的 sudo 存取權限

USER1 ALL=(ALL) NOPASSWD: /bin/cp

我正在嘗試以下命令:

USER1@ySERVERNAME:HOME_PATH$ sudo -i -u USER2 cp file1 file2

我收到一個錯誤:

Sorry, user USER1 is not allowed to execute '/bin/bash -c cp file1 file2' as USER2 on SERVERNAME.

我該如何解決?

答案1

解決方案:

-i您應該從命令中刪除sudo

sudo -u USER2 cp file1 file2

解釋:

您面臨的問題是您的sudo存取權限僅限於/bin/cp並使用您沒有的sudo -i所需額外權限。sudo

正如錯誤中所指定的:

抱歉,不允許使用者 USER1 在 SERVERNAME 上以 USER2 身分執行「/bin/bash -c cp file1 file2」。

使用時sudo -i -u USER2 cp您正在執行的命令是/bin/bash -c cp 您沒有sudo權限的。因為您僅限於您有權使用的命令sudo/bin/cp

更多資訊:曼須藤

  -i, --login
                 Run the shell specified by the target user's password
                 database entry as a login shell.  This means that login-
                 specific resource files such as .profile or .login will be
                 read by the shell.  If a command is specified, it is passed
                 to the shell for execution via the shell's -c option.  If no
                 command is specified, an interactive shell is executed.  sudo
                 attempts to change to that user's home directory before
                 running the shell.  The command is run with an environment
                 similar to the one a user would receive at log in.  The
                 Command environment section in the sudoers(5) manual
                 documents how the -i option affects the environment in which
                 a command is run when the sudoers policy is in use.

相關內容