Every other answer here requires root access, allows any user on the same machine to copy the file, or requires password sharing. Here is a method that does not:
Have USER2
(let's call him Bob) run the following commands (you can replace /tmp
with any directory that both users have permissions to write to, but /tmp
is ideal because by default it is sticky, which prevents a malicious user from subverting this process. A directory owned by Bob that is world-readable also works):
[bob@computer ~]$ touch /tmp/test.txt
[bob@computer ~]$ chmod 622 /tmp/test.txt
This creates a file that is world-writable, but not readable.
Then have USER1
(let's call her Alice) run (if you're paranoid, Alice can check the permissions first to make sure the file is owned by Bob):
[alice@computer ~]$ dd if="$HOME/test.txt" of=/tmp/test.txt
This overwrites the contents of /tmp/test.txt
. If you want to check integrity of the file, Alice should also generate a hash of the file. For instance:
[alice@computer ~]$ openssl sha1 < "$HOME/test.txt" > /tmp/test.txt.sha1
You can instead digitally sign the file, or any other method of guaranteeing its integrity.
And finally Bob moves the file and takes ownership of it:
[bob@computer ~]$ mv /tmp/test.txt "$HOME"
[bob@computer ~]$ chmod 600 "$HOME/test.txt"
And Bob can check the integrity if he likes. If so, he should check to make sure only Alice can write to /tmp/test.txt
.
[bob@computer ~]$ diff /tmp/test/txt.sha1 <(openssl sha1 < "$HOME/test.txt")
If the file was copied correctly, this should display no output.