rsync上传本地文件到服务器
使用 rsync
上传文件夹非常方便,rsync
是一种高效的工具,它能够同步文件和目录,并且只传输修改过的部分。你可以使用 rsync
将本地的文件夹上传到远程服务器,命令格式如下:
# 安装
Command 'rysnc' not found, did you mean:
错误提示表明你的系统中没有安装 rsync
,系统自动提示你安装。你可以通过以下步骤安装 rsync
,无论是在 Linux、Windows(通过 WSL 或 Cygwin)、还是 macOS 上。
# Ubuntu/Debian 系:
使用 apt
命令来安装:
bash复制代码sudo apt update
sudo apt install rsync
1
2
2
# CentOS/Fedora/RHEL 系:
使用 yum
或 dnf
命令来安装:
bash复制代码# 对于 CentOS/RHEL 使用 yum
sudo yum install rsync
# 对于 Fedora 使用 dnf
sudo dnf install rsync
1
2
3
4
5
2
3
4
5
# 基本命令格式:
rsync -avz /path/to/local/folder/ user@remote_server:/path/to/remote/folder/
1
# 参数说明:
-a
:归档模式,表示递归复制文件夹,并保留符号链接、权限、时间戳等属性。-v
:详细模式,显示传输的详细信息。-z
:传输时压缩数据,适用于慢速网络。/path/to/local/folder/
:本地文件夹的路径。注意,尾部的/
很重要,表示同步文件夹内部内容,而不是整个文件夹。user@remote_server
:远程服务器的用户名和地址。/path/to/remote/folder/
:远程服务器上的目标路径。
# 示例:
假设你想将本地的 /home/user/documents/
文件夹上传到远程服务器的 /var/www/documents/
文件夹中,可以这样操作:
rsync -avz /home/user/documents/ user@192.168.1.100:/var/www/documents/
1
# 如果需要使用指定 SSH 密钥
如果你需要使用指定的 SSH 私钥(例如 ~/.ssh/id_rsa
),可以通过 -e
参数指定 ssh
命令和密钥文件:
rsync -avz -e "ssh -i ~/.ssh/id_rsa" /home/user/documents/ user@192.168.1.100:/var/www/documents/
1
# 如果不希望传输某些文件
你还可以通过 --exclude
参数排除某些文件或文件夹:
rsync -avz --exclude 'node_modules' /home/user/documents/ user@192.168.1.100:/var/www/documents/
1
# 在后台运行 rsync
如果你希望 rsync
在后台运行,可以使用 nohup
命令,这样即使关闭终端也不会中断传输:
nohup rsync -avz /home/user/documents/ user@192.168.1.100:/var/www/documents/ &
1
# 例子
sudo rsync -avz --progress -e "ssh -i /home/ubuntu/.ssh/id_rsa" ./model/ bigmodel@192.168.1.236:/home/ai/tool/facefusion/model
1
# 常见错误与解决方案:
- SSH 连接问题:确保远程服务器允许 SSH 连接,且你拥有正确的用户名、IP 地址和 SSH 密钥。
- 权限问题:如果在远程服务器上没有权限写入目标路径,请使用
sudo
赋予适当权限。
在线编辑 (opens new window)
上次更新: 2025/02/25, 18:30:54