Attempting to setup an SFTP server on a mounted hard drive, and have followed the instructions from a bunch of tutorials, always get "client_loop: send disconnect: Connection reset" on login.

I started by creating a group called sftp, then assigning a user to that group with its home directory in the HDD:

sudo useradd -g sftp -d /media/linux/MyHDD/NAS/USERNAME -s /sbin/nologin USERNAME-nas

sudo passwd USERNAME

Where USERNAME is my personal username

I then attempted to set ownership of everything above the USERNAME folder to root and the USERNAME folder to the user:

sudo chown -R root:root /media

sudo chown -R USERNAME:sftp /media/linux/MyHDD/NAS/USERNAME

Editted the sshd_config file to contain:

Subsystem sftp internal-sftp

Match Group sftp
ChrootDirectory /media/linux/MyHDD/NAS/USERNAME
ForceCommand internal-sftp
AllowTcpForwarding no
X11 Forwarding no

Not forgetting to restart the sshd service

sudo systemctl restart sshd

As I wanted to access this folder on my linux computer, I set the permissions to 777 for the media folder

sudo chmod -R 777 /media

After doing all this, when attempting to login to sftp and entering password I am greeted with:


client_loop: send disconnect: Connection reset
Connection closed

Is this a permissions issue, or possibly an ownership issue, I don't know. I have spent a long time on this and cannot seem to figure it out, so any help would be appreciated.

  • Try running your sftp with -v to get verbose debugging. sftp -v -P PORTNO USERNAME@IP
    – Natolio
    Commented May 24, 2021 at 11:27

1 Answer 1

ChrootDirectory /media/linux/MyHDD/NAS/USERNAME
sudo chown -R USERNAME:sftp /media/linux/MyHDD/NAS/USERNAME
sudo chmod -R 777 /media

The "ChrootDirectory" feature requires that the chroot directory and its parent directories have certain ownership and permissions:

Specifies the pathname of a directory to chroot(2) to after authentication. At session startup sshd(8) checks that all components of the pathname are root-owned directories which are not writable by any other user or group. After the chroot, sshd(8) changes the working directory to the user's home directory. Arguments to ChrootDirectory accept the tokens described in the TOKENS section.

There are two issues with the way you set up your chroot directory:

First, /media, /media/linux/MyHDD/NAS/USERNAME, and all of the directories in between can't be world- or group-writable. They need to be mode 755 rather than 777.

Second, /media/linux/MyHDD/NAS/USERNAME itself has to be owned by root. The directories and files inside it can be owned by the user, but the USERNAME directory itself has to be owned by root.

  • 1
    Thank you! That's it working now. Really appreciate your help!
    – ManOfLakes
    Commented May 24, 2021 at 14:14

You must log in to answer this question.

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