CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Network Transfer & Tools / scp

scp

Copy files to and from servers over SSH

Beginnersshtransferremote

scp copies files between your machine and a server (or between two servers) over SSH, with the same keys, ports and ~/.ssh/config as ssh. It's the quickest way to push a tarball or pull a log; for big trees or repeated syncs use rsync instead.

Syntaxscp [-r] [-p] [-P PORT] [-i KEY] [-J JUMP] SOURCE... [USER@]HOST:DEST (or HOST:SRC LOCAL_DEST)

Watch it run

Push a build to an app server, then pull its error log back to your machine for a closer look.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
scp app.tar.gz [email protected]:/tmp/

Local file first, remote destination second. The progress line shows percent, size, speed and elapsed time. The trailing slash on /tmp/ makes the destination unambiguously a directory.

Anatomy of a command

$
commandscp

Secure copy. Opens an SSH connection and transfers the files inside it.

How it works

scp starts ssh as a subprocess to the remote host, so authentication, host key checks, ProxyJump and every ~/.ssh/config setting apply unchanged. Since OpenSSH 9.0 scp speaks the SFTP protocol by default: it asks the server for the sftp subsystem and then opens, writes and closes files with SFTP requests. The old behaviour (-O) instead ran 'scp -t' or 'scp -f' in a remote shell and streamed files with a simple rcp-style protocol, which let the remote shell expand wildcards and caused several security bugs. Either way scp copies every file completely each time: there is no comparison with what's already at the destination, no resume after an interruption and no delete of extra files. Paths with spaces need quoting for the remote side too. With -3, a copy between two remote hosts goes through your machine instead of directly between them.

Options you'll actually use

-rCopy directories recursively
-pPreserve modification times and modes
-P PORTRemote SSH port (capital P)
-i KEYIdentity (private key) file
-J user@bastionGo through a jump host
-CCompress during transfer (helps on slow links, text files)
-l KBITLimit bandwidth in Kbit/s
-qQuiet: no progress meter
-3Copy between two remote hosts through the local machine
-OUse the legacy SCP protocol instead of SFTP (old servers)
-vVerbose SSH debug output

Common mistakes

scp -p 2222 file host:/tmp/

In scp, -p means preserve times; the port option is capital -P (the opposite of ssh).

scp -P 2222 file host:/tmp/
scp file host (forgetting the colon)

Without a colon both sides are local, so scp silently creates a local file called 'host'.

scp file host: # or host:/path/
Using scp to redeploy a big directory again and again

scp re-sends every file in full and never deletes removed files, so it's slow and leaves stale files behind.

rsync -az --delete dist/ host:/var/www/site/
Restarting a huge scp from zero after a dropped connection

scp has no resume; the partial file is just overwritten.

rsync --partial --progress big.iso host:/data/ # or sftp reput

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    There's no remote host in the sandbox, but scp can copy locally. Copy ~/app to /tmp/app-copy recursively, preserving times.

  2. 2

    Write the command that downloads /var/log/nginx/error.log from 10.0.2.15 (user ubuntu, SSH port 2222) into the current directory.

  3. 3

    Write the command that uploads ~/app.tar.gz to /tmp on 10.0.2.15 through the jump host bastion.devops-lab.io.

Related commands

sshLog in to remote servers securelyrsyncSync files and directories efficientlysftpInteractive and scripted file transfer over SSHtarBundle files into an archive and extract them
PreviousxargsNextsftp
Linux / Network Transfer & Tools / scp

scp

Copy files to and from servers over SSH

Beginnersshtransferremote

scp copies files between your machine and a server (or between two servers) over SSH, with the same keys, ports and ~/.ssh/config as ssh. It's the quickest way to push a tarball or pull a log; for big trees or repeated syncs use rsync instead.

Syntaxscp [-r] [-p] [-P PORT] [-i KEY] [-J JUMP] SOURCE... [USER@]HOST:DEST (or HOST:SRC LOCAL_DEST)

Watch it run

Push a build to an app server, then pull its error log back to your machine for a closer look.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
scp app.tar.gz [email protected]:/tmp/

Local file first, remote destination second. The progress line shows percent, size, speed and elapsed time. The trailing slash on /tmp/ makes the destination unambiguously a directory.

Anatomy of a command

$
commandscp

Secure copy. Opens an SSH connection and transfers the files inside it.

How it works

scp starts ssh as a subprocess to the remote host, so authentication, host key checks, ProxyJump and every ~/.ssh/config setting apply unchanged. Since OpenSSH 9.0 scp speaks the SFTP protocol by default: it asks the server for the sftp subsystem and then opens, writes and closes files with SFTP requests. The old behaviour (-O) instead ran 'scp -t' or 'scp -f' in a remote shell and streamed files with a simple rcp-style protocol, which let the remote shell expand wildcards and caused several security bugs. Either way scp copies every file completely each time: there is no comparison with what's already at the destination, no resume after an interruption and no delete of extra files. Paths with spaces need quoting for the remote side too. With -3, a copy between two remote hosts goes through your machine instead of directly between them.

Options you'll actually use

-rCopy directories recursively
-pPreserve modification times and modes
-P PORTRemote SSH port (capital P)
-i KEYIdentity (private key) file
-J user@bastionGo through a jump host
-CCompress during transfer (helps on slow links, text files)
-l KBITLimit bandwidth in Kbit/s
-qQuiet: no progress meter
-3Copy between two remote hosts through the local machine
-OUse the legacy SCP protocol instead of SFTP (old servers)
-vVerbose SSH debug output

Common mistakes

scp -p 2222 file host:/tmp/

In scp, -p means preserve times; the port option is capital -P (the opposite of ssh).

scp -P 2222 file host:/tmp/
scp file host (forgetting the colon)

Without a colon both sides are local, so scp silently creates a local file called 'host'.

scp file host: # or host:/path/
Using scp to redeploy a big directory again and again

scp re-sends every file in full and never deletes removed files, so it's slow and leaves stale files behind.

rsync -az --delete dist/ host:/var/www/site/
Restarting a huge scp from zero after a dropped connection

scp has no resume; the partial file is just overwritten.

rsync --partial --progress big.iso host:/data/ # or sftp reput

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    There's no remote host in the sandbox, but scp can copy locally. Copy ~/app to /tmp/app-copy recursively, preserving times.

  2. 2

    Write the command that downloads /var/log/nginx/error.log from 10.0.2.15 (user ubuntu, SSH port 2222) into the current directory.

  3. 3

    Write the command that uploads ~/app.tar.gz to /tmp on 10.0.2.15 through the jump host bastion.devops-lab.io.

Related commands

sshLog in to remote servers securelyrsyncSync files and directories efficientlysftpInteractive and scripted file transfer over SSHtarBundle files into an archive and extract them
PreviousxargsNextsftp