Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Transfer Files Between Linux Machines Over SSH
Transferring files between Linux machines over SSH is a common task for system administrators and developers. SSH (Secure Shell) is a protocol that allows you to securely transfer files between machines, as well as remotely access and manage them. SSH creates an encrypted tunnel between machines, protecting your data from eavesdropping and tampering.
Setting Up SSH
Before transferring files, SSH must be installed and running on both machines. You can check if SSH is installed by running
ssh -v
If the command returns version information, SSH is installed. Otherwise, install it using your distribution's package manager
# Ubuntu/Debian sudo apt-get install openssh-server openssh-client # CentOS/RHEL sudo yum install openssh-server openssh-clients
Start and enable the SSH service
# Ubuntu/Debian sudo systemctl start ssh sudo systemctl enable ssh # CentOS/RHEL sudo systemctl start sshd sudo systemctl enable sshd
SCP (Secure Copy)
SCP (Secure Copy) is a command-line utility for securely transferring files between machines over SSH. It uses the same syntax as the regular cp command but works across networks.
Basic SCP Syntax
To copy a file from local to remote machine
scp [options] source_file user@remote_host:destination_path
To copy a file from remote to local machine
scp [options] user@remote_host:source_path destination_file
SCP Examples
Copy a file to remote machine
scp document.pdf john@192.168.1.100:~/Documents/
Copy a file from remote machine
scp john@192.168.1.100:~/report.txt ~/Downloads/
Copy an entire directory recursively
scp -r ~/project/ john@192.168.1.100:~/backups/
Preserve file permissions and timestamps
scp -p script.sh john@192.168.1.100:~/bin/
SFTP (Secure File Transfer Protocol)
SFTP (Secure File Transfer Protocol) provides an interactive session for transferring files over SSH. Unlike SCP, SFTP allows you to browse directories and transfer multiple files in a single session.
Starting an SFTP Session
sftp user@remote_host
Example
sftp john@192.168.1.100
Common SFTP Commands
| Command | Description |
|---|---|
put local_file remote_file |
Upload file from local to remote machine |
get remote_file local_file |
Download file from remote to local machine |
ls |
List files in remote directory |
lls |
List files in local directory |
cd directory |
Change remote directory |
lcd directory |
Change local directory |
mkdir directory |
Create directory on remote machine |
quit or exit
|
Exit SFTP session |
Advanced Features
SSH Key Authentication
Generate SSH key pairs for passwordless authentication
ssh-keygen -t rsa -b 4096 ssh-copy-id user@remote_host
Useful SCP Options
| Option | Description |
|---|---|
-r |
Copy directories recursively |
-p |
Preserve file permissions and timestamps |
-C |
Enable compression during transfer |
-P port |
Specify SSH port (if not default 22) |
-v |
Verbose output for debugging |
Example with Multiple Options
scp -rCp -P 2022 ~/website/ admin@server.example.com:/var/www/
Comparison
| Feature | SCP | SFTP |
|---|---|---|
| Session Type | Non-interactive | Interactive |
| Best For | Quick single transfers | Multiple file operations |
| Directory Browsing | No | Yes |
| Resume Transfers | No | Yes (with some clients) |
| Scripting | Excellent | Limited |
Conclusion
SCP and SFTP are essential tools for secure file transfer between Linux machines over SSH. SCP is ideal for quick, scripted transfers, while SFTP provides an interactive environment for complex file operations. Both methods ensure data security through SSH encryption, making them preferred choices for system administration tasks.
