Using SCP for File Transfer to Linux Servers
Published: January 11, 2026
SCP (Secure Copy Protocol) is a simple yet powerful command-line tool for securely transferring files between your local machine and remote Linux servers. Built on top of SSH, SCP provides encrypted file transfers without needing to set up FTP servers or other file-sharing services. Whether you’re deploying applications to a server, managing Raspberry Pi projects, or just need to move files around your network, SCP is an essential tool in your toolkit.
This guide covers the most common SCP use cases with practical examples you can use immediately.
Prerequisites
Before using SCP, you need:
- SSH access to your remote Linux server (Raspberry Pi, VPS, etc.)
- The remote server’s IP address or hostname
- Valid credentials (password or SSH key)
SCP comes pre-installed on macOS, Linux, and is available on Windows through PowerShell or WSL (Windows Subsystem for Linux).
Basic Syntax
The general syntax for SCP is:
scp [options] source destination
Remote locations are specified using the format: username@hostname:path
Authentication
When you run an SCP command, you’ll be prompted for authentication if you haven’t set up SSH key-based authentication. The prompt will look like this:
username@192.168.1.100's password:
Simply type your password and press Enter. Note that you won’t see any characters (not even asterisks) as you type - this is normal for security reasons.
For frequent transfers or automated scripts, setting up SSH key authentication (covered later in this guide) eliminates password prompts entirely.
Copying Files From Local to Remote
Copy a single file to a remote server:
scp myfile.txt username@192.168.1.100:/home/username/
This copies myfile.txt from your current directory to the remote server’s /home/username/ directory. If using password authentication, you’ll be prompted to enter your password before the transfer begins.
Copy a file and rename it on the remote server:
scp local-config.json username@192.168.1.100:/etc/app/config.json
This transfers local-config.json from your machine and saves it as config.json in the remote /etc/app/ directory.
Copy a file to your home directory on the remote server:
scp deployment.zip username@raspberrypi.local:~/
The ~/ shorthand refers to the remote user’s home directory. This is especially useful for Raspberry Pi projects where you’re frequently uploading code or configuration files.
Copying Files From Remote to Local
Download a file from a remote server:
scp username@192.168.1.100:/var/log/app.log ./
This copies app.log from the remote server to your current local directory.
Download a file and rename it locally:
scp username@192.168.1.100:/etc/config.json ./backup-config.json
This downloads the remote config.json and saves it as backup-config.json in your current directory.
Download from a Raspberry Pi using its hostname:
scp pi@raspberrypi.local:/home/pi/sensor-data.csv ~/Downloads/
This is particularly useful for Raspberry Pi projects where you need to retrieve logs, sensor data, or other files generated by your applications.
Copying Directories
Copy an entire directory to a remote server:
scp -r myproject/ username@192.168.1.100:/var/www/
The -r flag (recursive) tells SCP to copy the directory and all its contents. This is perfect for deploying web applications or uploading project files.
Download an entire directory from a remote server:
scp -r username@192.168.1.100:/var/backups/ ./local-backups/
This recursively downloads the entire /var/backups/ directory and all its subdirectories to your local machine.
Useful Options
Preserve file timestamps and permissions:
scp -p important-file.sh username@192.168.1.100:~/scripts/
The -p flag preserves modification times, access times, and file modes. This is important for scripts and executables where permissions matter.
Specify a custom SSH port:
scp -P 2222 myfile.txt username@192.168.1.100:~/
If your SSH server runs on a non-standard port (not port 22), use the -P flag (note: uppercase P for SCP, unlike ssh which uses lowercase -p).
Use verbose output for troubleshooting:
scp -v myfile.txt username@192.168.1.100:~/
The -v flag shows detailed information about the transfer process, helpful for debugging connection issues.
Limit bandwidth usage:
scp -l 1000 large-file.zip username@192.168.1.100:~/
The -l flag limits bandwidth to the specified value in Kbit/s. This example limits transfer to 1000 Kbit/s (about 125 KB/s), useful when you don’t want to saturate your network connection.
Combining Options
You can combine multiple options for more complex transfers:
scp -rp -P 2222 myproject/ username@192.168.1.100:/var/www/myapp/
This command:
- Recursively copies the directory (
-r) - Preserves file attributes (
-p) - Uses port 2222 (
-P 2222)
Common Use Cases
Deploying a web application:
scp -r dist/ username@webserver.com:/var/www/html/
After building your application locally, copy the entire dist/ folder to your web server.
Uploading code to a Raspberry Pi:
scp -r my-iot-project/ pi@raspberrypi.local:/home/pi/projects/
Transfer your IoT project code to your Raspberry Pi for testing and deployment.
Backing up remote configuration files:
scp -p username@192.168.1.100:/etc/nginx/nginx.conf ./backups/nginx-$(date +%Y%m%d).conf
Download a configuration file with a timestamped filename for backup purposes.
Retrieving logs from a remote server:
scp username@192.168.1.100:/var/log/application/*.log ./logs/
Download all log files from the remote server’s log directory. Note: The wildcard (*.log) is expanded by the remote shell. To ensure proper expansion, you may need to quote the remote path: scp username@192.168.1.100:'/var/log/application/*.log' ./logs/
Setting Up SSH Key Authentication
SSH key authentication provides a secure, password-less way to authenticate with remote servers. Once configured, SCP will use your SSH keys automatically, making file transfers seamless without password prompts. This is especially valuable for:
- Automated deployments - Scripts and CI/CD pipelines can transfer files without manual password entry
- Frequent transfers - Eliminate the need to type passwords repeatedly
- Enhanced security - Keys are more secure than passwords and can be protected with passphrases
Generating SSH Keys
If you don’t already have SSH keys, generate a new key pair:
ssh-keygen -t ed25519 -C "your_email@example.com"
The -C flag adds a comment (typically your email) to help identify the key later. This is optional but recommended for key management.
When prompted, you can:
- Press Enter to accept the default file location (
~/.ssh/id_ed25519) - Optionally enter a passphrase for additional security (or press Enter for no passphrase)
The command generates two files:
~/.ssh/id_ed25519- Your private key (never share this!)~/.ssh/id_ed25519.pub- Your public key (this gets copied to servers)
Alternative key type: If you’re connecting to older systems that don’t support Ed25519, use RSA instead:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Copying Your Public Key to the Remote Server
The easiest way to set up key-based authentication is using ssh-copy-id:
ssh-copy-id username@192.168.1.100
This command:
- Prompts for your password (one last time!)
- Copies your public key to the remote server’s
~/.ssh/authorized_keysfile - Sets the correct permissions automatically
For Raspberry Pi:
ssh-copy-id pi@raspberrypi.local
After running this command, all future SSH and SCP connections to that server will use your key instead of asking for a password.
Manual Key Installation
If ssh-copy-id isn’t available (e.g., on some Windows systems), you can manually copy your key:
Step 1: Display your public key:
cat ~/.ssh/id_ed25519.pub
Step 2: Copy the entire output (starts with ssh-ed25519 and ends with your email).
Step 3: SSH into the remote server using password authentication:
ssh username@192.168.1.100
Step 4: On the remote server, create the .ssh directory if it doesn’t exist:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
Step 5: Add your public key to the authorized_keys file:
echo "paste-your-copied-public-key-here" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
Replace paste-your-copied-public-key-here with the actual public key content you copied in Step 2.
Step 6: Exit the SSH session and test:
exit
ssh username@192.168.1.100
If configured correctly, you’ll connect without a password prompt.
Verifying Key-Based Authentication
Test that your key authentication is working:
ssh -v username@192.168.1.100
Look for lines in the output like:
debug1: Offering public key: /home/youruser/.ssh/id_ed25519
debug1: Server accepts key
Note: The exact path will vary by operating system. On Windows, it might be /c/Users/youruser/.ssh/id_ed25519 or similar.
Once SSH key authentication is working, all SCP commands will automatically use it:
scp myfile.txt username@192.168.1.100:~/
No password prompt - the transfer happens immediately!
Using Different SSH Keys
If you have multiple SSH keys for different servers, specify which key to use with the -i option:
scp -i ~/.ssh/my_custom_key myfile.txt username@192.168.1.100:~/
This is useful when managing multiple servers or when using different keys for work and personal projects.
SSH Config for Simplified Commands
For servers you access frequently, create an SSH config file to avoid typing full connection details. Edit ~/.ssh/config:
Host mypi
HostName 192.168.1.100
User pi
IdentityFile ~/.ssh/id_ed25519
Host webserver
HostName example.com
User deploy
Port 2222
IdentityFile ~/.ssh/deploy_key
With this configuration, you can use short aliases:
scp myfile.txt mypi:~/
scp -r dist/ webserver:/var/www/
Much cleaner than typing the full username@hostname every time!
Tips and Best Practices
-
Use hostnames instead of IP addresses when possible (e.g.,
raspberrypi.localinstead of192.168.1.100). This makes your commands more readable and resilient to IP changes. -
Test with small files first before transferring large directories to ensure your paths and credentials are correct.
-
Use tab completion in your terminal. Most shells support tab-completing remote paths after the colon in SCP commands.
-
Set up SSH config for frequently accessed servers. Add entries to
~/.ssh/configto avoid typing full connection details every time. -
Be careful with trailing slashes on directories. The behavior of
scp -r dir/ remote:dest/versusscp -r dir remote:dest/can differ.
Troubleshooting
Permission denied errors: Make sure you have write permissions in the destination directory on the remote server.
Connection refused: Verify SSH is running on the remote server (sudo systemctl status ssh on most Linux systems).
Host key verification failed: This usually means the remote host’s SSH key has changed. If you’re certain this is expected (e.g., you reinstalled the OS), you can remove the old key with: ssh-keygen -R hostname_or_ip
Alternatives
While SCP is excellent for simple file transfers, consider these alternatives for specific scenarios:
- rsync - Better for syncing directories, incremental transfers, or resuming interrupted transfers
- SFTP - Interactive file transfer, better for browsing remote directories
- Git - Superior for code deployment when you want version control
For most quick file transfers, especially in Raspberry Pi projects and simple deployments, SCP remains the quickest and most straightforward option.
Official Documentation
For more detailed information about SCP options and advanced usage:
Happy transferring!