How-to

How SFTP Works Inside an SSH Connection

SFTP looks like a simple file browser, but underneath it is a compact request and response protocol running in an SSH channel. Knowing its shape explains its speed and its limits.

Where does SFTP start?

Inside an established SSH connection. After the usual key exchange and login, the client opens a session channel and, instead of asking for a shell, sends a "subsystem" request named sftp. The server starts its SFTP component for that channel, and from then on the channel carries binary SFTP packets instead of terminal text. Encryption, integrity and authentication all come from SSH; SFTP itself only defines file operations. The SSH layers underneath are described in How SSH works.

Which server component answers?

Whatever the Subsystem sftp line in sshd_config names. Two choices exist in OpenSSH:

SettingWhat runsTypical use
/usr/lib/openssh/sftp-server (Debian, Ubuntu)A separate helper programDefault on Debian-family images
/usr/libexec/openssh/sftp-server (Enterprise Linux)The same helper, other pathDefault on Rocky, AlmaLinux, CentOS Stream
internal-sftpBuilt into sshd's session processChrooted accounts, no helper binary needed

internal-sftp matters for chroot jails, where the helper binary would not exist inside the jail. Setting one up is shown in SFTP vs SSH.

How do client and server agree on a version?

The client sends an INIT packet with the version it speaks; the server replies with VERSION and a list of extensions. OpenSSH speaks version 3, from an IETF draft that never became a final standard but became the de facto one. Later drafts defined versions up to 6, yet nearly every client and server in use today speaks version 3 plus extensions, which is why compatibility is so good.

What do the operations look like?

Each request carries a request id and gets a response with the same id. Files and folders are accessed through handles, opaque tokens the server returns on open:

  • OPEN a file, receive a HANDLE.
  • READ handle, offset, length; receive DATA.
  • WRITE handle, offset, data; receive STATUS.
  • CLOSE the handle.
  • OPENDIR and READDIR for listings; STAT, SETSTAT, RENAME, REMOVE, MKDIR for metadata.

Because reads and writes name explicit offsets, resuming a transfer is natural: the client simply starts at the size already present, which is what reget and reput do.

Why is SFTP fast despite small packets?

Pipelining. The client does not wait for each response before sending the next request; it keeps many reads or writes in flight with different request ids. OpenSSH's client defaults to 64 outstanding requests of 32 KB each, adjustable with -R and -B:

sftp -R 128 -B 65536 mirror@198.51.100.12

On long paths, such as Seattle to Singapore, more outstanding requests keep the link busy despite the round-trip time. On short paths the defaults already fill a fast port.

Which extensions add features?

OpenSSH announces several extensions in its VERSION reply, including posix-rename@openssh.com for atomic renames that replace existing files, statvfs@openssh.com behind the df command, hardlink@openssh.com, fsync@openssh.com for forcing data to disk, limits@openssh.com for advertising packet sizes, and copy-data for server-side copies without moving data across the network. Clients use them when offered and fall back otherwise.

How can I watch the protocol?

sftp -vvv mirror@198.51.100.12 2>&1 | grep -iE 'sftp|extension|version' | head
grep -n '^Subsystem' /etc/ssh/sshd_config    # on the server: which component answers

Verbose client output lists the server's version and extensions. For server-side logging, add -l INFO to the Subsystem line, for example Subsystem sftp internal-sftp -l INFO, and file operations appear in the auth log with the user and path.

What does SFTP not do?

It does not compare files or transfer only differences, which is rsync's job; it does not offer a server-side search; and it has no standard way to set quotas, which belong to the file system. Everyday use is covered in Use SFTP step by step, and the full command list in SFTP commands reference.

Keep Reading

Use SFTP step by step

Connect, browse, upload, download and resume with the sftp client, run batch jobs and use graphical clients on Windows and macOS.

Open

What is SFTP?

The SSH File Transfer Protocol in plain terms: what it secures, how it compares with FTP, FTPS and SCP, and where it fits on a server.

Open

SFTP vs SSH

SFTP is a file transfer subsystem that rides inside SSH; learn when to hand someone SFTP only and how to jail them.

Open

How SSH works

Follow one SSH connection from the TCP handshake through key exchange, host verification and login to channels and forwarding.

Open

FAQ

Quick Answers

No. It runs as a subsystem inside the SSH server, started per session.

Version 3, plus OpenSSH extensions such as posix-rename and statvfs.

An SFTP server built into sshd, needed for chroot jails because no helper binary has to exist inside.

The client pipelines many read and write requests at once; -R and -B tune how many and how large.

Add -l INFO to the Subsystem sftp line in sshd_config.