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

Andy

Well-known member
Creative Team
User ID
1
Joined
7 Jan 2019
Messages
1,766
Reaction score
145
Points
63
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.
 
You will be asked for the user's password on Machine B for both actions. Take into consideration configuring SSH keys for password-less authentication to enable smooth connections (particularly helpful for automated scripts like cron tasks).
 
This guide on SSH and SCP for file transfers is super helpful! I always struggled with moving files between machines, and your clear explanations make it seem easy. Thanks for breaking it down in such a simple way.
 
SSH and SCP enable secure and efficient file transfers between machines, allowing you to easily push or pull files and directories using simple command-line instructions.
 
This SSH guide hits the mark, but really, if you’re not using SSH keys yet, what are you doing? Save yourself some hassle by checking this out for a smoother experience. <a href="https://texttosong.ai/" target="_blank">Lyrics to Music AI</a>
 
 Short URL:
Back
Top