Automatically change ownership using WinSCP?

All we need is an easy explanation of the problem, so here it is.

I saw several threads about having issues with unexpected file permissions after file upload. I tried their solutions with no luck, I have to raise the problem in my way.

I run Nginx as nobody:nogroup on Debian 6. I changed the root web folder /usr/share/nginx/html to nobody:nogroup. Now I use WinSCP to upload file to the machine , but I use root account. If I create a new directory or update a new file, the ownership would be root:root. I am new to Linux world, I currently open a shell to change ownership of wwwroot after I have uploaded something to my server.

What is the best practice to upload file as root, but keep files and directories nobody:nogroup? Shall I set a password to account nobody and use this account to upload files?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can change group of the upload directory chgrp nogroup UPLOAD_DIR and set group ID on it chmod g+s UPLOAD_DIR – files created in this directory will have group nogroup. You can set default rights using umask 002 (perhaps when starting WinSCP session?) and all files will have rw rights for group. The owner will still be root, but the Nginx process should be able to manage files through nogroup membership.

Method 2

I do not know of any automatic solution for new files. Though, if you find that you need to keep changing the ownership manually, you can make use of various WinSCP features to make your life easier.

WinSCP allows changing ownership in the GUI (though if you use OpenSSH server, you have to use numeric GID/UID).

Alternatively, you can execute the chown command from WinSCP itself to avoid opening separate shell terminal. You can even setup a custom command chown nobody:nogroup . in WinSCP to speed this up.

You can even assign a keyboard shortcut to the command to make this even easier:

enter image description here

For updating files, make sure you disable transfer setting Transfer to temporary filename as this effectively creates a new file for every transfer (by default for files over 100 KiB only). When disabled, WinSCP only updates an existing file, so the ownership is preserved.
https://winscp.net/eng/docs/resume#automatic
https://winscp.net/eng/docs/ui_pref_resume

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply