How to Use Linux SCP Command

29/12/2020
Chưa phân loại
The full form of SCP is Secure Copy. It is used to securely copy files from your computer to remote servers and remote servers to your computer using the secure SSH protocol. If SCP is used to copy files, the communication between the remote servers and your computer will be encrypted the same way as the SSH remote connections are done. So, it provides the same authentication method and same level of security as SSH does. This is the advantage of using SCP.

In this article, I am going to show you how to use the SCP command to securely copy files from your computer to and from a Linux remote server. So, let’s get started.

Installing SSH Server and Client Programs on the Remote Server:

If you want to use SCP to copy files to and from a remote server, the SSH server software must be installed on the remote server and scp command must also be available.

On Linux, you can easily install the OpenSSH server and the OpenSSH client package (which contains the scp command) from the official package repository of your desired Linux distribution. I am going to cover some of the common ones in this article.

CentOS 7/RHEL 7:

On CentOS 7 or RHEL 7, you can install the OpenSSH server and the OpenSSH client packages using the YUM package manager as follows:

$ sudo yum install -y openssh-clients openssh

Ubuntu/Debian:

On Ubuntu, Debian, or any Debian based Linux distributions, you can install the OpenSSH server and OpenSSH client packages using the APT package manager as follows:

$ sudo apt install -y openssh-client openssh-server

Arch Linux:

On Arch Linux, you can run the following commands to install the OpenSSH server and OpenSSH client packages using the Pacman package manager.

$ sudo pacman -Sy
$ sudo pacman -S openssh

Installing scp on the Client:

In order to use scp to copy files to the remote server from your computer or copy files from the remote server to your computer, you must have the scp program available in both places (computer and remote server).

On Linux, you can easily install the OpenSSH client package from the official package repository of your desired Linux distribution.

CentOS 7/RHEL 7:

On CentOS 7 or RHEL 7, you can install the OpenSSH client package using the YUM package manager as follows:

$ sudo yum install -y openssh-clients

Ubuntu/Debian:

On Ubuntu, Debian, or any Debian based Linux distributions, you can install the OpenSSH client package using the APT package manager as follows:

$ sudo apt install -y openssh-client

Arch Linux:

On Arch Linux, you can run the following commands to install the OpenSSH client package using the Pacman package manager.

$ sudo pacman -Sy
$ sudo pacman -S openssh

Using SCP:

In order to copy files using SCP, you need to know the IP address of domain name of your server.

To find the IP address of your server, run the following command on the server:

$ ip a

As you can see, the IP address of my server is 192.168.21.131. It will be different for you. So, make sure to replace it with yours from now on.

If you want to use DNS name, you should be able to find it in the admin panel of your VPS provider.

The format of the scp command is:

$ scp [options (optional)] source destination

If you want to copy files from your computer to the remote server, then the destination will be in the format username@hostname/IP_address:remote_directory

If you want to copy files from your remote server to your computer, then the source will be in the format username@hostname/IP_address:remote_path

Here, username is the user that you’re logging in the remote server as.

hostname or IP_address is the DNS name or IP address of your remote server.

remote_path is the directory or file on the remote server that you want to copy to your computer or the directory on the remote server where you want to copy a file or directory from your computer.

Some examples will make it clear.

Let’s say, you want to copy rancheros.iso file from the current working directory of your computer to the HOME directory of the user you’re login in as on your remote server. To do that, run scp as follows:

$ scp rancheros.iso >shovon@192.168.21.131:~

NOTE: Here, ~ represents the HOME directory.

If you’re connecting to the remote server for the first time, you should see the following message. Now, type in yes and then press <Enter>.

Now, type in the password of the user you’re login in as and press <Enter>.

The file is copied to the remote server.

As you can see, the file is there.

To copy a directory (let’s say configs/) from your computer to the remote server, run the following command:

$ scp -r configs/ shovon@192.168.21.131:~

Now, type in the password and press <Enter>. The directory along with all the contents of the directory should be copied to the remote server.

The same way, you can copy a file (let’s say /etc/fstab) from your remote server to your computer’s ~/Downloads directory as follows:

$ scp shovon@192.168.21.131:/etc/fstab ~/Downloads

The file is copied to your computer as you can see.

To copy a directory (let’s say ~/images) from your remote server to the HOME directory of your computer, run scp as follows:

$ scp -r shovon@192.168.21.131:~/images ~

As you can see, the directory including the contents of the directory is copied to my computer’s home directory.

So, that’s how you use SCP to copy files from or to your remote directory. Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Hướng dẫn cài đặt MacOS trên AWS EC2

Step 1: Configure MacOS Amazon Machine Image Let’s start creating an AWS EC2 Instance. Go to AWS console and navigate to...
28/12/2020

Node.js Send E-Mail with Attachment

Node.js is the server side JavaScript engine that is loved by web developers and system administrators all over the world....
29/12/2020

[Graylog] [LAB] [Phần 8] Đặt lại mật khẩu cho user admin trong Graylog server

Đôi khi vì một lý do nào đó bạn quên mật khẩu của user khi đăng nhập hoặc đơn giản bạn muốn...
30/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024