SFTP (SSH File Transfer Protocol, or Secure Shell File Transfer Protocol) provides secure file access, transfer, and management over SSH. It uses a single encrypted channel on TCP port 22, so you don’t need a separate FTP server or extra firewall ports. This guide walks you through installing and configuring an SFTP server on Ubuntu 20.04 using OpenSSH, then shows common SFTP client commands.
SFTP vs FTP
The File Transfer Protocol is a standard communication protocol used for the transfer of computer files from a server to a client on a computer network (including the Internet). FTP is built on a client–server model architecture using separate control and data connections between the client and the server. FTP has always been popular for storing or moving large files between systems. Files can be added to a central server, then users who want to access them can access them from there.
SFTP is aimed at providing enhanced security with tunneling using Secure Shell 2 (SSH2), a secure tunneling protocol. It emulates an FTP connection and provides a firewall friendly and encrypted channel for file transfers using TCP port 22. SSH offers enhanced security by having the entire file transfer session, including all session control commands, entirely encrypted at all times while only requiring a single port be opened on your firewall versus the two ports that need to be opened for FTP and SSL connections.
As an added feature, Secure FTP also compresses all data during the transmission, which can result in faster file transfers.
Related posts
- How to install and set up SFTP server in Ubuntu 22.04
- How to work with SFTP client in Linux – 10 sftp commands
- How to set up an SFTP server on Debian 11 Server
- Download files from SFTP server using a Python script
- List, upload and download files from an SFTP server using Go
- How to set up an SFTP server on OpenSUSE Leap 15.3 Server
- How to set up an SFTP server on CentOS 8 / RHEL 8 Server
Prerequisites
- Ubuntu 20.04 server or desktop with sudo access.
- SSH port 22 reachable (and allowed in firewall if you connect remotely).
Install and configure SFTP on Ubuntu 20.04
We will: (1) install the OpenSSH server, (2) create an SFTP user and group, (3) configure the SSH daemon for chrooted SFTP, and (4) connect and run basic SFTP commands.
1. Install OpenSSH server
SFTP is provided by the OpenSSH server. Ensure your system is up to date, then install the ssh meta-package (which pulls in OpenSSH server) if needed:
| |
| |
Example output:
| |
2. Create SFTP user and group
Create a dedicated group and user for SFTP access (you can change the names).
Create a group:
| |
Example output:
| |
Create a user in that group with a home directory:
| |
Set a password for the SFTP user:
| |
Restrict the home directory to the user (required for chroot):
| |
3. Configure the SSH daemon for SFTP
Edit the SSH server config:
| |
Add the following block at the end of the file (use a single Match block; order matters):
| |
This config:
- Match group sftpgroup1 – applies only to users in
sftpgroup1. - ChrootDirectory /home – restricts them to
/home(they see/homeas/and can only reach their own/home/sftpuser1). - ForceCommand internal-sftp – allows SFTP only; no shell or port forwarding.
Save and exit, then restart SSH:
| |
Note: ChrootDirectory (here /home) must be owned by root and not writable by the chrooted user. Each user’s directory under /home (e.g. /home/sftpuser1) can be owned by that user so they can upload and create files there.
4. Connect from the terminal
From any machine with an SSH client (including the server itself), connect with:
| |
Or from another host, use the server’s IP or hostname instead of 127.0.0.1. When prompted, enter the SFTP user’s password.
Example:
| |
You are chrooted under /home; your effective root is /home, and your home directory appears as /sftpuser1. Run cd sftpuser1 to enter your upload directory.
Basic SFTP commands
Check version
| |
Show remote working directory
To see the current working directory on the remote server, use pwd:
| |
Show local working directory
To see the local machine’s present working directory, use lpwd:
| |
The SFTP user is restricted to their home. Run cd sftpuser1 to enter it, then create a directory with mkdir:
| |
Upload files
From the local shell (one-liner): use a here-string to run put after connecting:
| |
Example:
| |
From inside an SFTP session: use ls (remote), lls (local), and put to upload:
| |
Use mput with a glob to upload multiple files (e.g. data[23] uploads data2 and data3):
| |
Download files
From the local shell: download a single file in one command:
| |
Example:
| |
From inside SFTP: use get for one file and mget with a glob for multiple:
| |
| |
Create and delete directories
Use mkdir and rmdir:
| |
Remove files
Use rm (supports globs):
| |
Rename files
Use rename oldpath newpath:
| |
Check filesystem usage
Use df (and df -h for human-readable sizes). The stats are for the remote SFTP server’s filesystem, not the local machine:
| |
Get help
Run ? or help inside an SFTP session. For full documentation, see man sftp on the client.
| |
Exit the SFTP session
Use bye, exit, or quit:
| |
Verifying the setup
- SSH/SFTP:
systemctl status sshshould show the SSH service running; port 22 should be listening. - Connect:
sftp [email protected](or your server IP) should prompt for password and then show thesftp>prompt. - Chroot: After login,
pwdshould show/sftpuser1(or similar); you should not be able to access paths outside the chroot.
Summary
You installed the OpenSSH server on Ubuntu 20.04, created an SFTP-only user and group, and configured sshd with a Match block so that group is chrooted under /home and limited to SFTP. You can connect with sftp user@host, then use put/get, mput/mget, mkdir/rmdir, rm, and rename for file operations. For more SFTP client commands, see How to work with SFTP client in Linux.