How to run SUDO command in WinSCP to transfer files from Windows to linux [duplicate]
Asked Answered
F

8

93

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

Flapdoodle answered 14/1, 2016 at 20:19 Comment(0)
B
41

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.

Beffrey answered 14/1, 2016 at 20:25 Comment(4)
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”, WinSCP FAQ How do I change user after login (e.g. su root)?, WinSCP support forum thread: Using WinSCP to upload to an ec2 server, Article Connect to Amazon’s Linux via WinSCP as root.Mesopause
Incorrect. There are actually a few different ways to accomplish this.Convincing
incorrect answer, please see the top answer above. don't know how an incorrect answer will be accepted?Qualm
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.Buckshot
B
147

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

Brandnew answered 21/6, 2018 at 15:0 Comment(11)
For more details see WinSCP FAQ How do I change user after login (e.g. su root)?Mesopause
its /usr/libexec/openssh/sftp-server for Amazon Linux AMIPriedieu
@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-serverPumpkinseed
/usr/lib/openssh/sftp-server for ubuntu 14Reine
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".Convincing
Worked for me on Ubuntu 18 (sudo su -c /usr/lib/sftp-server)Zilpah
Cannot initialize SFTP protocol. Is the host running an SFTP server? on the server, the SFTP server is running.Madonia
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.Extract
For centossudo /usr/libexec/openssh/sftp-serverCapita
Here is a working command for Amazon Lightstail server (Ubuntu): sudo su -c /usr/lib/openssh/sftp-serverCoonan
For Debian12: SFTP setting sudo /usr/lib/openssh/sftp-server (found by cat /etc/ssh/sshd_config |grep -i sftp-server) and I had to add myusername ALL=NOPASSWD: /usr/lib/openssh/sftp-server in any file in /etc/sudoers.d/Avrilavrit
I
70

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

enter image description here

enter image description here

Ila answered 8/12, 2018 at 5:59 Comment(7)
Error: "sudo: no tty present and no askpass program specified"Mortensen
Error skipping startup message. Your shell is probably incompatible with the application (BASH is recommended).Madonia
I solve this problem with change root instead of user and change root password.Madonia
I used sudo su - <user-name> and it worked for me.Prothalamion
Just make sure that your File Protocol is SCP not SFTPIsahella
Like Rami says. That´s itNadeau
Shell sudo su - works for me.Nevillenevin
B
41

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.

Beffrey answered 14/1, 2016 at 20:25 Comment(4)
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”, WinSCP FAQ How do I change user after login (e.g. su root)?, WinSCP support forum thread: Using WinSCP to upload to an ec2 server, Article Connect to Amazon’s Linux via WinSCP as root.Mesopause
Incorrect. There are actually a few different ways to accomplish this.Convincing
incorrect answer, please see the top answer above. don't know how an incorrect answer will be accepted?Qualm
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.Buckshot
T
6

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.

Trestle answered 10/9, 2018 at 8:16 Comment(0)
D
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.

Danette answered 11/4, 2021 at 23:4 Comment(0)
U
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
Unwise answered 10/9, 2021 at 14:39 Comment(1)
The above line is very permissive, it allows user YourUserName connected from anywhere (the first ALL) to run any command (the second ALL) as a root without being asked for password.Isogamete
I
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.

Immortality answered 25/2, 2020 at 8:1 Comment(3)
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?Immortality
If you have a question, then post a question instead of an "answer".Mesopause
It's kinda workaround for the above question, as I said I am looking for the direct solution for the same question.Immortality
C
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://mcmap.net/q/225573/-ssh-and-sudo-pam_unix-sudo-auth-conversation-failed-auth-could-not-identify-password-for-username

Cryptonym answered 27/12, 2020 at 13:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.