Reputation: 15
I'm using Renci.SshNet to connect to my Linux server. But it seems like my "sftpuser" can not find the dir /media/FileServer/. In fact when I connect, and check the working directory, it's at / and the only 'directories' it can find is /.. and /. used code:
var dir = sftp.ListDirectory(sftp.WorkingDirectory);
foreach (var dirEntry in dir)
{
Console.Out.WriteLineAsync(dirEntry.FullName);
}
sftp.ChangeDirectory("/media/FileServer/"); //Here the error occurs
I've set the sshd config as following:
Match Group sftp_users
X11Forwarding no
AllowTcpForwarding no
ChrootDirectory /media/FileServer
ForceCommand internal-sftp
I've also done
sudo chown root:root /media/FileServer
sudo chmod 755 /media/FileServer
but no success.
I've tried making the sftpuser the dir owner with sudo chown -R sftpuser:sftpuser /media/FileServer
But when I do this I get another error stating "An established connection was aborted by the server." and when I chekc the logs it states "fatal: bad ownership or modes for chroot directory "/media/FileServer"
Upvotes: 1
Views: 1942
Reputation: 25400
ChrootDirectory /media/FileServer
...
sftp.ChangeDirectory("/media/FileServer/"); //Here the error occurs
In your ssh server configuration, you're setting sessions to have a root directory of "/media/FileServer". That means the actual "/media/FileServer" directory on the server will appear to remote users as the root directory, "/".
When you make an SFTP connection to the server--and assuming the connection is subject to the "ChrootDirectory" directive--then the "/" directory within the session is the "/media/FileServer" directory. Your changeDirectory()
call is attempting to change to the directory "/media/FileServer/media/FileServer". This directory most likely doesn't exist, so you get an error.
I've tried making the sftpuser the dir owner with sudo chown -R sftpuser:sftpuser /media/FileServer
The ChrootDirectory feature requires that the ChrootDirectory and its parent directories all be owned by root without group or world write permission. Changing "/media/FileServer" to be owned by "sftpuser" prevents the feature from working, and the SSH server will drop the session due to the misconfiguration.
Upvotes: 0