DEV Community

Eduardo Issao Ito
Eduardo Issao Ito

Posted on • Edited on

SFTP server in Ubuntu

How to setup a basic SFTP server in Ubuntu.

Requirements

  • No login
  • Read and write permissions
  • Access only to a base directory
  • Default directory should be the base directory

Instalation

Install the sftp server package
sudo apt-get install openssh-server 
Enter fullscreen mode Exit fullscreen mode
Create sftp group and user
sudo groupadd sftpgroup sudo useradd myuser -m -G sftpgroup -s /usr/sbin/nologin sudo passwd myuser 
Enter fullscreen mode Exit fullscreen mode
Prepare base sftp directory
sudo chown root:root /home/myuser sudo mkdir /home/myuser/data sudo chown myuser:sftpgroup /home/myuser/data 
Enter fullscreen mode Exit fullscreen mode
Configure sftp server

Edit the file /etc/ssh/sshd_config

Comment the following line:

#Subsystem sftp /usr/lib/openssh/sftp-server 
Enter fullscreen mode Exit fullscreen mode

Add the folowing lines at the end of the file:

Subsystem sftp internal-sftp Match group sftpgroup ChrootDirectory %h X11Forwarding no AllowTcpForwarding no ForceCommand internal-sftp -d /data 
Enter fullscreen mode Exit fullscreen mode

Restart the server

sudo service ssh restart 
Enter fullscreen mode Exit fullscreen mode

Test

$ sftp myuser@localhost myuser@localhost's password: Connected to localhost. sftp> pwd Remote working directory: /data sftp> put test.txt Uploading test.txt to /data/test.txt test.txt 100% 0 0.0KB/s 00:00 sftp> ls test.txt sftp> quit 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)