Running a Command Over sftp With sudo Permissions in WinSCP (aka How To Script sudo Without Entering a Password)

WinSCP, as well as some other ftp/sftp clients, allow you to run commands while connected to the server. They don’t allow you the same flexibility as a true ssh session, but it’s not as intimidating to many newcomers either. Other times you may want to have a script execute without user intervention but which needs to run sudo. How do you do it?

So here was my situation recently that reminded me of this trick: I had to advise someone who needed to copy some files to a directory on a server that required more permissions than his ssh user had. But they were scared off by the idea of having to face a shell. So instead I got them to fire up WinSCP. From the Options menu I got them to enable/show the Commands line (alternatively you could go to Commands, Open Terminal). Then to feed the password to the sudo you follow this format:

echo mypassword | sudo -S /path/to/my/command

And that’s it! Obviously replace mypassword with your actual user password, and always use the full path to the command you want to execute.