📁 Sử Dụng SSHFS
📋 Giới Thiệu
SSHFS (SSH Filesystem) cho phép bạn mount filesystem từ server từ xa vào máy local thông qua kết nối SSH. Điều này giúp:
- Truy cập file từ xa như file local
- Sử dụng các công cụ local để thao tác file từ xa
- Không cần cấu hình phức tạp như NFS
📦 Cài Đặt
# Cài đặt SSHFS
sudo apt -y install sshfs
🛠️ Cách Sử Dụng Cơ Bản
1. Tạo Mount Point
# Tạo thư mục để mount
mkdir ~/remote-files
2. Mount Filesystem
# Cú pháp cơ b ản
sshfs user@remote-server:/remote/path ~/remote-files
# Ví dụ cụ thể
sshfs [email protected]:/home/admin/data ~/remote-files
3. Kiểm Tra Mount Point
# Kiểm tra mount point
df -hT
# Output mẫu
Filesystem Type Size Used Avail Use% Mounted on
[email protected]:/home/admin/data fuse.sshfs 100G 50G 50G 50% /home/user/remote-files
4. Unmount Filesystem
# Ngắt kết nối SSHFS
fusermount -u ~/remote-files
🔄 Tùy Chọn Mount Nâng Cao
1. Mount với Tùy Chọn Bổ Sung
sshfs user@remote-server:/remote/path ~/remote-files \
-o reconnect \
-o compression=yes \
-o allow_other \
-o IdentityFile=~/.ssh/id_rsa
Giải Thích Tùy Chọn
reconnect
: Tự động kết nối lại khi mất kết nốicompression=yes
: Bật nén dữ liệuallow_other
: Cho phép user khác truy cậpIdentityFile
: Chỉ định SSH key
2. Mount Tự Động khi Khởi Động
Thêm vào file /etc/fstab
:
# Thêm dòng sau vào /etc/fstab
user@remote-server:/remote/path /home/user/remote-files fuse.sshfs defaults,_netdev 0 0
🔧 Ví Dụ Thực Tế
1. Mount Thư Mục Backup
# Mount thư mục backup từ server
sshfs backup@backup-server:/backup ~/backup-files \
-o reconnect \
-o compression=yes \
-o ServerAliveInterval=15
2. Mount với Nhiều Tùy Chọn
# Mount với cấu hình nâng cao
sshfs [email protected]:/data ~/remote-data \
-o reconnect \
-o compression=yes \
-o cache=yes \
-o kernel_cache \
-o auto_cache \
-o negative_vncache \
-o ServerAliveInterval=15 \
-o ServerAliveCountMax=3
⚙️ Các Tùy Chọn Phổ Biến
Tùy Chọn | Mô Tả |
---|---|
compression=yes | Bật nén dữ liệu |
reconnect | Tự động kết nối lại |
cache=yes | Bật cache |
allow_other | Cho phép user khác truy cập |
IdentityFile | Chỉ định file SSH key |
ServerAliveInterval | Thời gian gửi keep-alive |
🔍 Xử Lý Sự Cố
1. Lỗi Quyền Truy Cập
# Kiểm tra quyền thư mục mount
ls -la ~/remote-files
# Sửa quyền nếu cần
chmod 755 ~/remote-files
2. Lỗi Kết Nối
# Kiểm tra kết nối SSH
ssh user@remote-server
# Kiểm tra log
tail -f /var/log/syslog