Easy SSH/SCP Guide: How to Transfer Files and Folders Between Machines

Andy

Administrator
Creative Team
User ID
1
Joined
7 Jan 2019
Messages
1,142
Reaction score
58
Points
48
Discover the versatility of SSH, not only as a secure protocol for system access but also as a robust tool for file transfers. In this guide, we delve into the world of SCP, a utility that harnesses the power of SSH for efficient file transfers between servers. Whether you're an IT professional or an enthusiast, learn how to streamline your file management tasks with these essential tools.

Before we begin, let's establish the context with these assumptions:

  • You're operating from Machine A, which has the IP address 10.0.15.2.
  • You're aiming to transfer files to or from Machine B, which has the IP address 10.0.17.2.
  • On Machine B, you will use the SSH login '[email protected]'.
  • SSH is running on the default port 22 on Machine B.
  • The file or folder on Machine A is located at: /path/to/machine_A/local.
  • The destination on Machine B is: /path/to/machine_B/remote.

⬆️ Pushing a File from Machine A to Machine B:
To transfer a file from your current machine (A) to a remote machine (B), use the following command:
Bash:
scp -p -P "22" -C "/path/to/A/local/file.txt" "[email protected]:/path/to/B/remote/file.txt"
This securely copies the local file from Machine A to the specified path on Machine B.

⬆️ Pushing a Folder from Machine A to Machine B:
To copy an entire directory from Machine A to Machine B, use:
Bash:
scp -rp -P "22" "/path/to/A/local/directory" "[email protected]:/path/to/B/remote"
This command will recursively copy the entire directory and its contents from the local path on Machine A to the specified path on Machine B.

⬇️ Pulling a File from Machine B to Machine A:
Conversely, if you need to download a file from Machine B to Machine A, reverse the file paths:
Bash:
scp -p -P "22" -C "[email protected]:/path/to/B/remote/file.txt" "/path/to/A/local/file.txt"
This command fetches the file from Machine B and places it in the specified location on Machine A.

⬇️ Pulling a Folder from Machine B to Machine A:
To download an entire directory from Machine B to Machine A, reverse the paths:
Bash:
scp -rp -P "22" "[email protected]:/path/to/B/remote/directory" "/path/to/A/local"
This command fetches the entire directory and its contents from Machine B and places them in the specified location on Machine A.

💡 Additional Tips:
  • Authentication: For both operations, you'll be prompted for the password of the user on Machine B. To facilitate seamless connections (especially useful for automated scripts like cron jobs), consider setting up SSH keys for password-less authentication.
  • Cron Jobs: If you're automating transfers with cron and encounter issues, running pkill ssh-agent might help by cleaning up lingering SSH sessions.
 
 Short URL:
Back
Top