skip to content

SFTP

SFTP is its own request-and-handle protocol run as an SSH subsystem, reusing SSH's keys and single port. Interviewers treat it as the modern default and check it is not confused with FTPS or scp.

on this pageshow

explore

questions

14

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

For a CI job pushing files with OpenSSH's sftp, how do you pin the SFTP server's host key and what do BatchMode=yes and StrictHostKeyChecking=yes add?

level: middleimportance: must knowfreq 45%

basics

~20 s

Ship a known_hosts line for the server, obtained from its operator or checked against their fingerprint, and point UserKnownHostsFile at it; StrictHostKeyChecking=yes refuses any other key and BatchMode=yes turns every prompt into an immediate failure.

open as a page

In OpenSSH's sftp, how does batch mode with sftp -b change error handling, and how should a CI job's batch file use that?

level: middleimportance: must knowfreq 40%

basics

~20 s

With sftp -b, OpenSSH's sftp stops at the first failing file command (put, rename, rm, mkdir and others) and exits non-zero, so the CI step fails; a leading '-' tolerates one line's failure, and authentication must need no typing.

open as a page

With OpenSSH's sshd, how do you configure a group of partner accounts that can only use SFTP, each confined to its own directory?

level: middleimportance: must knowfreq 40%

basics

~10 s

Put the partners in one group and add a Match Group block to sshd_config with ChrootDirectory, ForceCommand internal-sftp and DisableForwarding; the chroot must be root-owned, so each partner writes into a subfolder it owns.

open as a page

A Paramiko SFTP upload script calls set_missing_host_key_policy(AutoAddPolicy()); what does that do, and how should Paramiko, JSch and ssh2-sftp-client check host keys instead?

level: juniorimportance: should knowfreq 30%

basics

~20 s

AutoAddPolicy makes Paramiko trust and store any host key it has not seen, so an impostor is accepted silently; load a pinned file and keep the default RejectPolicy, use JSch's setKnownHosts with StrictHostKeyChecking yes, and give ssh2-sftp-client a hostVerifier.

open as a page

For an OpenSSH SFTP-only partner account, what do the authorized_keys options restrict and from= add, and where should that key file live?

level: middleimportance: should knowfreq 26%

basics

~10 s

restrict disables forwarding, PTY allocation and ~/.ssh/rc for that key; from= accepts it only from listed addresses or CIDR ranges. Keep the file outside the partner's writable area, for example AuthorizedKeysFile /etc/ssh/authorized_keys/%u.

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

Your SFTP upload job sometimes leaves a bank's consumer reading half-written settlement files; how do you make each drop atomic and each retry safe?

level: seniorimportance: should knowfreq 28%

basics

~20 s

Upload under a temporary name the consumer ignores, close it, then rename it to the final name in one request; before retrying, check whether the final file already exists, because plain SFTP rename fails on a clash and posix-rename silently overwrites.

open as a page

Why does OpenSSH's sshd refuse a ChrootDirectory that the SFTP partner owns or that is group-writable, and how must the directory tree be laid out instead?

level: seniorimportance: should knowfreq 30%

basics

~20 s

sshd chroots as root on the user's behalf, so it requires every directory on the chroot path to be root-owned and not group- or world-writable; otherwise the user could reshape their own jail. Writable folders go one level below.

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

You must give forty carriers SFTP drop folders on OpenSSH — how do you weigh one shared account, per-carrier chrooted accounts and a dedicated drop host?

level: principalimportance: should knowfreq 14%

basics

~20 s

Give each carrier its own account in one chrooted, forced-SFTP group, with its own restricted key, and prefer a host or sshd instance whose global policy is the transfer policy; a shared account loses revocation, attribution and isolation.

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

After a CI image moved to a current OpenSSH, a script's scp to an older partner server fails; why, and when is scp -O the right fix?

level: seniorimportance: nice to knowfreq 14%

basics

~20 s

Since OpenSSH 9.0, scp speaks SFTP by default, so it fails on servers without an SFTP subsystem, on shell-style inner quoting, and on ~user paths an old sftp-server cannot expand; -O restores the legacy protocol for those servers only.

open as a page

How do you get per-file transfer logs and request limits from OpenSSH's internal-sftp for chrooted partners, and why doesn't sshd's LogLevel produce them?

level: seniorimportance: nice to knowfreq 12%

basics

~20 s

Give internal-sftp its own flags: -l INFO (or VERBOSE) and -f for the facility, because sshd's LogLevel and SyslogFacility do not apply to it; -P and -p deny or allow requests. Some systems also need /dev/log inside the chroot.

open as a page