How to Use the scp Command in Linux (Easy Guide)
In this article
The scp (secure copy) command in Linux lets you copy files and directories between your local machine and a remote server, or between two remote servers, over an SSH connection. It’s essentially cp but across a network, and it encrypts everything in transit. You don’t need any special software on the remote machine—just SSH access. This guide walks you through the most common uses, with clear examples, common pitfalls, and fixes.
What You'll Need
- A Linux (or macOS) local machine with
scpinstalled (it comes with OpenSSH, usually preinstalled). - The remote server’s hostname or IP address.
- A valid username and password or SSH key for the remote server.
- SSH service running on the remote server (port 22 by default).
Step-by-Step
- Open a terminal. On most Linux distributions, press Ctrl+Alt+T or search for “Terminal” in your apps menu.
- Understand the basic syntax:
The source and destination can be local paths (likescp [options] source destination/home/user/file.txt) or remote paths in the formusername@host:/path/to/file. - Copy a file from local to remote:
This copiesscp /home/user/file.txt user@server.example.com:/home/user/file.txtto the remote user’s home directory. You’ll be prompted for the remote password unless you have SSH keys set up. - Copy a file from remote to local:
This downloads the file to your current local directory.scp user@server.example.com:/home/user/file.txt /home/user/ - Copy an entire directory: Use the
-r(recursive) option:
This copies the wholescp -r /home/user/mydir user@server.example.com:/home/user/mydirfolder and its contents. - Use a specific SSH port: If the remote server uses a non-default port (e.g., 2222), add
-P(capital P):
Note: Forscp -P 2222 file.txt user@server.example.com:/home/user/sshit’s lowercase-p, but forscpit’s uppercase-P. - Preserve file timestamps and permissions: Use
-p(lowercase):scp -p file.txt user@server.example.com:/home/user/ - Copy between two remote servers: You can specify both source and destination as remote paths:
This will transfer the file from server1 to server2 through your local machine (the data passes through your computer, so it’s not a direct server-to-server transfer).scp user1@server1.com:/path/file.txt user2@server2.com:/path/ - Use SSH keys for passwordless transfers: If you have an SSH key set up,
scpwill use it automatically. To generate a key, runssh-keygen -t rsaand then copy the public key to the remote server withssh-copy-id user@server.example.com. After that,scpwon’t ask for a password.
Common Mistakes
- Forgetting the colon in remote paths:
scp file.txt user@server.com/home/user/is wrong—you need a colon after the host:user@server.com:/home/user/. - Using lowercase
-pfor port: For port, it’s-P. Lowercase-ppreserves file attributes. - Not using
-rfor directories: Trying to copy a folder without-rwill give an error like “omitting directory”. - Permission issues on destination: If you don’t have write permission in the remote directory, you’ll get a “Permission denied” error. Use a directory you own, or use
sudoon the remote side (whichscpcannot do directly—you’d need to copy to a writable location and then move it). - Mixing up source and destination order: The first path is the source, the second is the destination. Swapping them can overwrite files unintentionally.
Tips
- Use
-v(verbose) to see detailed debugging info if something goes wrong. - Use
-Cto enable compression, which speeds up transfers of text files over slow connections. - For large transfers, consider using
rsyncinstead—it resumes interrupted transfers and can be more efficient. - Always double-check the destination path—
scpwill happily overwrite an existing file without warning. - If you’re copying many files, consider tarring them first:
tar czf - folder | ssh user@host 'tar xzf - -C /dest'can be faster thanscp -rfor many small files.
Troubleshooting
If scp doesn’t work as expected, try these in order:
- Check SSH connectivity: Run
ssh user@server.example.comto see if you can log in. If SSH fails,scpwill too. Fix SSH first. - Check hostname and username: Typos are common. Verify the hostname resolves:
ping server.example.comornslookup server.example.com. - Check port and firewall: If you’re using a non-standard port, make sure it’s open on the remote server’s firewall. Try
telnet server 2222to test. - Check permissions on the source file: You need read permission on the local file and write permission on the remote directory. Use
ls -lto check. - Try verbose mode: Run
scp -v file user@server:/destand look for error messages like “Connection refused” or “Permission denied”. - If you get “Host key verification failed”: The remote host’s key has changed. Remove the old key from
~/.ssh/known_hosts(find the line with the hostname) and try again. - If the transfer stalls: Check your network connection, and try with
-Cfor compression or reduce the window size with-l limit(in kbit/s).
Frequently Asked Questions
Can I use scp with a Windows machine?
Yes, Windows 10 and later include OpenSSH, so scp works in PowerShell or Command Prompt. You can also use tools like WinSCP or PuTTY’s pscp for a graphical or command-line alternative.
How do I copy a file to a remote server without typing a password?
Set up SSH key authentication: generate a key pair with ssh-keygen, then copy the public key to the server using ssh-copy-id user@server. After that, scp will use the key automatically.
What’s the difference between scp and rsync?
scp is simpler and copies everything every time. rsync can check for differences and only transfer changed parts, which is faster for large or repeated transfers. rsync also supports resuming interrupted transfers.
Can I copy a file between two remote servers directly?
Yes, but the data passes through your local machine, so it’s not a direct server-to-server transfer. For a true direct transfer, you’d need to log into one server and use scp from there.
Why do I get “Permission denied (publickey)”?
This usually means the server doesn’t accept your password or key. Check that you’re using the correct username, that the server allows password authentication, or that your public key is in the server’s ~/.ssh/authorized_keys file.
Is scp secure?
Yes, scp encrypts all data during transfer using SSH encryption. It’s as secure as SSH itself. However, note that scp is being deprecated in some environments in favor of rsync or sftp, but it still works fine on most systems.