SkillPilotAI

scp

scp is the one-shot file copy command of OpenSSH: it copies files between local and remote hosts with the same login and encryption as ssh. It is the quickest way to move one or a few files to or from a server.

143.1 What it does, where it runs

  • One-shot copy over SSH; since OpenSSH 9.0 it uses the SFTP protocol inside.
  • Windows 10 (1809+) / 11: OpenSSH Client. Linux: openssh-client (apt) / openssh-clients (dnf). macOS: built in.
  • Lab: laptop 192.168.163.10, srv1 192.168.163.20, srv2 192.168.163.30.

143.2 How it works

Upload, download and remote-to-remote
  1. 1.Rule: the path with host: in front is remote, everything else is local

  2. 2.Upload: laptop -> encrypted SSH connection -> file saved on srv1

  3. 3.Download: same path in the reverse direction

  4. 4.Remote to remote (default / -3): srv1 -> laptop -> srv2

  5. 5.-R: srv1 connects to srv2 itself (srv1 needs a login on srv2)

  6. 6.-R -A: laptop's agent is forwarded, so srv1 can log in to srv2

143.3 Source and target forms

  • user@host:path is remote; no path after the colon means the user's home directory.
  • Many sources can go into one target folder.
  • URI form scp://user@host:port/path: the first slash is only a separator, so use // for an absolute path.
  • IPv6: put the address in square brackets, e.g. 'dev@[fe80::...%eth0]:/tmp/'.
Terminal
scp report.txt dev@srv1:/tmp/scp dev@srv1:/etc/os-release dl/scp report.txt dev@srv1:scp a.txt b.txt dev@srv1:/tmp/
Output
$ ssh dev@srv1 'ls /tmp/*.txt'/tmp/a.txt/tmp/b.txt/tmp/report.txt/tmp/uri.txt
Terminal
scp report.txt scp://dev@srv1:2222/tmp/uri.txt     # tmp/ inside home: errorscp report.txt scp://dev@srv1:2222/uri.txt         # home directoryscp report.txt scp://dev@srv1:2222//tmp/uri.txt    # absolute /tmpscp -6 report.txt \  'dev@[fe80::f013:d6ff:fe18:80d8%eth0]:/tmp/'

143.4 Spaces, wildcards and colons in names

  • Default SFTP mode: the remote path needs only the local shell's quotes.
  • Legacy -O mode runs the path through the remote shell: a space gives ambiguous target unless you add inner double quotes.
  • Quote a remote wildcard ('dev@srv1:/tmp/*.txt') so the local shell does not expand it.
  • A local file with a colon in its name needs ./ in front, otherwise scp treats it as a host.
Terminal
scp 'my report.txt' 'dev@srv1:/tmp/my report.txt'scp -O 'my report.txt' 'dev@srv1:/tmp/old report.txt'     # ambiguous targetscp -O 'my report.txt' 'dev@srv1:"/tmp/old report.txt"'   # worksscp 'dev@srv1:/tmp/*.txt' logs/scp ./name:with:colons.txt dev@srv1:/tmp/

143.5 Remote to remote copy

  • No option and -3: srv2's log shows the login from 192.168.163.10 (the laptop), so data went through the laptop.
  • -R: srv1 connects itself; without a key for srv2 it gets Permission denied.
  • -R -A: forwarding the laptop's agent fixes it; srv2's log shows the login from 192.168.163.20.
Terminal
scp dev@srv1:/tmp/db.sql dev@srv2:/tmp/db1.sqlscp -3 dev@srv1:/tmp/db.sql dev@srv2:/tmp/db3.sqlscp -R dev@srv1:/tmp/db.sql dev@srv2:/tmp/    # Permission deniedeval $(ssh-agent -s); ssh-add -qscp -R -A dev@srv1:/tmp/db.sql dev@srv2:/tmp/
Output
$ scp -R -A dev@srv1:/tmp/db.sql dev@srv2:/tmp/dbR.sqlrc=0[srv2] Accepted publickey for dev from 192.168.163.20[srv2] ls /tmp/db*/tmp/db1.sql  /tmp/db3.sql  /tmp/dbR.sql

143.6 Real errors

  • No such file or directory: the remote file does not exist.
  • Permission denied when writing to /root: SSH login is fine, but the file permission on the server is missing.
  • not a regular file: you copied a folder without -r.
Terminal
scp dev@srv1:/tmp/nope.txt .        # No such file or directoryscp report.txt dev@srv1:/root/      # Permission denied (file permission)scp project dev@srv1:/tmp/          # folder without -r
Output
$ scp project dev@srv1:/tmp/scp: local "project" is not a regular filescp: failed to upload file project to /tmp/

143.7 Options

