Quick tip I came across today while trying to directly upload data via SSH/SCP/SFTP or RSYNC to my NextCloud instance and change its owner to that of the webserver.
Yes, the “right way” to do it is via WebDAV but that’s not a great way to do it when you need to upload a ton of large files. If you find yourself in a similar situation, check out my short gist-style samples and give me a shout with any comments you have 🙂
Uploading a file to a remote server via SSH as root and changing its owner (chown) to www-data
cat /your/path/file.ext | ssh firstname.lastname@example.org 'cat > /destination/path/file.ext; chown www-data:www-data /destination/path/file.ext'
There’s probably a cleaner way to do it but this works and is pretty straightforward.
This is the file you want to transfer (for example a large video file)
| ssh email@example.com
This pipes the file you just loaded with the cat command to an SSH connection as the root user (important) on the destination
'cat > /destination/path/file.ext;
Takes the input from the file we just piped over SSH to the server and resembles the output to a file at a destination we want
chown www-data:www-data /destination/path/file.ext'
The final part is to change the ownership of the file to that of the webserver (www-data commonly), so that NextCloud in my case can read it
Using RSYNC to transfer a bunch of files as root and then changing the ownership to the webserver (www-data)
rsync -og --chown=www-data:www-data /path/local/folder firstname.lastname@example.org:/destination/path
Note that -og is important as otherwise, the command will fail without error. The –chown part should be pretty self-explanatory at this point 😉
Mac user? Make sure you have a recent version of RSYNC or this won’t work for you!https://dev.to/al5ina5/updating-rsync-on-macos-so-you-re-not-stuck-with-14-year-old-software-1b5i
Getting NextCloud to notice the new file(s)
The benefit of using WebDAV is that NextCloud automatically knows what’s happened. In our ‘around about way’ (typical for somebody like me from Milton Keynes) is that we need to instruct it to scan for new files.
I use a simple cron job that kicks off a scan every 15 min like this:
*/15 * * * * www-data php /app/code/occ files:scan --all
Note that we need to tell cron to execute OCC to run as the webserver user and I am telling it to scan all users everywhere. You can amend that to suit your needs (e.g. only your user).
Add your local SSH key to your remote server and log in without a password:
(assuming you don’t have one, otherwise skipt to step 2)
1 Create an SSH key on your local machine
Open a terminal (e.g. Terminal.app on a Mac) and copy and paste the following command
Then hit OK / enter until done (don’t set a password or else you’ll need to enter it when you use it later)
2 Share the public key part of your new private SSH key with the destination (e.g. your server)
You will need to enter your password this one time to get the key uploaded. Note that we are not uploading the private key (keep that one where it is!) – just the public one.
3 Try to log in
If all went well, you are now logged on to your destination as root (be careful to not accidentally do something silly like rm -rf /).
Sources and further reading
- How can I use ssh to cat to a file in a remote Linux directory if the directory doesn’t exist yet?
- How to copy a file without using scp inside an ssh session?
- Change permissions upon uploading with scp
- Using RSYNC and change owner/group
- Cloudron (Get $30 service credits and I’ll get some too if you sign-up for a paid plan using this link)
- NextCloud (a free and open-source alternative to cloud-hosted collaboration and file-sharing services)
- Rsync Manual