copy file from user to another in linux
Solution 1
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
.
ORIGINAL ANSWER:
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.
Solution 2
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
Solution 3
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.
Related videos on Youtube
![tkyass](https://i.stack.imgur.com/ze95O.jpg?s=256&g=1)
tkyass
Updated on September 18, 2022Comments
-
tkyass almost 2 years
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.
-
Scott - Слава Україні about 6 yearsRelated (but not really very useful): How do I copy a file/folder from another user’s home directory in Linux? (on Super User).
-
-
magor about 8 yearsThis 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).
-
tkyass about 8 yearsthanks @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
-
Kamil Maciorowski about 8 yearsPerhaps the secondUser has no read permission. In that case you should invoke
chmod a+r file
first (as firstUser). -
magor about 8 yearsAs 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.
-
react-newbie over 6 yearsWhat about "I have no root access and I don't know the password of the secondUser."?