How to Use SCP Command to copy file between server and local

SCP means secure copy. This command is used to copy files between hosts. It is also known as remote file copy command. Its secure because it is using same authentication as SSH command. We will see how to copy file from server to local computer and vice versa.

Copy file from server to local computer in linux

#syntax
scp -P port username@hostname:/filepath localpath

#example
scp -P 20017 root@example.com:/var/www/html/myfile.zip /home/codextblog/Downloads/

Above command will copy myfile.zip from remote computer to your computer under Downloads directory. You can ignore port option if your server is using default port.

Copy file from local computer to server in linux

#syntax
scp localpath username@hostname:/filepath

#example
scp /home/codextblog/Downloads/mysecondfile.zip root@example.com:/var/www/html/

Above command will copy mysecondfile.zip file from your computer to remote computer under html directory.

SCP command with private key authentication

Sometimes you need a identity file, generally a private key file is required to authenticate your local machine identity. In that case you have to pass key file in your SCP command as below.

#syntax
scp -i privatekey.pem username@hostname:/filepath localpath

#example
scp -i my-ec2.pem ec2-user@example.com:/home/ec2-user/test.sql /home/codextblog/Downloads/

Want to ask a question or leave a comment?

Leave a Comment

(0 Comments)

All the comments are goes into moderation before approval. Irrelevant comment with links directly goes to spam. Your email address will not be published.

Subscribe Here
SUBSCRIBE NOW

Want to Increase your Magento 2 Development Knowledge?

If Yes! Then Subscribe to our newsletter and receive step-by-step tutorials, code snippets once a week!!
close-link

To Avoid Spam Downloads, We Want Your Email

We will send you download link right
away. Please submit form below.
SEND ME DOWNLOAD LINK
Close