I'm trying to copy a file from my home directory to another user's directory. I have a sudo access for the other user with no password. I tried doing something like:

sudo secondUser cp /home/firstUser/file /home/secondUser 

is there a way to do that? also I have no root access and I don't know the password of the secondUser.


6 Answers 6


EDIT: There is a way to do this quickly and easily with no additional setup.

cat ~firstUser/file | sudo -u secondUser tee ~secondUser/file >/dev/null

This will transfer all the file's content exactly. If you care about having the file permissions and/or timestamps match the original, you will need to fix those separately using chmod and touch.


The problem here is that:

  • secondUser doesn't have access to your home directory.
  • You don't have access to secondUser's home directory.

As such, regardless of whether you run the cp command as yourself or as secondUser, it can't perform the file copy.

Given that you said you don't have root access, the obvious answer is to copy the file through an intermediate world-readable location such as /tmp and change the permissions of the file to world-readable. If the data in the file is sensitive you may not want to do this, however, because anyone on the server will be able to read the file while it is in transit. If it's not sensitive data, just do:

cp file /tmp/
chmod a+r /tmp/file
sudo -u secondUser cp /tmp/file ~secondUser
rm /tmp/file

A better option, if you can arrange it, is to make a group that contains only you and secondUser, and chgrp the copy of the file in /tmp to be owned by that group. This way you don't need to make the file world-readable, but only readable by the group (with chmod g+r /tmp/file). However, groupadd itself requires root access, so this is unlikely to be easy to arrange. Depending on the circumstances (if you are frequently trying to share/collaborate with secondUser it might be applicable), you could consider asking your administrator to set up this group for future use.

  • I suspect it's also possible to do this using rsync and pretending the recipient is on a remote server, e.g. some variation of rsync --rsh='sudo -u seconduser -s' ~firstuser/file ~seconduser/file but I don't have time to test this right now.
    – Wildcard
    Sep 18, 2023 at 19:02

The problem with:

sudo -u secondUser cp /home/firstUser/file /home/secondUser 

is that as soon as you become secondUser, you don't have access to /home/firstUser/file.

The solution is you need to open the file for secondUser as firstUser and have secondUser inherit that file descriptor.

As a security measure, sudo will close all file descriptors except 0,1, and 2; however, that's all you need in this case (you can explicitly allow inheritance for more file descriptors):

sudo -u secondUser < /home/firstUser/file sh -c 'cat > /home/secondUser/file' 

For a more generic solution with possible metadata preservation, you can put the output of something like tar or cpio on your sudo process's stdin:

 tar c file1 file2 file3 | sudo -u secondUser tar x -C ~secondUser

The -u flag to sudo will allow you to specify the user account under which a command should run, but will require you to know the other user's password.

sudo -u secondUser cp file /home/secondUser/file

If you do not know the password of the other user, you may not be able to do this. This is part of the normal privilege separation design of the system, and should ideally not be able to be bypassed.

You could also move the file into a place where both user accounts have access to it, such as /tmp, and have the second user move it to their own home directory.

  • This only works if the file can be accessed by secondUser. If it can't be accessed, this command will fail. If it can be accessed, then you/he can copy anytime the file himself (as secondUser).
    – magor
    Apr 21, 2016 at 19:36
  • thanks @Grond for your reply, I have tried that and it didn't ask me for the password instead it said that the cp: cannot stat 'file': permission denied
    – tkyass
    Apr 21, 2016 at 20:05
  • Perhaps the secondUser has no read permission. In that case you should invoke chmod a+r file first (as firstUser). Apr 21, 2016 at 20:18
  • As i mentioned, if the 'file' cannot be accessed by secondUser, this command will fail with the error what you've quoted. If it can be accessed, the command will work but then it's not necessary to sudo, secondUser can copy the file himself anytime. You also have to change all the parent folders' permission as well for the file to be accessible for the second user. But then maybe other users will be able to enter and see your folder and it's content.
    – magor
    Apr 21, 2016 at 20:27

You can do

su secondUser

and then do stuff as the 2nd user.


Copy the file to /tmp and change permissions to 777. su to the second user. Copy the file from /tmp.
The command sudo secondUser is not valid.


Instead of this, sudo secondUser cp /home/firstUser/file /home/secondUser

Use this, sudo cp /home/firstUser/file /home/secondUser

  • 1
    What about "I have no root access and I don't know the password of the secondUser."?
    – user62916
    Feb 25, 2018 at 0:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .