First, create a directory to be accessible by user
. Here user
is just a local user. Replace it with whatever user you want
sudo mkdir -p /var/sftp/files
Change the ownership of the files
directory to sftp user so that user
can read and write on this directory.
sudo chown user:user/var/sftp/files
Set the owner and group owner of the /var/sftp
to root
. This will ensure that files can be uploaded only in one directory i.e. files
and not in the parent directory.
sudo chown root:root /var/sftp
sudo chmod 755 /var/sftp
Now, edit the SSH config file
sudo nano /etc/ssh/sshd_config
Append these lines to the end of the file
Match User user
ForceCommand internal-sftp
PasswordAuthentication yes
ChrootDirectory /var/sftp
PermitTunnel no
AllowAgentForwarding no
AllowTcpForwarding no
X11Forwarding no
Replace user
with the username from above.
Finally, save the configuration and restart SSH service
sudo systemctl restart ssh