82

I am trying to use WinSCP to transfer files over to a Linux Instance from Windows.

I'm using private key for my instance to login to Amazon instance using ec2-user. However ec2-user does not have access to write to the Linux instance

How do I sudo su - to access the root directory and write to the Linux box, using WinSCP or any other file transfer method?

Thanks

Eddie C.
  • 918
  • 10
  • 16
PC2015
  • 977
  • 1
  • 6
  • 10

8 Answers8

132

I know this is old, but it is actually very possible.

  • Go to your WinSCP profile (Session > Sites > Site Manager)

  • Click on Edit > Advanced... > Environment > SFTP

  • Insert sudo su -c /usr/lib/sftp-server in "SFTP Server" (note this path might be different in your system)

  • Save and connect

Source

AWS Ubuntu 18.04: enter image description here

adinas
  • 4,150
  • 3
  • 37
  • 47
Alessandro Benassi
  • 1,640
  • 2
  • 14
  • 19
  • 1
    For more details see WinSCP FAQ [How do I change user after login (e.g. su root)?](https://winscp.net/eng/docs/faq_su) – Martin Prikryl Jun 22 '18 at 05:24
  • 31
    its ```/usr/libexec/openssh/sftp-server``` for Amazon Linux AMI – Ahsaan Yousuf Nov 27 '18 at 11:57
  • 1
    @Mr.Shan0 I love you :D FYI I think this is for Amazon Linux 2 which is CentOS-based... Amazon Linux is Ubuntu-based and uses /usr/lib/sftp-server – Connor Feb 07 '19 at 21:13
  • 2
    /usr/lib/openssh/sftp-server for ubuntu 14 – user889030 Mar 13 '19 at 08:45
  • Just to add to this- I'm using openssh on CentOS 7, "sudo su -c /usr/lib/openssh/sftp-server" worked for me, but only *after* editing the sudoers file to comment out "Defaults !visiblepw". – Brady Dec 14 '19 at 22:26
  • 16
    Worked for me on Ubuntu 18 (`sudo su -c /usr/lib/sftp-server`) – Adam Jan 28 '20 at 11:53
  • 2
    Cannot initialize SFTP protocol. Is the host running an SFTP server? on the server, the SFTP server is running. – Darwin Nov 03 '20 at 21:28
  • 2
    Same as @Biodiversity I am getting an "Cannot initialize SFTP protocol. Is the host running an SFTP server?" error on WinSCP. I've verified my sftp-server path is correct too. – protango Jan 07 '21 at 08:34
  • 1
    For centos`sudo /usr/libexec/openssh/sftp-server` – Peuczynski May 29 '21 at 20:17
  • Here is a working command for Amazon Lightstail server (Ubuntu): `sudo su -c /usr/lib/openssh/sftp-server` – Rodion Mostovoi Nov 11 '22 at 21:45
63

There is an option in WinSCP that does exactly what you are looking for:

enter image description here

enter image description here

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
40

AFAIK you can't do that.
What I did at my place of work, is transfer the files to your home (~) folder (or really any folder that you have full permissions in, i.e chmod 777 or variants) via WinSCP, and then SSH to to your linux machine and sudo from there to your destination folder.

Another solution would be to change permissions of the directories you are planning on uploading the files to, so your user (which is without sudo privileges) could write to those dirs.

I would also read about WinSCP Remote Commands for further detail.

Idos
  • 15,053
  • 14
  • 60
  • 75
  • 13
    It not really true that this is not possible. References: Duplicate question: [WinSCP connect to Amazon AMI EC2 Instance changing user after login to “root”](http://stackoverflow.com/q/6414158/850848), WinSCP FAQ [How do I change user after login (e.g. su root)?](http://winscp.net/eng/docs/faq_su), WinSCP support forum thread: [Using WinSCP to upload to an ec2 server](https://winscp.net/forum/viewtopic.php?t=9476), Article [Connect to Amazon’s Linux via WinSCP as root](http://www.thatsgeeky.com/2011/10/connect-to-amazons-linux-via-winscp-as-root/). – Martin Prikryl Jan 15 '16 at 07:02
  • 2
    Incorrect. There are actually a few different ways to accomplish this. – Brady Dec 14 '19 at 22:19
  • 1
    incorrect answer, please see the top answer above. don't know how an incorrect answer will be accepted? – Nhân Trần Dec 16 '21 at 10:12
  • 1
    Please edit this question to real answer and if you want to keep the useful data of your answer, keep it. But this is very mistaken. – Leandro Bardelli Apr 21 '22 at 13:56
5

Usually all users will have write access to /tmp. Place the file to /tmp and then login to putty , then you can sudo and copy the file.

S A
  • 187
  • 1
  • 2
  • 7
4

I just wanted to mention for SUSE Enterprise server V15.2 on an EC2 Instance the command to add to winSCP SFTP server commands is :

sudo su -c /usr/lib/ssh/sftp-server

I didn't have enough Reputation points to add a comment to the original answer but I had to fish this out so I wanted to add it.

Anton Krug
  • 1,555
  • 2
  • 19
  • 32
2

ssh to FreePBX and run the commands stated below in your terminal:

sudo nano -f /etc/sudoers.d/my_config_file

YourUserName ALL=(ALL) NOPASSWD:ALL

sudo systemctl restart sshd

Winscp:

under session login ==> Advanced ==> SFTP

Change SFTP Server to:

sudo /usr/libexec/openssh/sftp-server
ecm
  • 2,583
  • 4
  • 21
  • 29
Ronen
  • 21
  • 2
1

I do have the same issue, and I am not sure whether it is possible or not,

tried the above solutions are not worked for me.

for a workaround, I am going with moving the files to my HOME directory, editing and replacing the files with SSH.

Naresh A
  • 57
  • 6
  • Yes, but in /tmp, there could be lots of application session files, I suggest to have the files in the HOME directory only to avoid the confusion and user can track their files if required they can verify. Can you kindly provide the solution on my question, if you have any? – Naresh A Feb 25 '20 at 14:20
  • If you have a question, then post a question instead of an "answer". – Martin Prikryl Feb 26 '20 at 07:01
  • It's kinda workaround for the above question, as I said I am looking for the direct solution for the same question. – Naresh A Feb 26 '20 at 07:44
0

Tagging this answer which helped me, might not answer the actual question

If you are using password instead of private key, please refer to this answer for tested working solution on Ubuntu 16.04.5 and 20.04.1

https://stackoverflow.com/a/65466397/2457076

SRi
  • 1,046
  • 7
  • 10