You want to send a configuration to a server, or bring back a log to analyse it at home. scp does that copy in one line, with the same syntax as cp: the source first, the destination second. It goes through SSH, so the transfer is encrypted and uses your keys and your ~/.ssh/config. Two surprises wait for beginners: a folder is refused without -r, and a file already on the server is overwritten without any question.
This page teaches these steps on a practice folder, checking the result on the server each time. For a repeated synchronisation, see rsync; for an interactive session, sftp.
What you will learn
- Set up a practice SSH server and connect to it with a key
- Send a file to a server and bring one back
- Copy a folder with
-rand predict where it arrives - Keep the date and the permissions of a file with
-p, without mixing up-pand-P, the port - Know that
scpoverwrites without asking - Read the
scperror messages and choose betweenscp,rsync,sftpandtar
Prerequisites
You know how to open a Linux terminal, move around with cd, list with ls and copy with cp. You can also change permissions with chmod (change permissions), search with find (find) and run a command on a server with ssh host 'command' (remote commands). Work with an ordinary account, not with root.
Prepare the practice server
scp needs an SSH server that accepts your key. If you do not have one, your own machine plays that role, with localhost as its address. Install the SSH server and start it (configure sshd details the service). Its name depends on the distribution: ssh on Debian and Ubuntu, sshd on AlmaLinux.
sudo apt install openssh-serversudo systemctl enable --now sshsudo dnf install openssh-serversudo systemctl enable --now sshdIf you do not have a key yet, create one with ssh-keygen -t ed25519 (create an SSH key). Then give your server a name in ~/.ssh/config (the SSH client explains the file). Replace moi ("me") with your account, and localhost with the address of your server if you have one.
Host srv HostName localhost User moiCopy your public key to the server, then connect once to accept its fingerprint (answer yes, then type exit):
ssh-copy-id srvssh srvIf your key has a passphrase, load it once into the agent with ssh-add, otherwise every scp asks for it again. Finally, check that ssh srv true prints nothing and does not ask for a password. If this command fails, scp srv: will fail too.
Create the practice folder
The examples use ~/scp-lab locally and a single folder on the server, scp-lab-srv. It is created first: if it already exists, the command stops and nothing else is created. Everything the page sends goes into it, and the final cleanup only deletes it. touch -d gives the files a date in 2020 so that -p shows. outil.sh has permissions 770, gros.bin weighs 1 MB, and scp-lab-srv/a.txt contains ANCIEN on the server. The names use French words, which the commands keep: outil is "tool", gros "big", ANCIEN "old", NOUVEAU "new", projet "project", bonjour "hello", fin "end", and later recu "received", envoye "sent", copie "copy", contenu "content", sans "without", avec "with", cle "key", retour "return", AJOUT "addition", mon "my", espace "space" and illisible "unreadable".
ssh srv mkdir scp-lab-srv && mkdir ~/scp-lab && cd ~/scp-lab && {mkdir -p projet/subprintf 'bonjour\n' > projet/t.txt; printf 'fin\n' > projet/sub/f.txtprintf 'NOUVEAU\n' > a.txt; printf 'echo ok\n' > outil.sh; chmod 770 outil.shhead -c 1000000 /dev/zero > gros.bintouch -d '2020-01-02 03:04' projet/t.txt projet/sub/f.txt a.txt outil.shssh srv 'printf "ANCIEN\n" > scp-lab-srv/a.txt'}The outputs on this page come from an OpenSSH 10.0p2 client (Debian 13) talking to an OpenSSH 10.2p1 server (Ubuntu); ssh -V prints your version. Stay in ~/scp-lab until "Clean up the practice folder". Remote paths without a leading / start from your home folder on the server.
The scp command in detail
An scp command is written in this order: the options, the source, then the destination. The srv: prefix designates the server; without it, the path is local. Read the line from left to right: each number points to the explanation of that piece.
Anatomy of the command
Syntaxscp [options] source destination
scp-r-p-P 22projetsrv:dest/ -
scpthe programIt copies over SSH. -
-rthe directoriesoptionalEnter subdirectories. -
-pthe datesoptionalKeep the date and permissions of the file. -
-P 22the portoptionalThe SSH one, with a capital P. -
projetthe sourceHere, on your machine. -
srv:dest/the destinationThe server, a colon, then the path.
Outputnothing: the copy succeeds
The 3 usage scenarios
The direction of the copy depends on no option: it depends on the position of the srv: prefix. On the left, you bring a file back; on the right, you send one. First bring back a.txt from the server under the name recu.txt, then send your a.txt under the name envoye.txt.
scp srv:scp-lab-srv/a.txt recu.txtcat recu.txtscp a.txt srv:scp-lab-srv/envoye.txtecho $?ssh srv ls scp-lab-srvANCIEN0a.txtenvoye.txtrecu.txt contains ANCIEN, the content of the file on the server. The upload prints nothing and returns code 0: a successful copy is silent. Always check on the server side, here with ssh srv ls, which shows envoye.txt next to a.txt.
Transfer between two remote servers
With two prefixes, scp copies from one server to another. The -3 option routes the data through your machine, which helps when the two servers cannot reach each other directly. It uses your bandwidth twice. Here both sides are srv, which is enough to see the syntax.
scp -3 srv:scp-lab-srv/a.txt srv:scp-lab-srv/via-local.txtThe command prints nothing and returns code 0. The OpenSSH manual says that going through your machine is the default behaviour: -3 makes it explicit, and -R asks for a direct copy between the two servers.
Forgetting -r for folders
scp refuses a folder without -r, and copies nothing. The message gives the cause on the first line and the failed operation on the second, and the return code is 1.
scp projet srv:scp-lab-srv/echo $?scp: local "projet" is not a regular filescp: failed to upload file projet to scp-lab-srv/1With -r, scp copies the folder and everything it contains. The folder itself arrives in the destination; check it with find on the server.
scp -r projet srv:scp-lab-srv/ssh srv 'cd scp-lab-srv && find projet -type f | sort'projet/sub/f.txtprojet/t.txtThe result depends on the destination: if it does not exist, the copy takes that name; if it exists, the source arrives inside it as a subfolder. Running the same command again therefore changes the result. Run it twice and compare the two lists:
scp -r projet srv:scp-lab-srv/copiessh srv 'cd scp-lab-srv && find copie -type f | sort'scp -r projet srv:scp-lab-srv/copiessh srv 'cd scp-lab-srv && find copie -type f | sort'copie/sub/f.txtcopie/t.txtcopie/projet/sub/f.txtcopie/projet/t.txtcopie/sub/f.txtcopie/t.txtOn the second run, copie existed: projet arrived in it as a subfolder, and the files from the first run stayed. With -r, a trailing slash (projet/) changes nothing, unlike rsync. To send the content without the folder, the source is projet/*: the shell replaces it with projet/sub and projet/t.txt before scp runs, and scp receives them as two sources.
The content of a folder, hidden files included
A colleague has to copy the whole content of projet to the server, hidden files included but without the projet folder itself, into scp-lab-srv/contenu/. They think they are done as soon as the command says nothing.
Prepare both sides
Add a hidden file .env containing secret to projet, and create the contenu folder in scp-lab-srv on the server.
Send the content like the colleague
Send the items of projet into contenu/, without the projet folder, using a shell wildcard.
Compare both sides
List projet locally and contenu on the server, hidden files included, and look for what is missing. Complete the copy, then compare again.
- scp -r
- ls -A
- ssh srv ls
A hint 1 / 1
A shell wildcard does not match names that start with a dot. ls -A shows them.
I have tried: show the solution
printf 'secret\n' > projet/.envssh srv mkdir scp-lab-srv/contenuscp -r projet/* srv:scp-lab-srv/contenu/ls -A projetssh srv ls -A scp-lab-srv/contenuscp projet/.env srv:scp-lab-srv/contenu/ssh srv ls -A scp-lab-srv/contenu.envsubt.txtsubt.txt.envsubt.txtThe shell expands projet/* without the hidden files: .env stays on your side, with no error message and with code 0. Only the comparison of the two ls -A reveals it. Sending projet/.env explicitly completes contenu, which then holds .env, sub and t.txt.
The -p vs -P trap
Both options exist and only differ by case. -P (uppercase) chooses the SSH Port. -p (lowercase) keeps the modification date and the permissions of the file. Without -p, the copy carries the current date. Check the dates on the server side (%TY prints the year of modification):
scp a.txt srv:scp-lab-srv/sans-p.txtscp -p a.txt srv:scp-lab-srv/avec-p.txtssh srv 'cd scp-lab-srv && find sans-p.txt avec-p.txt -printf "%TY %p\n"'2026 sans-p.txt2020 avec-p.txtLook at the first column: the year of the copy for sans-p.txt, the original year, 2020, for avec-p.txt. Permissions follow the same logic. outil.sh has permissions 770; %m prints those of the copies in octal.
scp outil.sh srv:scp-lab-srv/sans-p.shscp -p outil.sh srv:scp-lab-srv/avec-p.shssh srv 'cd scp-lab-srv && find sans-p.sh avec-p.sh -printf "%m %p\n"'750 sans-p.sh770 avec-p.shWithout -p, the permissions of the copy depend on the server: 750 here, instead of the original 770.
If you type -p 2222 thinking you give a port, scp reads 2222 as the name of a file to copy. The command fails, which limits the damage:
scp -p 2222 a.txt srv:scp-lab-srv/echo $?scp: stat local "2222": No such file or directory255The right way to write it is scp -P 2222 a.txt srv:scp-lab-srv/. If that port is not open on the server, the message comes from ssh and the code is also 255:
ssh: connect to host srv port 2223: Connection refusedscp: Connection closedThe name printed is the one of your server (with HostName localhost, you will read localhost).
Understand scp in 2 minutes
scp opens an SSH connection to srv: everything that travels between your machine and the server is encrypted. The map follows the copy of projet to srv:dest/ and shows the effect of each option; its dest/ stands for your scp-lab-srv/.
Before using it, predict three cases: scp -p projet srv:dest/ (without -r), then scp -r projet srv:dest/, then scp -r -p projet srv:dest/. What does dest/ hold at the end, and which dates show on the server? Then check on the map: look at the option cards, at the content of srv:dest/ and at the "Dates on the server" screen.
Mental model
How scp copies a directory
open the SSH connectionread one item of the sourcewrite it on the server for each file to copy
Try Source · projet
Connect, read, write
Content of srv:dest/
$ scp projet srv:dest/
- scp opens an SSH connection to srv. If ssh srv fails, scp fails too.
- projet is a directory. Without -r, scp refuses it and copies nothing.
- Nothing is copied, the exit code is 1.
What the animation shows
- The source on the left, the destination on the right, like
cp. The colon names the server. - A directory needs `-r`, and the trailing slash changes nothing.
- Without `-p`, the copy takes the current date.
- A file already there is overwritten, without asking.
Golden rules
Check SSH first
If this command fails, scp will fail:
ssh srv trueWrite the port with a capital P
Lower-case -p keeps the dates:
scp -P 2222 a.txt srv:dest/Essential vocabulary
Copy · what leaves or comes back
a.txt srv:- Send a file
scp a.txt srv:dest/; echo $?→ code 0 srv:a.txt- Bring a file back
scp srv:dest/a.txt recu.txt; cat recu.txt→ recu.txt projet/*- The content only
scp -r projet/* srv:dest/; ssh srv ls dest→ sub and t.txt "mon f.txt"- A name with a space
cp a.txt 'mon f.txt'; scp 'mon f.txt' srv:dest/; ssh srv ls dest→ arrived whole
Check · what happens on arrival
a.txt srv:- Overwrites without asking
scp a.txt srv:dest/; ssh srv cat dest/a.txt→ NOUVEAU projet srv:- A directory without -r
scp projet srv:dest/; echo $?→ code 1 srv:absent/- A missing directory
scp a.txt srv:absent/x.txt; echo $?→ code 1 -p 2222- The lower-case p
scp -p 2222 a.txt srv:dest/→ looks for a file named 2222
Silent overwrite without confirmation
scp overwrites the destination without asking anything: there is no equivalent of cp -i. The server has scp-lab-srv/a.txt with ANCIEN, and your a.txt contains NOUVEAU.
ssh srv cat scp-lab-srv/a.txtscp a.txt srv:scp-lab-srv/ssh srv cat scp-lab-srv/a.txtANCIENNOUVEAUThe old content is gone without a message. Before sending over an important file, bring it back first with scp srv:scp-lab-srv/a.txt a.txt.bak: that way you keep a local copy. The same risk exists the other way round: a download overwrites the local file of the same name.
Send, modify, bring back, compare
A colleague wants to modify a file directly on the server, then fetch it back home without losing the original file, and check what changed.
Send with the dates
Send a.txt into scp-lab-srv/ under the name copie.txt, keeping its date, and read the year of copie.txt on the server.
Modify on the server
Add the line AJOUT at the end of copie.txt, directly from your terminal with ssh.
Bring back under another name
Fetch copie.txt locally under the name retour.txt, without touching a.txt.
Compare
Compare a.txt and retour.txt, then count the lines of retour.txt.
- scp -p
- ssh srv
- diff
A hint 1 / 1
To add a line at the end of a file, the >> redirection does not overwrite it. diff compares two files, wc -l counts lines.
I have tried: show the solution
scp -p a.txt srv:scp-lab-srv/copie.txtssh srv 'cd scp-lab-srv && find copie.txt -printf "%TY %p\n"'ssh srv 'echo AJOUT >> scp-lab-srv/copie.txt'scp srv:scp-lab-srv/copie.txt retour.txtdiff a.txt retour.txtwc -l < retour.txt2020 copie.txt1a2> AJOUT2The upload with -p keeps the 2020 date on copie.txt. The change on the server adds a line, and retour.txt contains NOUVEAU then AJOUT, while a.txt did not move: scp only overwrites the destination.
Spaces in remote paths
The local shell splits a name at spaces. Put the name between single or double quotes, whether the file is local or remote:
cp a.txt 'mon f.txt'scp 'mon f.txt' srv:scp-lab-srv/ssh srv 'ls "scp-lab-srv/mon f.txt"'scp-lab-srv/mon f.txtThe whole name was passed on. To bring this file back, the remote path with a space is protected the same way:
scp 'srv:scp-lab-srv/mon f.txt' copie-espace.txtcat copie-espace.txtNOUVEAUAvoid -O
Without an option, scp transfers through the SFTP subsystem of OpenSSH (since version 9.0). With -O, it falls back to the old protocol: the shell of the server interprets the name of the remote file, so a space, a ; or a $(…) changes meaning there. Only use -O against an old server without SFTP, and never with a name you do not control.
Use a specific SSH key
For a script, use a dedicated key: it avoids a password in clear text and can be revoked without touching other accesses. -i chooses the key, as with ssh. Replace the path with the one of your key.
scp -i ~/.ssh/id_ed25519 a.txt srv:scp-lab-srv/cle.txtThe command prints nothing and returns code 0. To stop writing -i, put IdentityFile ~/.ssh/id_ed25519 in the Host srv block of ~/.ssh/config.
Compression and bandwidth
-C turns compression on: useful for text over a slow link, useless on an archive or an image that is already compressed. -l caps the rate, in Kbit/s and not in KB/s.
Unit of -l: Kbit/s, not KB/s
gros.bin weighs 1 MB, that is 8,000 Kbit. At 2,000 Kbit/s, the copy therefore takes about 4 seconds; at 8,000 Kbit/s, about 1 second. -q removes the progress bar, time measures the duration.
time scp -q gros.bin srv:scp-lab-srv/gros1.bintime scp -q -l 2000 gros.bin srv:scp-lab-srv/gros2.bintime scp -q -l 8000 gros.bin srv:scp-lab-srv/gros3.binreal 0m0,108sreal 0m3,997sreal 0m0,874sThe duration depends on the size and on the limit. Since 800 Kbit/s is about 100 KB/s, write -l 800, not -l 100, if you aim at 100 KB/s.
When to switch to rsync
scp suits a single copy. As soon as a copy repeats, or the file is big, rsync does better: it only copies what changed, it can simulate with -n and exclude files. After an interruption, scp starts again from zero, whereas rsync -P gros.bin srv:scp-lab-srv/ keeps the fragment already received and resumes from there. In return, rsync must be installed on both sides, whereas scp comes with the SSH client.
Which command should you choose?
The choice depends on how often the copy happens and on what you want to do on the server.
| Need | Command |
|---|---|
| Single copy of a file or a folder to a server | scp |
| Repeated backup or deployment, resume after an interruption | rsync |
| Browse a server, several files in one session | sftp |
| A compressed archive to send, with precise permissions | tar then SSH |
| Local copy | cp |
Troubleshooting
Start with the method, then look for your message in the table: each line gives the cause and the fix.
Diagnostic method
Run ssh srv true first: if the connection fails, look on the SSH side, not on the scp side. Otherwise run again with scp -v: the useful line is almost always the last one before the failure. The first line of the error names the path and the cause; the paths are those of the server, except local, which designates your machine.
Common errors
| Message | Cause | Solution |
|---|---|---|
scp: dest open "ro/a.txt": Permission denied | No write permission on the remote folder or file | Aim at an accessible folder, check with ssh srv ls -ld ro |
scp: dest open "/etc/a.txt": Permission denied | Writing into a system folder with an ordinary account | Copy into your home folder, then ssh -t srv sudo mv; never a root login to write into /etc |
scp: dest open "absent/x.txt": No such file or directory | Destination folder missing (with a destination absent/, the message is dest open "absent/": Failure) | Create the folder with ssh srv mkdir absent |
scp: open local "illisible.txt": Permission denied | Local file not readable | Read the local permissions with ls -l |
ssh: connect to host … port 2223: Connection refused, code 255 | Wrong port, sshd stopped or firewall | Check the port with -P |
subsystem request failed on channel 0, then scp: Connection closed (code 255) | The SSH server has no SFTP subsystem | On the server, Subsystem sftp internal-sftp in /etc/ssh/sshd_config, then restart the service |
WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! | The server key no longer matches known_hosts | Check with the administrator that the server was reinstalled, then ssh-keygen -R srv (the SSH client) |
zsh: no matches found: srv:scp-lab-srv/*.log | The local shell expands the wildcard before scp | Quote it: scp 'srv:scp-lab-srv/*.log' . |
No space left on device | Disk full on the destination | Free some space, check with ssh srv df -h |
| A local file was replaced by a download | scp overwrites without asking | Back up first: cp a.txt a.txt.bak |
Clean up the practice folder
Delete the local practice folder and the folder on the server, the only two the page created. With localhost, scp-lab-srv is in your home folder: re-read the name before running rm -r.
cd ~rm -r ~/scp-labssh srv rm -r scp-lab-srvKey points
- The source is on the left, the destination on the right; the
srv:prefix designates the server, and its position gives the direction of the copy. A successful copy is silent: check on the server side. - A folder needs
-r. A missing destination takes the name of the source, an existing destination receives it as a subfolder.projet/*does not send hidden files such as.env. -p(lowercase) keeps dates and permissions,-P(uppercase) chooses the port: a-p 2222is read as a file name.scpoverwrites without asking; only the permissions of the remote file prevent it (Permission denied).-lis expressed in Kbit/s, not in KB/s. Without an option,scpuses SFTP;-Ogives the remote name to the shell of the server, to be avoided.- To repeat or resume a copy, use rsync.
Knowledge check
Check that the essentials of this guide stuck. The questions cover only what has just been explained here.
Knowledge check
Check what you have learned with this interactive quiz
How it works
- The timer starts when you click Start
- Multiple choice, true/false and short answer questions
- You can move between questions
- Detailed results are shown at the end
Starts the quiz and the timer
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Transfer files with SFTP
A new full quiz with random questions
Work again only on the questions you missed