Sao chép file giữa các máy linux với câu lệnh SCP

Linux

Contents:

SCP (Secure Copy) là một câu lệnh dùng để copy tập tin giữa các máy tính hệ điều hành Linux. SCP sử dụng giao thức bảo mật SSH để chuyển file giữa 2 hệ thống, giao thức này bảo mật hơn giao thức gửi file ftp thông thường.

Xem thêm:

Loạt bài thủ thuật về Linux

Sau đây là một số ví dụ thường dùng với câu lệnh SCP

Lệnh SCP command yêu cầu mật khẩu của remote server. Nếu bạn muốn không cần mật khẩu hoặc tạo script, lập lịch thì bạn cần phải tạo SSH Key cho remote server

Từ Local đến Remote Server

Để copy file từ  local đến remote server bạn sử dụng lệnh theo cú pháp sau:

scp /path/to/local/file.txt user@serverIP:/remote/path/

Ví dụ gửi file test.txt đến thư mục home tại server IP 192.168.1.199

scp test.txt [email protected]:/home

Từ Remote Server về Local

Để copy file từ remote server về local bạn sử dụng lệnh theo cú pháp sau:

scp user@serverIP:/remote/file.txt /path/to/local/

Ví dụ copy file test.txt từ home tại server IP 192.168.1.199 về thư mục home tại local:

scp [email protected]:/home/test.txt /home/

Lệnh SCP với cổng (port)

Trong trường hợp đã thay đổi cổng mặc định của SSH bạn sử dụng option -P để khai báo cổng SSH của remote server.

Ví dụ để copy file từ  thư mục home của local đến thư mục home của remote server với cổng 1222 bạn sử dụng lệnh theo cú pháp sau:

scp -P 1222 home/test.txt [email protected]:/home/

Copy thư mục

Để copy thư mục bằng lệnh SCP bạn sử dụng thêm option -r

Ví dụ để copy  thư mục home/test của local đến thư mục home của remote server với cổng 1222 bạn sử dụng lệnh theo cú pháp sau:

scp -r -P 1222 home/test [email protected]:/home/

Share: