File Transfer to Server: SCP, SFTP and rsync Guide
Complete guide to SCP syntax, SFTP interactive mode, rsync for sync and backup, and WinSCP/FileZilla for Windows users. Secure file transfer to Linux servers.
Table of Contents
File Transfer to Server: SCP, SFTP and rsync Guide
There are multiple secure methods for transferring files to your Linux server. SCP (Secure Copy Protocol), SFTP (SSH File Transfer Protocol), and rsync all use SSH infrastructure to provide encrypted file transfer. This guide covers all three tools and graphical alternatives for Windows users.
File Transfer with SCP
SCP is a simple and fast tool for copying files over SSH.
Basic SCP Syntax
# Copy local file to server
scp /local/file.txt user@server-ip:/remote/directory/
# Copy from server to local machine
scp user@server-ip:/remote/file.txt /local/directory/
# Copy directory (-r recursive)
scp -r /local/directory/ user@server-ip:/remote/directory/
# Custom port (-P uppercase)
scp -P 2222 file.txt user@server-ip:/destination/
# With SSH key file
scp -i ~/.ssh/id_rsa file.txt user@server-ip:/destination/
# Copy between two remote servers
scp user1@server1:/file.txt user2@server2:/destination/
SCP Options
# Transfer with compression (-C)
scp -C large-file.tar.gz user@server-ip:/destination/
# Show progress (-v verbose)
scp -v file.txt user@server-ip:/destination/
# Limit bandwidth (KB/s)
scp -l 1024 file.txt user@server-ip:/destination/
# Multiple files
scp file1.txt file2.txt user@server-ip:/destination/
# With wildcard
scp *.log user@server-ip:/var/log/backup/
SCP is ideal for simple file copying. For large directory synchronization or backups, rsync is more efficient.
File Transfer with SFTP
SFTP is a full-featured file transfer protocol running over SSH. It's the secure alternative to FTP.
SFTP Connection
# Start SFTP session
sftp user@server-ip
# With custom port
sftp -P 2222 user@server-ip
# With SSH key
sftp -i ~/.ssh/id_rsa user@server-ip
SFTP Interactive Commands
Commands available once an SFTP session is open:
# List remote directory
ls
ls -la
# Change remote directory
cd /var/www/html
# Change local directory
lcd ~/Downloads
# Show current remote directory
pwd
# Show current local directory
lpwd
# Upload file (local → remote)
put file.txt
put file.txt /destination/directory/
# Upload directory
put -r /local/directory/ /remote/directory/
# Download file (remote → local)
get file.txt
get /remote/file.txt ~/Downloads/
# Download directory
get -r /remote/directory/ /local/directory/
# Create remote directory
mkdir new-directory
# Delete remote file
rm file.txt
# Move/rename remote file
rename old-name.txt new-name.txt
# Close SFTP session
exit
quit
Batch SFTP Operations
# SFTP in batch mode (with script)
sftp -b - user@server-ip << 'EOF'
cd /var/www/html
put index.html
put style.css
put app.js
EOF
# With batch file
cat > sftp-commands.txt << 'EOF'
cd /var/www/html
put -r ./dist/
EOF
sftp -b sftp-commands.txt user@server-ip
Synchronization and Backup with rsync
rsync is a powerful tool for efficiently synchronizing files and directories. It only transfers changed files.
Basic rsync Syntax
# Sync local directory to server
rsync -avz /local/directory/ user@server-ip:/remote/directory/
# Sync from server to local machine
rsync -avz user@server-ip:/remote/directory/ /local/directory/
# With custom SSH port
rsync -avz -e "ssh -p 2222" /local/ user@server-ip:/remote/
# With SSH key
rsync -avz -e "ssh -i ~/.ssh/id_rsa" /local/ user@server-ip:/remote/
rsync Options
# -a: archive (recursive + permissions + timestamps + symlinks)
# -v: verbose output
# -z: compress during transfer
# -h: human-readable sizes
# -P: progress + partial (resume interrupted transfers)
# --delete: delete files not in source
# --dry-run: simulate without actual transfer
# --exclude: exclude specific files
# Full backup command
rsync -avzh --progress /var/www/ user@server-ip:/backup/www/
# Sync including deletions
rsync -avz --delete /local/ user@server-ip:/remote/
# Simulate first
rsync -avz --dry-run /local/ user@server-ip:/remote/
# Exclude specific files
rsync -avz --exclude='*.log' --exclude='.git' /local/ user@server-ip:/remote/
# Limit bandwidth (KB/s)
rsync -avz --bwlimit=1024 /local/ user@server-ip:/remote/
Automated Backup with rsync
# Daily backup with cron
crontab -e
# Backup every night at 02:00
0 2 * * * rsync -avz --delete /var/www/ user@backup-server:/backup/www/ >> /var/log/rsync-backup.log 2>&1
# Dated backup
rsync -avz /var/www/ user@server-ip:/backup/www-$(date +%Y%m%d)/
rsync's most powerful feature is its delta transfer algorithm: it only transfers changed blocks. If a small part of a large file changes, rsync only sends that changed portion, saving significant bandwidth.
Graphical Interfaces for Windows Users
WinSCP
WinSCP is the most popular SFTP/SCP client for Windows.
Installation and Connection:
- Download from winscp.net
- Create a new session:
- File protocol: SFTP
- Server address: server-ip
- Port: 22
- Username and password
- For SSH key: Advanced → SSH → Authentication → Private key file
WinSCP Features:
- Drag-and-drop file transfer
- Directory synchronization
- Built-in text editor
- Command-line interface (winscp.com)
- Background transfers
FileZilla
FileZilla is a cross-platform SFTP client for Windows, macOS, and Linux.
Setting Up Connection:
- Download from filezilla-project.org
- Site Manager → New Site:
- Protocol: SFTP - SSH File Transfer Protocol
- Server: server-ip
- Port: 22
- Logon type: Normal or Key file
- Connect
SSH Config for Easy Connections
# Edit ~/.ssh/config
nano ~/.ssh/config
Host my-server
HostName 203.0.113.50
User ubuntu
Port 22
IdentityFile ~/.ssh/id_rsa
ServerAliveInterval 60
# Now connect with short alias
scp file.txt my-server:/destination/
sftp my-server
rsync -avz /local/ my-server:/remote/
Troubleshooting
Connection Refused
# Test SSH connection
ssh -v user@server-ip
# Check port
nmap -p 22 server-ip
# Check firewall
sudo ufw status
Permission Error
# Check destination directory permissions
ls -la /destination/directory/
# Fix permissions if needed
sudo chmod 755 /destination/directory/
sudo chown user:user /destination/directory/
Conclusion
SCP, SFTP, and rsync are essential tools for secure file transfer to Linux servers. Use SCP for simple copying, SFTP for interactive management, and rsync for synchronization and backups. Windows users can access the same functionality through WinSCP or FileZilla's graphical interfaces.
Related Articles
How to Connect to Your VDS Server via SSH (Complete Guide)
Step-by-step SSH connection guide for Linux VDS: PuTTY, Terminal, key-based authentication, Path Panel firewall rules, and security best practices.
How to Connect to Windows VDS via RDP: Complete Guide
Complete guide to connecting to Windows Server VDS via Remote Desktop (RDP): setup steps, NLA security, Path Panel firewall rule, and troubleshooting tips.
How to Change VDS Server OS: Step-by-Step Reinstall Guide
Step-by-step guide to changing your VDS server OS via REXE panel: backup tips, supported Linux distributions and Windows Server versions explained.