Securely Transfer Files with SCP in Linux
Introduction
In Unix-like operating systems, transferring files between machines is common. One of the safest and easiest tools for this is scp
(Secure Copy Protocol). scp
uses SSH (Secure Shell) to move files between hosts, keeping data safe and intact. This blog will cover the basics of scp
, including its syntax, common uses, and some practical examples.
What is SCP?
scp
is a command-line utility that allows you to securely copy files and directories between two locations:
From your local machine to a remote machine.
From a remote machine to your local machine.
Between two remote machines.
Since scp
uses SSH for data transfer, it requires SSH access to the target machine. This ensures that the file transfer is encrypted, protecting your data from eavesdropping.
Basic Syntax
The basic syntax for scp
is as follows:
scp [options] source_file target_file
Step-by-Step Example
Let's go through a practical example of using scp
to transfer files between a local machine and a remote server.
Step 1: Create a Directory on the Remote Server
First, log in to your remote server and create a directory where you will copy the file.
shCopy codeubuntu@ip-172-31-25-99:~$ mkdir from_file
ubuntu@ip-172-31-25-99:~$ ls
from_file
ubuntu@ip-172-31-25-99:~$ cd from_file/
ubuntu@ip-172-31-25-99:~/from_file$ pwd
/home/ubuntu/from_file
ubuntu@ip-172-31-25-99:~/from_file$ exit
logout
Connection to ec2-18-117-182-251.us-east-2.compute.amazonaws.com closed.
Step 2: Use SCP to Copy a File from Local to Remote
Now, from your local machine, use the scp
command to copy a file to the directory you just created on the remote server.
shCopy codehp@LAPTOP-UNNSR4CK MINGW64 ~/Downloads
$ scp -i jump-server1.pem trigle.java ubuntu@ec2-18-117-182-251.us-east-2.compute.amazonaws.com:/home/ubuntu/from_file
trigle.java 100% 229 0.8KB/s 00:00
Copying a File from Remote to Local
Similarly, you can copy a file from the remote server to your local machine.
Step 1: Create a File on the Remote Server
First, log in to your remote server, create a file, and add some content to it.
shCopy codeubuntu@ip-172-31-25-99:~/from_file$ touch file.txt
ubuntu@ip-172-31-25-99:~/from_file$ echo "Hello Pratiksha" > file.txt
ubuntu@ip-172-31-25-99:~/from_file$ ls
file.txt
Step 2: Use SCP to Copy the File to Your Local Machine
From your local machine, use the scp
command to copy the file from the remote server.
shCopy codehp@LAPTOP-UNNSR4CK MINGW64 ~/Downloads
$ scp -i jump-server1.pem ubuntu@ec2-18-117-182-251.us-east-2.compute.amazonaws.com:/home/ubuntu/from_file/file.txt .
file.txt 100% 16 0.0KB/s 00:00
Step 3: Verify the File on Your Local Machine
After copying, check that the file is available on your local machine.
shCopy codehp@LAPTOP-UNNSR4CK MINGW64 ~/Downloads
$ ls -l file.txt
-rw-r--r-- 1 hp 197121 16 Jul 10 16:19 file.txt
Step 4: Display the File Content
Finally, display the content of the file to verify that it has been copied correctly.
shCopy codehp@LAPTOP-UNNSR4CK MINGW64 ~/Downloads
$ cat file.txt
Hello Pratiksha
Conclusion
scp
is a powerful and secure tool for moving files between machines. It uses SSH to keep your data safe during transfer, making it essential for system administrators and developers. Whether you need to copy files to a remote server, get backups, or move directories, scp
offers a reliable and easy solution.
By mastering scp
, you can easily manage file transfers in your Unix-like environment, boosting your productivity and keeping your data secure. Try it in your next project and enjoy the ease of secure file transfer.
Subscribe to my newsletter
Read articles from Pratiksha Satpute directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Pratiksha Satpute
Pratiksha Satpute
๐ Hello! I'm Pratiksha Satpute, an aspiring DevOps Engineer on an exciting journey of mastering the DevOps lifecycle. Currently, I'm deeply immersed in the 90DaysOfDevOps challenge, where I explore and conquer daily tasks involving Linux, Docker, AWS, Git, and more. ๐ฑ Every day brings a new learning experience, from configuring and managing Docker containers to automating tasks with cron jobs, and mastering Git and GitHub for efficient version control. I am passionate about understanding the intricacies of DevOps tools and practices, and I continuously strive to enhance my skills and knowledge. ๐ I enjoy sharing my progress and insights through blog posts, aiming to help and inspire others in the tech community. Join me on my journey as I navigate through the world of DevOps, and let's learn and grow together! ๐ฌ Connect with me to discuss DevOps, Docker, cloud computing, or anything tech-related. I'm always open to new ideas, collaborations, and opportunities to learn from fellow enthusiasts.