OptionMeaning
-rCopy folders recursively
-pPreserve modification time and mode (takes no value)
-P 2222Port (capital P)
-i keyPrivate key to use
-BBatch mode: never prompt, fail directly
-l 8000Bandwidth limit in Kbit/s
-C / -q / -vCompression / no progress line / verbose
-3 / -R / -ARemote copy via laptop / srv1 connects itself / agent forwarding
-OLegacy SCP protocol (default is SFTP)
-J / -F / -4 / -6Jump host / ssh config file / IPv4 only / IPv6 only
-o Option=valAny ssh_config option
-X nrequests / bufferParallel SFTP requests / buffer size
Terminal
scp -r project dev@srv1:/tmp/touch -d '2025-01-15 10:30:00' report.txtscp report.txt dev@srv1:/tmp/plain.txt     # gets current timescp -p report.txt dev@srv1:/tmp/kept.txt   # keeps 2025-01-15 10:30scp -P 2222 report.txt dev@srv1:/tmp/
Terminal
scp -v -i ~/.ssh/id_work -o IdentitiesOnly=yes \  report.txt dev@srv1:/tmp/scp -B -i ~/.ssh/id_work -o IdentitiesOnly=yes \  report.txt dev@srv2:/tmp/
Output
$ scp -B -i ~/.ssh/id_work ... dev@srv2:/tmp/dev@srv2: Permission denied (publickey,password,...).scp: Connection closedrc=255
Terminal
head -c 2000000 /dev/urandom > big.bintime scp -q big.bin dev@srv1:/tmp/           # about 1 stime scp -q -l 8000 big.bin dev@srv1:/tmp/   # 8000 Kbit/s: about 3 sscp -v -C report.txt dev@srv1:/tmp/          # zlib compressionscp big.bin dev@srv1:/tmp/
Output
$ scp big.bin dev@srv1:/tmp/big.bin        100% 1953KB  12.7MB/s   00:00$ scp -q big.bin dev@srv1:/tmp/(no progress line)

Protocol: -v shows Sending subsystem sftp. A server without the SFTP subsystem (old router, embedded device) gives subsystem request failed; -O falls back to the old scp -t protocol.

Terminal
scp -v report.txt dev@srv1:/tmp/        # Sending subsystem: sftpscp -O -v report.txt dev@srv1:/tmp/     # runs scp -t on the serverscp -P 2223 report.txt dev@srv2:/tmp/   # subsystem request failedscp -O -P 2223 report.txt dev@srv2:/tmp/legacy.txt
Jump host with -J
  1. 1.srv2 port 2201 accepts logins only from srv1 (private server)

  2. 2.Direct scp -P 2201 from the laptop: Permission denied

  3. 3.scp -J dev@srv1: laptop first connects to srv1

  4. 4.The SSH connection to srv2 is tunnelled through srv1

  5. 5.srv2 log: Accepted publickey from 192.168.163.20 (srv1)

Terminal
scp -P 2201 report.txt dev@srv2:/tmp/          # Permission deniedscp -J dev@srv1 -P 2201 report.txt dev@srv2:/tmp/jump.txtscp -F /root/lab.conf report.txt work:/tmp/scp -4 -v report.txt dev@srv1:/tmp/
Output
$ scp -4 -v report.txt dev@srv1:/tmp/debug1: Connecting to srv1 [192.168.163.20] port 22.debug1: Connection established.

143.8 ssh options with scp -o

  • -o passes any ssh_config option to scp.
  • StrictHostKeyChecking=yes: unknown server gives Host key verification failed; accept-new saves the key the first time.
  • ConnectTimeout=2 on an unused IP: failed after exactly 2 seconds.
  • Connection: BatchMode, ConnectionAttempts, ProxyJump, ServerAliveInterval, ControlMaster/ControlPath/ControlPersist.
  • Auth: IdentityFile, IdentitiesOnly, PasswordAuthentication=no, PreferredAuthentications=publickey.
  • Host keys / crypto: UserKnownHostsFile, HostKeyAlgorithms, Ciphers, MACs, KexAlgorithms (legacy algos only when really needed).
Terminal
scp -o StrictHostKeyChecking=yes /tmp/t.txt dev@srv1:scp -o StrictHostKeyChecking=accept-new /tmp/t.txt dev@srv1:time scp -o ConnectTimeout=2 \  report.txt dev@192.168.163.99:/tmp/
Output
$ time scp -o ConnectTimeout=2 report.txt dev@192.168.163.99:/tmp/ssh: connect to host 192.168.163.99 port 22: Operation timed outscp: Connection closedreal    0m 2.03s

143.9 Comparison: scp vs sftp vs rsync

ToolBest forWhy
scpQuick one-shot copy of one or a few filesSingle command, same syntax as cp
sftpBrowsing and managing files on a serverInteractive ls, get, put, rm, batch files
rsyncBig or repeated folder syncSends only changed data (delta), --delete

143.10 Common mistakes

  • scp -p 2222 for a port: -p means preserve, so 2222 is treated as a local file. Use -P 2222 or the URI form.
  • scp://host:port/tmp/x expecting /tmp: you need // for an absolute path.
  • Copying a folder without -r.
  • Believing scp is insecure: the old protocol's problem was shell filename handling, not encryption; OpenSSH 9+ uses SFTP inside.
  • Scripts without -B / BatchMode: they hang on a password prompt.

143.11 Interview tips

  • The path with host: is remote; everything else is local.
  • Since OpenSSH 9, scp uses SFTP; -O forces the legacy SCP protocol for old devices.
  • Remote-to-remote goes through your laptop by default (-3); -R makes the source connect directly.
  • scp for quick copies, sftp for interactive work, rsync for big or repeated syncs.