linux/scp

Secure Copy Files Between Hosts

Securely copy files or directories between local and remote hosts over SSH.

network
file-transfer
security
ssh
linux

Command

scp

Explanation

The `scp` (secure copy) command leverages SSH to securely transfer files between local and remote systems. It supports recursive copying with `-r`, and can use different authentication keys via `-i`. Bandwidth limits can be applied using the `-l` flag. It is commonly replaced by `rsync` for advanced synchronization needs.

Common Use Cases

  • Transfer backup files between servers
  • Upload application files to a remote system
  • Copy directories for deployment or testing

Best Practices

  • Use SSH keys for authentication instead of passwords
  • Verify file integrity after transfer
  • Use `rsync` for incremental or large transfers

Common Mistakes to Avoid

  • Using wrong paths or overwriting files on remote systems
  • Forgetting `-r` when copying directories

Troubleshooting

Problem: Permission denied during file transfer

Solution: Check file permissions and SSH key access for the remote user.

Problem: Connection timed out

Solution: Ensure the remote host is reachable and SSH is enabled.

Examples

Copy file to remote host

scp file.txt user@host:/path

Copy file from remote host to current directory

scp user@host:/path/file.txt .

Recursively copy a directory

scp -r project/ user@host:/backup/