48

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

Im 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

PC2015
  • 517
  • 1
  • 4
  • 10
  • 1
    Possible duplicate of [WinSCP connect to Amazon AMI EC2 Instance changing user after login to "root"](http://stackoverflow.com/questions/6414158/winscp-connect-to-amazon-ami-ec2-instance-changing-user-after-login-to-root) – Martin Prikryl Jan 15 '16 at 07:00

7 Answers7

58

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
  • 3,453
  • 3
  • 29
  • 39
Pelly
  • 774
  • 7
  • 13
  • 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
  • 13
    its ```/usr/libexec/openssh/sftp-server``` for Amazon Linux AMI – Mr.Shan0 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
  • 1
    /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
  • 7
    Worked for me on Ubuntu 18 (`sudo su -c /usr/lib/sftp-server`) – Dammeul Jan 28 '20 at 11:53
  • Cannot initialize SFTP protocol. Is the host running an SFTP server? on the server, the SFTP server is running. – namjoo.org Nov 03 '20 at 21:28
  • 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
  • For centos`sudo /usr/libexec/openssh/sftp-server` – Peuczynski May 29 '21 at 20:17
35

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
  • 14,036
  • 13
  • 48
  • 65
  • 1
    Thank You! I think I will use the home directory as you suggested and transfer files over from putty thereafter. – PC2015 Jan 14 '16 at 20:34
  • 11
    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
  • 1
    Incorrect. There are actually a few different ways to accomplish this. – Brady Dec 14 '19 at 22:19
31

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

enter image description here

enter image description here

Eng.Fouad
  • 107,075
  • 62
  • 298
  • 390
3

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
  • 133
  • 1
  • 1
  • 7
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
  • 59
  • 4
  • 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
1

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
  • 964
  • 1
  • 13
  • 27
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
  • 479
  • 4
  • 8