How to connect Winscp to a remote Linux machine?

[Last Updated: Apr 21, 2017]

Linux Ubuntu 

What is Winscp?

WinSCP (Windows Secure Copy) is an open-source GUI tool to transfer/edit file securely from MS Windows to remote computer, typically to a Linux computer. The destination computer must be running FTP or SFTP server.

In this quick guide, we will learn how to connect it to remote Ubuntu 16.04 server.

Winscp configuration

After downloading Winscp, start it by clicking it's icon. Create a session by filling the appropriate values:

After saving and clicking on Login, we can access and work with local and remote files:

By selecting files either in local (left) or remote (right) panel and by clicking on toolbar icons or by invoking right click context menu, we can perform various operations like transfer/edit/delete/create etc. We can also drag and drop files between the two panels.

By default, we can only transfer/edit files to remote home directory (/home/joe). Trying to transfer files for example to '/var/www/html', will give the following error:

That means we can only work within the user's remote home directory, later on, a user from sudo group can transfer the modified files to to the right destination on the remote machine. Or the user 'joe' can be given permission to modify the target directory '/var/www/html'. Conceptually, the Linux system is meant to be used this way. But if we still want to have all access to the whole system then we must be working as 'root' user from Winscp.

Performing operations as root user

Exit Winscp and start again. In the login window click on 'Edit' button (just below the User name field) and then click on 'Advance', that will open 'Advanced Site Settings' window. In the SFTP panel enter following value for 'SFTP server':

sudo /usr/lib/sftp-server

Your sftp-server location can be other than /usr/lib/sftp-server. To find the location, use the following (logged in via putty):

$ whereis sftp-server
sftp-server: /usr/lib/sftp-server /usr/share/man/man8/sftp-server.8.gz

Make sure you are finding 'sftp-server' location, not 'sftp' location.

After you have entered the value for SFTP server click OK, then save and then Login. This time you should be able to transfer files everywhere. If that's still not possible and Winscp go back to the login window over and over again, then on the remote server computer, you can add 'NOPASSWD' permission in the '/etc/sudoers' file or in a file (without any extension or ~) under '/etc/sudoers.d/' direcotry. Also you must use 'visudo' to modify the sudoers file.

Modifying sudoers file manually is very dangerous, a mistake can lead you to a situation where you can never use 'sudo' again unless you reinstall the whole system. You should do your own research before modifying it. Some of the information can be found here

In my case, I did the following (accessing my remote cloud machine via putty):

 visudo -f /etc/sudoers.d/90-cloud-init-users
I entered a new line:
 joe ALL=(ALL) NOPASSWD:/usr/lib/sftp-server

That fixed the problem of Winscp 'asking for login persistently' for me.