An older question, I know, but times change and so do some techniques. Just in case someone is still looking for a streamlined way to accomplish this.
- Your user on the server is a sudoers.
- You are running Windows 10.
- The files in
/var/www should belong to user:group
The concept is to combine remote commands over
scp file transfersers without relying on GUIs such as
WinSCP. These commands can be run either from a
Command Prompt or
PowerShell. There are five main tasks to permorm:
- Environment setup
- Transfer files to server
- Set remote file permissions
- Transfer between remote folders
Tasks 3-5 can be performed in a single step. If you plan to do this often, leaving the environment setup will allow you to omit tasks 1 and 5.
You may or may not already have folder you can use as a temporary repository for the transfer. If not, you can run:
ssh email@example.com "mkdir ~/wwwtemp"
Depending on your server's settings, you may or may not be prompted for
user's password/passphrase to authenticate the
Once the session is authenticated, the
mkdir ~/wwwtemp command will execute then the
ssh session will terminate, and you will be back at your prompt (
Command Prompt or
Transfer Files to Server
The next thing to do is to transfer the files from the local Windows machine to the Ubuntu server using
scp like so:
scp -R local\path firstname.lastname@example.org:~/wwwtemp/
Depending on your server's authentication method, you may or not need to enter a password/passphrase.
Permissions and Final Destination of Files
Once the file transfer has completed, you can run a series of commands over
ssh like thusly:
ssh -t email@example.com "sudo chown -R www-data:www-data ~/wwwtemp && sudo mv -R ~/wwwtemp/* /var/www/ && sudo rmdir ~/wwwtemp"
Again, depending on the authentication method of your server you may or may not be prompted for a
password/passphrase. Regardless of your authentication method,
sudo will prompt you for
user's password. Unless, of course you have disable requirement for password when
rmdir. See this question for guidance on how to do that.
This step covers tasks 3-5:
sudo chown -R www-data:www-data ~/wwwtemp recursively sets the desired file permissions on the files you just uploaded.
sudo mv -R ~/wwwtemp/* /var/www/ recursively moves the contents of the temporary repository to its final destination.
sudo rmdir ~/wwwtemp removes the temporary repository. It is necessary to use
sudo here since we changed the directory owner in task 3.
&& separates each command. The commands will be performed in sequence. If you plan to keep the repository
wwwtemp, you can omit the final command in the sequence.
You can omit
&& sudo rmdir ~/wwwtemp from the end of the final
ssh command string if you would like to continue using the temporary repository in future. Doing so also means that you can omit the first
ssh command each time you desire to transfer files to your server in this manner.