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选项查看详细日志,常见原因包括网络中断、权限不足、路径不存在等。