📂 Truyền File Qua SSH
📋 Giới Thiệu
SSH cung cấp hai phương thức truyền file an toàn:
- SCP (Secure Copy): Sao chép file đơn giản
- SFTP (SSH File Transfer Protocol): Giao thức truyền file đầy đủ tính năng
🚀 Sử Dụng SCP
1. Copy File từ Local lên Server
# Copy file test.txt lên server
$ scp ./test.txt [email protected]:~/
[email protected]'s password: # nhập mật khẩu
test.txt 100% 10 0.0KB/s 00:00
2. Copy File từ Server về Local
$ scp [email protected]:/home/ubuntu/test.txt ./test.txt
[email protected]'s password:
test.txt 100% 10 0.0KB/s 00:00
🔄 Sử Dụng SFTP
1. Kết Nối SFTP
$ sftp [email protected]
[email protected]'s password: # nhập mật khẩu
Connected to www.srv.world.
sftp>
2. Điều Hướng và Xem File
# Xem thư mục hiện tại trên server
sftp> pwd
Remote working directory: /home/ubuntu
# Xem thư mục hiện tại trên local
sftp> !pwd
/home/ubuntu
# Liệt kê file trên server
sftp> ls -l
drwxrwxr-x 2 ubuntu ubuntu 6 Apr 22 21:33 public_html
-rw-rw-r-- 1 ubuntu ubuntu 10 Apr 21 22:53 test.txt
# Liệt kê file trên local
sftp> !ls -l
total 4
-rw-rw-r-- 1 ubuntu ubuntu 10 Apr 22 21:31 test.txt
3. Upload File
# Upload một file
sftp> put test.txt ubuntu.txt
Uploading test.txt to /home/ubuntu/ubuntu.txt
test.txt 100% 10 0.0KB/s 00:00
# Kiểm tra kết quả
sftp> ls -l
drwxrwxr-x 2 ubuntu ubuntu 6 Apr 22 21:33 public_html
-rw-rw-r-- 1 ubuntu ubuntu 10 Apr 22 21:39 ubuntu.txt
-rw-rw-r-- 1 ubuntu ubuntu 10 Apr 21 22:53 test.txt
# Upload nhiều file
sftp> put *.txt
Uploading test.txt to /home/ubuntu/test.txt
test.txt 100% 10 0.0KB/s 00:00
Uploading test2.txt to /home/ubuntu/test2.txt
test2.txt 100% 0 0.0KB/s 00:00
4. Download File
# Download một file
sftp> get test.txt
Fetching /home/ubuntu/test.txt to test.txt
/home/ubuntu/test.txt 100% 10 0.0KB/s 00:00
# Download nhiều file
sftp> get *.txt
Fetching /home/ubuntu/ubuntu.txt to ubuntu.txt
/home/ubuntu/ubuntu.txt 100% 10 0.0KB/s 00:00
Fetching /home/ubuntu/test.txt to test.txt
/home/ubuntu/test.txt 100% 10 0.0KB/s 00:00
5. Quản Lý Thư Mục và File
# Tạo thư mục mới
sftp> mkdir testdir
sftp> ls -l
drwxrwxr-x 2 ubuntu ubuntu 6 Apr 22 21:33 public_html
drwxrwxr-x 2 ubuntu ubuntu 6 Apr 22 21:53 testdir
-rw-rw-r-- 1 ubuntu ubuntu 10 Apr 22 21:39 ubuntu.txt
-rw-rw-r-- 1 ubuntu ubuntu 10 Apr 21 22:53 test.txt
# Xóa thư mục
sftp> rmdir testdir
rmdir ok, 'testdir' removed
# Xóa file
sftp> rm test2.txt
Removing /home/ubuntu/test2.txt
6. Thực Thi Lệnh Local
# Xem nội dung file trên local
sftp> !cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
...
ubuntu:x:1001:1001::/home/ubuntu:/bin/bash
7. Thoát SFTP
sftp> quit
221 Goodbye.
⚠️ Lưu Ý Quan Trọng
-
Bảo Mật:
- Sử dụng SSH key thay vì mật khẩu
- Giới hạn quyền truy cập file
- Kiểm tra kết nối an toàn
-
Hiệu Suất:
- Sử dụng nén khi truyền file lớn
- Tránh truyền quá nhiều file cùng lúc
- Kiểm tra băng thông mạng
🔍 Xử Lý Sự Cố
1. Lỗi Kết Nối
# Kiểm tra kết nối SSH
ssh -v username@remote_host
# Kiểm tra port SFTP
netstat -tuln | grep 22
2. Lỗi Quyền
# Kiểm tra quyền thư mục
ls -la /path/to/directory
# Sửa quyền nếu cần
chmod 755 /path/to/directory