sftp
sftp runs file operations (browse, upload, download, resume, batch) inside one encrypted SSH connection. It is the tool for interactive file work on a server and for scripted transfers with batch files.
144.1 What it does, where it runs
- All transfers ride on one SSH connection to TCP 22; no extra port (unlike FTP).
- Windows 10 (1809+) / 11: OpenSSH Client. Linux: openssh-client (apt) / openssh-clients (dnf). macOS: /usr/bin/sftp built in.
- Lab: client 192.168.164.10, srv 192.168.164.20 (OpenSSH 9.7p1).
144.2 How it works
- Step 1
1.Client opens a normal SSH connection to srv TCP 22
- Step 2
2.Login with a key: Authenticated (publickey)
- Step 3
3.Client requests the sftp subsystem (no new port)
- Step 4
4.get: file data flows server -> client
- Step 5
5.put: file data flows client -> server
144.3 Starting sftp: destination forms
| Form | Result |
|---|---|
| sftp user@host | Session starts in the home directory |
| sftp user@host:dir/ | Session starts in that folder |
| sftp://user@host:2222/www/ | URI form with port |
| sftp user@host:file [local] | Downloads the file and exits |
| sftp user@[2001:db8::10] | IPv6 in square brackets |
echo pwd | sftp -b - dev@192.168.164.20echo pwd | sftp -b - dev@192.168.164.20:logs/echo pwd | sftp -b - \ sftp://dev@192.168.164.20:2222/www/sftp dev@192.168.164.20:report.txtsftp dev@192.168.164.20:report.txt copy.txtsftp -q dev@192.168.164.20:nothere.txt# IPv6: sftp dev@[2001:db8::10]$ sftp -q dev@192.168.164.20:nothere.txtFile "/home/dev/nothere.txt" not found.(exit 1)144.4 Command-line options
- -i key: needed when the key is not a default name (drop user got Permission denied without it).
- -P port (capital); -o any ssh option; -F a separate config file (alias files below).
- -b file / -b - runs a batch and turns quiet mode on; -N turns quiet off again; -q hides the progress meter.
- -J jump host; -C compression (log shows zlib@openssh.com); -v verbose.
- -r whole folder, -p keep time and mode, -l Kbit/s limit, -B buffer size, -R parallel requests.
- -a resumes a broken download; -4/-6 address family, -c cipher, -f fsync, -X nrequests/buffer.
echo pwd | sftp -i ~/.ssh/id_drop -b - drop@192.168.164.20echo pwd | sftp -v -P 2222 -b - dev@192.168.164.20echo pwd | sftp -o Port=2222 -o User=dev -b - 192.168.164.20cat lab/configecho pwd | sftp -F lab/config -b - files$ cat lab/configHost files HostName 192.168.164.20 Port 2222 User dev IdentityFile ~/.ssh/id_ed25519 ConnectTimeout 5$ echo pwd | sftp -F lab/config -b - filessftp> pwdRemote working directory: /home/devSpeed: a 4 MB file took 1.4 s, 5.3 s with -l 8000 (8 Mbit/s); -B and -R help on high-latency links. All copies have the same md5:
echo 'get www' | sftp -r -p -b - dev@192.168.164.20time sftp -q dev@192.168.164.20:big.bin b1.bintime sftp -q -l 8000 dev@192.168.164.20:big.bin b2.bintime sftp -q -B 262144 -R 128 dev@192.168.164.20:big.bin b3.bin$ time sftp -q dev@192.168.164.20:big.bin b1.binreal 1.368s$ time sftp -q -l 8000 dev@192.168.164.20:big.bin b2.binreal 5.299s$ time sftp -q -B 262144 -R 128 dev@192.168.164.20:big.bin b3.binreal 1.519s$ md5sum b1.bin b2.bin b3.bin | cut -c1-12beddd2489249beddd2489249beddd2489249144.5 Resuming a broken transfer
- timeout killed a slow download after 2 s, leaving a partial file.
- sftp -a prints Resuming and fetches only the remaining bytes; md5sum matches.
- Inside the prompt reget = get -a and reput = put -a; reput completed the half upload (1044480 -> 2097152 bytes).
timeout 2 sftp -q -l 4000 dev@192.168.164.20:big.binsftp -a dev@192.168.164.20:big.binprintf 'lcd /tmp/dl\nreget big.bin\n' | sftp -b - dev@192.168.164.20timeout 3 sftp -q -l 4000 -b /root/b/putbig.txt dev@192.168.164.20sftp -b /root/b/reput.txt dev@192.168.164.20$ timeout 3 sftp -q -l 4000 -b /root/b/putbig.txt dev@192.168.164.20sftp> put up.bin[srv]$ ls -l /home/dev/up.bin-rw-r--r-- 1 dev dev 1044480 Nov 9 05:25 /home/dev/up.bin$ sftp -b /root/b/reput.txt dev@192.168.164.20sftp> reput up.bin[srv]$ ls -l /home/dev/up.bin-rw-r--r-- 1 dev dev 2097152 Nov 9 05:25 /home/dev/up.bin144.6 Batch file prefixes
- Step 1
1.sftp -b file runs commands line by line
- Step 2
2.A command fails (rm missing.log)
- Step 3
3.No prefix: sftp stops right there, exit code 1
- Step 4
4.- prefix (-rm): error printed, batch goes on, exit 0
- Step 5
5.@ prefix: command is not echoed; -@ combines both
# b/stop.txtcd logsrm missing.logls# b/keep.txtcd logs-rm missing.log@pwdls-@ls old*$ sftp -b b/keep.txt dev@192.168.164.20; echo "exit $?"sftp> cd logssftp> -rm missing.logremote delete /home/dev/logs/missing.log: No such file or directoryRemote working directory: /home/dev/logssftp> lsapp1.logapp2.logold.txtold.txtexit 0144.7 Interactive: navigation and listing
- Commands with l run locally: lpwd, lcd, lls, lmkdir, lumask.
- pwd / cd / ls work on the remote side; cd without a path goes back to the start folder.
- ls flags: -a hidden, -h readable sizes, -S by size, -t by time, -r reverse, -1 one per line, -n numeric IDs, -f no sort.
- df -h shows filesystem size and free space; df -i shows inodes.
pwdlpwdcd logslcd /tmp/tls -llls -lls -lals -lh big.binls -lSls -1 logsdf -h sharesftp> ls -l-rw-r--r-- ? dev dev 13 Nov 9 05:22 app1.log-rw-r--r-- ? dev dev 13 Nov 9 05:22 app2.log-rw-r--r-- ? dev dev 4 Nov 9 05:22 old.txtsftp> df -h share Size Used Avail (root) %Capacity 16.0MB 0BB 16.0MB 16.0MB 0%144.8 Interactive: transfers
| Command | Meaning |
|---|---|
| get / put | Download / upload (globs ok) |
| mget / mput | Many files with globs |
| get -p / put -p | Keep time and permissions |
| get -r / put -R | Whole folder |
| get -a / reget, put -a / reput | Resume |
| -f | fsync after transfer |
| progress | Meter on / off |
lcd /tmp/tget report.txtget -p notes.mdmget logs/*.logput local.txtmput *.csv upload/get -r wwwget missing.txtsftp> get -r wwwRetrieving /home/dev/wwwRetrieving /home/dev/www/csssftp> get missing.txtFile "/home/dev/missing.txt" not found.exit 1144.9 Interactive: file management and session
- mkdir, rename, ln -s (symlink), chmod, rm, rmdir; copy/cp copies on the server itself.
- chown/chgrp take numeric IDs; a normal user cannot give a file to root (Permission denied).
- rmdir on a non-empty folder gives Failure: rm the file first.
- version shows SFTP protocol version 3; !cmd runs a local command, ! opens a local shell; help or ?; bye/exit/quit leave.
mkdir backuprename notes.md backup/notes.mdln -s backup/notes.md latest.mdchmod 600 report.txt-chown 0 report.txt-put /etc/hostname /etc/hostname-rmdir backuprm backup/notes.mdrmdir backupsftp> -put /etc/hostname /etc/hostnamedest open "/etc/hostname": Permission deniedsftp> -rmdir backupremote rmdir "/home/dev/backup": Failuresftp> rm backup/notes.mdsftp> rmdir backupexit 0sftp> helpAvailable commands:bye Quit sftpget [-afpR] remote [local] Download fileput [-afpR] local [remote] Upload file!command Execute 'command' in local shell! Escape to local shell? Synonym for help144.10 ssh options with sftp -o
- -o takes any ssh_config keyword (61 listed): connection, authentication, host keys, crypto/multiplexing.
- ConnectTimeout=3 on an unreachable host: timed out. BatchMode=yes fails immediately on a wrong key (good for scripts).
- StrictHostKeyChecking=yes rejects an unknown host key; accept-new adds it to known_hosts.
- Block keywords like Host are config-file only.
echo pwd | sftp -o ConnectTimeout=3 -b - dev@192.168.164.99echo pwd | sftp -o BatchMode=yes -o IdentitiesOnly=yes \ -o IdentityFile=/root/.ssh/id_drop -b - dev@$Secho pwd | sftp -o UserKnownHostsFile=/tmp/kh \ -o StrictHostKeyChecking=accept-new -b - dev@$Secho pwd | sftp -o 'Host=files' -b - dev@$S$ echo pwd | sftp -o 'Host=files' -b - dev@192.168.164.20Host directive not supported as a command-line optionConnection closed144.11 Server side: chroot'd sftp-only user
- Step 1
1.drop tries ssh: key OK, but server says sftp connections only
- Step 2
2.drop connects with sftp: Match User drop applies
- Step 3
3.ForceCommand internal-sftp: no shell
- Step 4
4.ChrootDirectory /srv/drop: drop sees / (cd .. stays there)
- Step 5
5.put into root-owned / is denied, put into upload/ works
# srv: end of /etc/ssh/sshd_configMatch User drop ChrootDirectory /srv/drop ForceCommand internal-sftp AllowTcpForwarding no# clientssh -i ~/.ssh/id_drop drop@192.168.164.20 hostnamesftp -i ~/.ssh/id_drop -b b/drop.txt drop@192.168.164.20sftp> -put /tmp/t/local.txt /local.txtdest open "/local.txt": Permission deniedsftp> put /tmp/t/local.txt upload/sftp> ls -l upload-rw-r--r-- ? 1001 1001 11 Nov 9 05:26 upload/local.txt[srv]$ ls -l /srv/drop/upload-rw-r--r-- 1 drop drop 11 Nov 9 05:26 local.txt144.12 Common mistakes
- sftp -p 2222 for the port: -p means preserve; use -P 2222 or -o Port=2222 (works in ssh, scp and sftp).
- Batch scripts without the - prefix on optional commands: one harmless error stops the whole batch.
- Chroot directory not owned by root: sshd refuses it; keep a writable subfolder inside.
- Confusing host:dir/ (session) with host:file (download and exit).
144.13 Interview tips
- SFTP is a subsystem of SSH on port 22, not FTP over TLS (that is FTPS).
- Upload-only vendor: Match User + ForceCommand internal-sftp + root-owned ChrootDirectory + writable upload/ + keys; test ssh is refused and sftp works.
- Resume with sftp -a, reget or reput.
- Batch: -b file, - prefix to continue on errors, @ to hide echo.