skip to content

SSH Subsystem Integration

SFTP starts as the sftp subsystem on one SSH channel and frames length-prefixed requests over it; it is not FTP tunnelled through SSH. Interviewers ask why that passes firewalls FTP fails.

on this pageshow

questions

4

Is SFTP just FTP tunnelled through SSH, and why does an SFTP partner link need only TCP port 22 through the firewall?

level: juniorimportance: must knowfreq 55%

answer

  1. one connection, not two
  2. a session channel, then a request
  3. the subsystem named sftp
  4. no ports inside the payload

basics

~20 s

SFTP is not FTP over SSH: it is its own binary protocol, started as the sftp subsystem on an SSH session channel. Every request and reply travels inside one encrypted TCP connection, normally to port 22, with no second connection or negotiated port.

solid answer

~40 s

SFTP is not FTP inside SSH. It is a separate file-transfer protocol, an Internet-Draft (`draft-ietf-secsh-filexfer`) of which OpenSSH speaks version 3, that runs *inside* an SSH connection: after authentication the client opens a `session` channel and sends a `subsystem` request named `sftp`, and the server starts its SFTP server on that channel. From then on every open, read, write and listing is a length-prefixed SFTP packet carried as channel data over the same TCP connection to port 22. FTP instead keeps a control connection on port 21 and opens separate data connections whose addresses it negotiates in its commands, which is why firewalls need port ranges or helpers for it. For SFTP the rule is one inbound TCP port, and nothing in the payload asks a middlebox to open another.

code

pseudocode · 9 lines
pseudocode
C -> S  TCP connect to port 22, SSH key exchange, user authentication
C -> S  SSH_MSG_CHANNEL_OPEN "session"
S -> C  SSH_MSG_CHANNEL_OPEN_CONFIRMATION
C -> S  SSH_MSG_CHANNEL_REQUEST "subsystem" want-reply "sftp"
S -> C  SSH_MSG_CHANNEL_SUCCESS
C -> S  SSH_MSG_CHANNEL_DATA [ length | SSH_FXP_INIT | version 3 ]
S -> C  SSH_MSG_CHANNEL_DATA [ length | SSH_FXP_VERSION | 3 | extensions ]
C -> S  SSH_MSG_CHANNEL_DATA [ length | SSH_FXP_OPEN | id 1 | ... ]
# every later request and all file data stay on this one channel

go deeper

for a junior

Know that SFTP is its own protocol running inside SSH on one TCP connection to port 22, and that it is neither FTP nor FTPS.

for a middle

Walk the start-up: a session channel, a subsystem request named sftp, SSH_FXP_INIT and SSH_FXP_VERSION, then length-prefixed requests carried as channel data.

for a senior

Turn the mechanism into the firewall answer: one inbound port, no negotiated data ports, no addresses in the payload, nothing for a protocol helper to rewrite.

for a principal

Explain why moving a partner from FTP to SFTP removes a whole class of firewall and NAT exceptions, and what you still own afterwards: host keys, accounts and confinement.

