Copy File Between User and Root
When using a remote file-system mounted using fuse and sshfs even the local super-user cannot access the remote files. This can be a problem when, for example, there's a need to copy a file to local system's privileged areas such as /etc/.
Here's some examples:
$ cp /mnt/remote/Backups/rootfs/etc/udev/rules.d/41-permissions-tun.rules /etc/udev/rules.d/ cp: cannot create regular file `/etc/udev/rules.d/41-permissions-tun.rules': Permission denied $ sudo cp /mnt/remote/Backups/rootfs/etc/udev/rules.d/41-permissions-tun.rules /etc/udev/rules.d/ [sudo] password for tj: cp: cannot stat `/mnt/remote/Backups/rootfs/etc/udev/rules.d/41-permissions-tun.rules': Permission denied
It is possible for the super-user to execute a shell script that redirects to the destination file, such as:
cat /mnt/remote/Backups/rootfs/etc/udev/rules.d/41-permissions-tun.rules | sudo sh -c "cat > /etc/udev/rules.d/41-permissions-tun.rules"
but this form can run into problems if there are quotes in the command itself, since they may need escaping now the entire output command is wrapped in quotes.
There are several other possible workarounds involving staging the file in a temporary locaton, but I found this is an elegant way to avoid confusion and run each half of the transaction as the correct user:
$ cat /mnt/remote/Backups/rootfs/etc/udev/rules.d/41-permissions-tun.rules | sudo tee /etc/udev/rules.d/41-permissions-tun.rules
To append (like doing >> shell redirection) add the -a option:
$ cat /mnt/remote/Backups/rootfs/etc/udev/rules.d/41-permissions-tun.rules | sudo tee -a /etc/udev/rules.d/41-permissions-tun.rules
The only draw-back is that tee echos all the output to the console which, if the input is larger (or binary) causes other problems.