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 ๐Ÿ™‚

Does this happen to you when uploading lots or large files over WebDAV? Then this tip may keep you from bashing your head against the keyboard. Read on…

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 root@remote.server '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.

cat /your/path/file.ext

This is the file you want to transfer (for example a large video file)

| ssh root@remote.server 

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 root@remote.server:/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).

Bonus tip

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

ssh-keygen

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)

ssh-copy-id root@destination.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

ssh root@destination.server

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