FetchLinux的核心定位澄清
FetchLinux并非傳統意義上的“Linux服務器間通信工具”,其主要功能是基于SSH協議的文件同步與遠程文件管理工具,用于在本地與遠程Linux服務器之間高效傳輸文件、執行遠程操作(如上傳/下載、刪除、列出目錄),或作為自動化部署輔助工具(如配合Ansible實現系統更新)。若需實現服務器間通信(如命令交互、服務協同),建議使用SSH、rsync或專用通信工具(如ZeroMQ);若需文件同步或遠程文件操作,FetchLinux是合適選擇。
FetchLinux可通過包管理器或源碼安裝,以下是基于Debian/Ubuntu和Red Hat/CentOS的安裝步驟:
# Debian/Ubuntu
sudo apt update && sudo apt install fetchlinux -y
# Red Hat/CentOS
sudo yum install epel-release -y && sudo yum install fetchlinux -y
git clone https://github.com/fetchlinux/fetchlinux.git /opt/fetchlinux
cd /opt/fetchlinux
sudo ./install.sh # 根據項目文檔執行安裝腳本
安裝完成后,通過fetchlinux --version
驗證安裝是否成功。
若需自定義同步任務或行為,可修改配置文件(默認路徑:/etc/fetchlinux.conf
或~/.fetchlinux.conf
),示例如下:
[source]
local_path = /path/to/local/directory # 本地同步目錄
remote_path = user@remote_host:/path/to/remote/directory # 遠程同步目錄(格式:用戶名@主機IP/路徑)
[options]
compress = true # 啟用傳輸壓縮(減少帶寬占用)
verbose = true # 顯示詳細傳輸日志(便于排查問題)
ssh_key = /path/to/private_key # 指定SSH私鑰路徑(替代密碼認證)
配置完成后,保存文件即可生效。
將本地文件上傳至遠程服務器的指定目錄:
# 上傳單個文件
fetchlinux upload /path/to/local/file username@remote_host:/path/to/remote/directory
# 上傳整個目錄(遞歸)
fetchlinux upload /path/to/local/directory username@remote_host:/path/to/remote/directory -r
將遠程服務器的文件下載至本地目錄:
# 下載單個文件
fetchlinux download username@remote_host:/path/to/remote/file /path/to/local/directory
# 下載整個目錄(遞歸)
fetchlinux download username@remote_host:/path/to/remote/directory /path/to/local/directory -r
刪除遠程服務器上的指定文件或目錄:
# 刪除單個文件
fetchlinux delete username@remote_host:/path/to/remote/file
# 刪除目錄(遞歸)
fetchlinux delete username@remote_host:/path/to/remote/directory -r
查看遠程服務器目錄下的文件和子目錄:
fetchlinux ls username@remote_host:/path/to/remote/directory
以上命令均支持-v
選項(顯示詳細過程),例如fetchlinux upload -v file.txt user@host:/dir
。
若需定期同步兩個服務器之間的文件(如備份、配置同步),可通過配置文件+定時任務實現:
新建sync.conf
文件(路徑:/etc/fetchlinux/sync.conf
),內容如下:
[source]
local_path = /path/to/local/dir # 本地同步目錄
remote_path = user@remote_host:/path/to/remote/dir # 遠程同步目錄
[options]
compress = true # 啟用壓縮
verbose = true # 顯示日志
delete = false # 不刪除目標端多余文件(若需同步刪除,設為true)
通過命令行執行同步:
fetchlinux sync -c /etc/fetchlinux/sync.conf
編輯Cron任務(crontab -e
),添加以下內容(每天凌晨2點同步):
0 2 * * * /usr/bin/fetchlinux sync -c /etc/fetchlinux/sync.conf >> /var/log/fetchlinux_sync.log 2>&1
此配置會將同步日志輸出到/var/log/fetchlinux_sync.log
,便于后續查看。
默認情況下,FetchLinux使用密碼認證,但更安全的方式是SSH密鑰認證,步驟如下:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# 按提示保存密鑰(默認路徑:~/.ssh/id_rsa)
ssh-copy-id user@remote_host
# 輸入遠程服務器密碼,完成后公鑰會自動添加到遠程服務器的~/.ssh/authorized_keys文件
在FetchLinux命令中指定私鑰路徑:
fetchlinux -i ~/.ssh/id_rsa upload /path/to/local/file user@remote_host:/path/to/remote/directory
或修改配置文件中的ssh_key
選項(參考“配置FetchLinux”部分)。
local_path
有讀權限,遠程用戶對remote_path
有寫權限(上傳時),反之亦然(下載時)。compress=true
選項減少帶寬占用,或在穩定網絡環境下操作。-v
選項查看詳細日志,常見原因包括網絡中斷、權限不足、路徑不存在等。