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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay