1

How I can copy file or folder for other user. New file or folder must have his name.

I have sudo access for cp command

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

I am trying following command:

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

I got an error:

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

How can I solve it?

2

Solution:

You should remove the -i from the sudo command:

sudo -u USER2 cp file1 file2

Explanation:

The problem you are facing is that your sudo access is limited to /bin/cp and using sudo -i required extra sudo permissions which you don't have.

As specify in the the error:

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

When using sudo -i -u USER2 cp The command you are running is /bin/bash -c cp which you don't have sudo permissions for. As you are limited to the command which you have sudo permission for: /bin/cp.

More info: man sudo

  -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.
  • as I understand "file1" file need to have read permission, otherwise ti will give permission error: cp: cannot open ‘file1’ for reading: Permission denied – Hayk Hovhannisyan Feb 21 '18 at 7:56
  • @HaykHovhannisyan - If I've solved your question, please accept my answer – Yaron Feb 21 '18 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.