Thought this might be of interest. I'd seen various post requesting this but none that had it resolved. There may be a better way to do it that I didn't discover however, the method below works.
1) Copy the
sftp-server
binary to another name e.g.
cd /root
cp /usr/libexec/openssh/sftp-server admin1-sftp-server
2) Make the admin user the owner e.g.
chown admin1 admin1-sftp-server
3) Make the admin user's primary group the group of file e.g.
If admin1's GID in
/etc/passwd
is 100 and 100 is "users" group in
/etc/group
then:
chgrp users admin1-sftp-server
4) Set suid and sgid bit on the file and make it readable/executable by user and group. e.g.
chmod 6550 admin1-sftp-server
Don't make it writable by any of these users so they can't replace the binary itself – also as noted in step 1 this binary should be in a secure location such as
/root
to which no one but root has access normally.)
5) As discussed in
https://winscp.net/eng/docs/faq_su you set up
sudoers
file (use visudo) to allow the new script to be run by user as root. e.g.
ralph ALL=NOPASSWD: /root/admin1-sftp-server
billybob ALL=NOPASSWD: /root/admin1-sftp-server
Note that you don't have to do individual lines like that –
User_Alias
and
Cmnd_Alias
would work.
6) As discussed in same link edit the SFTP tab (Advanced options) sftp-server box in WinSCP for the session to have
sudo
to the
sftp-server
copy you made instead of "Default". e.g.
sudo /root/admin1-sftp-server
7) Save the session in WinSCP.
Select the saved session and "login" from WinSCP after doing above. It will open the copied
sftp-server
(e.g.
/root/admin1-sftp-server
) running as the user that owns this (admin1-)sftp-server instead of root. Any files created by this session will be owned by the user that owns (admin1-)sftp-server and grouped to the group of (admin1-)sftp-server.
Additional Note:
The downside to above approach is if you later patch your system
sftp-server
for some reason it won't automatically patch copies like admin1-sftp-server. If the reason for patching is a security concern it is important you repeat the process above after the patching to insure you have the same security fix in all copies.