Commands / Services and tunnels

Services and tunnels

sftp

Also SSH file transfer, secure FTP

An interactive file session over SSH: ls, get, put, rm, as with FTP but logged in and encrypted.

sftp USER@HOST opens a session that takes FTP-like commands (ls, cd, get, put). -b FILE runs commands from a file (-b - from standard input) and stops at the first error. Not the same as FTPS, which is FTP inside TLS.

sftp command usage

sftp [-b BATCHFILE] USER@HOST

Options

-b -
read commands from standard input, stop at an error
-o BatchMode=no
allow a password in batch mode (labs only)

sftp examples

printf 'ls -l\n' | sftp -b - backup@10.0.9.10
Lists the backup account's files.
sftp backup@10.0.9.10
Opens a session: then ls, cd, get FILE, put FILE, and lcd to move on your own side.
sftp -P 2222 admin@10.0.2.2
Uses the server's port 2222: capital -P, as with scp.
sftp -i ~/.ssh/backup_ed25519 backup@10.0.9.10
Logs in with that private key.
sftp backup@10.0.9.10:officeRouter.conf
Downloads one file into the current directory and quits, without a session.
printf 'put frr.conf\n' | sftp -b - backup@10.0.9.10
Uploads a file from a script, failing if the upload fails.
printf 'get -R configs\n' | sftp -b - backup@10.0.9.10
Downloads a whole directory: -R for recursive.
sftp -b nightly.txt backup@10.0.9.10
Runs the commands in nightly.txt and stops at the first that fails; a line starting with - may fail.
printf 'reget netlab.iso\n' | sftp -b - backup@10.0.9.10
Resumes a download that broke off instead of starting again.
printf 'mkdir archive\nrename officeRouter.conf archive/officeRouter-1.conf\n' | sftp -b - backup@10.0.9.10
Makes a directory and moves a file into it on the server.
sftp -J admin@bastion.lab admin@10.0.3.10
Reaches an inside server through the bastion host.
sftp -l 8000 backup@10.0.9.10
Caps transfers at 8000 Kbit/s.

Pitfalls

  • -b turns on BatchMode, which forbids asking for a password: use a key, or -o BatchMode=no with sshpass.

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Start with a free account: Google or email, no card.

Start free