## Two protocols that share three letters **SFTP**, the SSH File Transfer Protocol, is a binary request-and-response protocol for file operations: open a file, read and write blocks, list a directory, rename, remove. It was specified in the IETF's `draft-ietf-secsh-filexfer` series, which never became an RFC. OpenSSH's `sftp` client and its `sftp-server` implement **version 3** of it (draft `-02`), plus extensions OpenSSH documents itself; the draft series ended at version 6 (draft `-13`), which few servers speak. **FTP** (RFC 959) is a much older protocol of text commands and numeric replies, and **FTPS** (RFC 4217) is FTP secured with TLS. Neither shares anything with SFTP's wire format: SFTP borrows no FTP command, no reply code and no connection model. The shared letters are history, not lineage. So "SFTP is FTP tunnelled through SSH" is wrong in both halves. It is not FTP, and it is not tunnelled in the port-forwarding sense: it is an application that runs as a program at the far end of an SSH channel. ## How an SFTP session actually starts 1. The client opens one TCP connection to the server, normally to port 22 (RFC 4253, section 4.1). 2. SSH's transport layer agrees on encryption and proves the server's host key, and the user authenticates. Those steps belong to the material on key exchange and authentication. 3. The client sends `SSH_MSG_CHANNEL_OPEN` for a channel of type `session` (RFC 4254, section 6.1). 4. On that channel it sends `SSH_MSG_CHANNEL_REQUEST` of type `subsystem` with the name `sftp` (RFC 4254, section 6.5). The server maps the name to an SFTP server; OpenSSH's sshd does it with the `Subsystem` directive in `sshd_config`. 5. The client sends `SSH_FXP_INIT`, the server answers `SSH_FXP_VERSION`, and file requests follow, each carried as `SSH_MSG_CHANNEL_DATA` on the same channel. The SFTP packets themselves are simple: a 32-bit **length**, a one-byte **type** and, for requests, a 32-bit **request id** chosen by the client, which the reply echoes. Because replies are matched by id, a client can keep many requests in flight on the one channel, so uploads, downloads and listings all share it. ## Why FTP needs more than one port FTP separates **control** from **data**. Commands travel on a control connection to port 21; each file transfer or directory listing uses a separate **data connection** whose address is agreed on the control connection. With `PORT` the client names an address for the server to dial; with `PASV` the server names one for the client to dial. A firewall in front of an FTP server therefore has to allow a range of data ports, or run a helper that reads the control connection to learn which port comes next, and NAT has to rewrite the addresses inside those commands. Active and passive mode, and the helpers that repair them, are covered in the FTP material. ## What the firewall team actually opens | Property | FTP | SFTP | |---|---|---| | TCP connections per session | one control, plus one per transfer or listing | one | | Well-known port | 21 for control | 22 | | Data ports | negotiated per transfer with `PORT` or `PASV` | none: file data is channel data | | Addresses inside the payload | yes, in `PORT` and the `PASV` reply | no | | Encryption | none in plain FTP | the whole SSH connection | For the partner link the request to the firewall team is therefore short: - **Destination:** the SFTP server's address, TCP port 22 (or whichever port its sshd listens on). - **Source:** the partner's published addresses, if you restrict by source at all. - **Return traffic:** allowed statefully for that one connection. - **No helper and no port range:** there is nothing to track beyond the single TCP connection. That is one inbound TCP port to the SFTP server, plus return traffic for that connection. Nothing in SFTP asks a middlebox to open a second flow, and because everything after key exchange is encrypted, a middlebox could not read such a request anyway. ## Confusions to avoid - **SFTP is not FTPS.** FTPS keeps FTP's two-connection model and adds TLS; SFTP has no FTP commands and no TLS. - **Forwarding port 21 through SSH does not turn FTP into SFTP.** A local forward carries only the control connection; the data connections that `PORT` or `PASV` negotiate are still opened directly between the hosts. - **SFTP is not an RFC.** Cite it as an Internet-Draft, and remember that OpenSSH speaks version 3. - **One port does not mean one file at a time.** Many SFTP requests share the channel, and SSH can multiplex further channels over the same TCP connection (RFC 4254). - **Subsystems are a general mechanism.** `sftp` is the best-known name, but other protocols, NETCONF for network devices among them, start the same way under their own names.

  • Could you get SFTP's single-port behaviour by forwarding FTP's port 21 through an SSH tunnel?
    No. A local forward carries only the control connection to port 21. The data connections that `PORT` or `PASV` negotiate are separate TCP flows, still opened directly between client and server outside the tunnel, so FTP keeps needing its data ports. Only SFTP puts the file data itself inside the SSH channel.
  • How can several simultaneous transfers share one SFTP connection?
    Every SFTP request carries a 32-bit id chosen by the client, and the server echoes it in the reply, so a client can send many requests before the first answer arrives and match replies as they come back. All of them ride as data on one SSH channel, and SSH can multiplex further channels over the same TCP connection.

  • SFTP is just FTP tunnelled through an SSH connection
  • SFTP and FTPS are two names for the same protocol
  • SFTP needs a passive data port range just like FTP
  • SFTP is defined by a standards-track RFC
  • SFTP opens a new TCP connection for every file it transfers
open as a page

In OpenSSH's sshd_config, how does `Subsystem sftp internal-sftp` differ from `Subsystem sftp /usr/libexec/sftp-server`, and when would you choose each?

level: middleimportance: should knowfreq 30%

basics

~20 s

Both answer the sftp subsystem request. A sftp-server path makes sshd run a separate program through the user's login shell; internal-sftp runs the same server inside sshd with no binary, libraries or shell, the usual choice for chrooted transfer accounts.

open as a page

An OpenSSH SFTP account fails immediately after login once a colleague added a greeting to the account's shell startup file; why, and what is the fix?

level: seniorimportance: should knowfreq 22%

basics

~20 s

With an external sftp-server, sshd starts the server through the user's shell, so the greeting enters the SFTP channel first. The client reads those bytes as a packet length and aborts. Print only in interactive shells, or use internal-sftp.

open as a page

When an SFTP session starts over SSH, what do SSH_FXP_INIT and SSH_FXP_VERSION carry, and how does a client learn which extensions an OpenSSH server supports?

level: middleimportance: nice to knowfreq 13%

basics

~20 s

The client's SSH_FXP_INIT carries its protocol version; the server's SSH_FXP_VERSION answers with the version to use (3 for OpenSSH) and a list of extension name and data pairs. A client calls only listed extensions, through SSH_FXP_EXTENDED.

open as a page