以安全的方式在服务器之间复制文件,scp(secure copy),可以在本地主机和远程主机之间或者两个远程主机之间安全的传输文件,属于ssh的附加功能, 如果需要增量拷贝用rsync

Bash
# 命令格式
scp [option] source target
# 复制文件到远程root/tmp/目录下
scp file.txt root@192.168.0.1:/root/tmp/
scp file.txt root@192.168.0.1:/root/tmp/file.txt
# 复制目录加 -r 参数
scp -r ./dir/ root@192.168.0.1:/root/tmp/dir/
# 远程服务器下载目录
scp -r root@192.168.0.1:/root/tmp/dir/ ./tmp/dir/
# 自定义端口 默认是22
scp -P 2222 file root@192.168.0.1:/root/tmp

scp时指定账号和密码

Bash
sshpass -p 'your_password' scp your_local_file username@remote_host:/path/to/destination
  • your_password 是目标主机上指定用户的密码。
  • your_local_file 是要传输的本地文件的路径。
  • username 是目标主机上的用户名。
  • remote_host 是目标主机的主机名或 IP 地址。
  • /path/to/destination 是文件在目标主机上的目标路径。