1

I have a requirement to sudo to user 'admin' after connecting to SFTP server with below command.

sftp user@hostname
sftp> sudo su - admin
Invalid command.

Is there any other way I can use sudo?

1
  • 1
    The answers are correct but one minor piece of advice. sudo su - admin is unnecessary. You can simplify the command to sudo -u admin, either with or without additional -i .
    – doneal24
    Apr 16, 2023 at 15:58

2 Answers 2

5

It's not completely true that you cannot change the user with SFTP.

For example, even if you are connecting to the common OpenSSH server, you can instruct it to run the SFTP subsystem/server with elevated privileges using a SFTP server startup command like:

sudo /path/to/sftp-server

For example with OpenSSH sftp client you can use something like:

sftp -s "sudo /path/to/sftp-server" [email protected]

Or with (my) WinSCP SFTP client you can also override the SFTP server start command (both in GUI and scripting).

But there's lot of limitations to allow this working. Particularly the sudo may not require password and TTY. For details, see WinSCP FAQ How do I change user after login (e.g. su root)?

1
  • 1
    oh good trick! Yeah, true. This is a possible workaround in case you can do noninteractive sudo auth. Nice! Apr 17, 2023 at 15:46
4

FTP / SFTP is not a shell in which you can run commands. It's a very specific-purpose protocol for the transfer of files and related operations. The sftp program hence can only give you a command interface to the features of that protocol.

So, by the very design of what sftp is not, you can't.

If you have shell access to the same server through plain ssh, then you can run commands like sudo and access the files you care about from there, and, for example, put them in an archive file, give that access rights for exclusively the user you originally logged in, and then copy that.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .