Using the scp Command in CentOS

本文详细介绍了如何使用scp命令在CentOS系统间进行文件传输,包括命令语法、路径规范以及多文件传输操作。通过示例演示了本地文件到远程系统的传输过程,以及远程文件到本地系统的反向传输。

Repost from: http://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-openssh-using-scp.html


The scp command can be used to transfer files between machines over a secure, encrypted connection. It is similar to rcp.

The general syntax to transfer a local file to a remote system is as follows:

scp <localfile>username@tohostname:<remotefile>

The <localfile> specifies the source including path to the file, such as /var/log/maillog. The<remotefile> specifies the destination, which can be a new filename such as /tmp/hostname-maillog. For the remote system, if you do not have a preceding /, the path will be relative to the home directory of username, typically /home/username/.

To transfer the local file shadowman to the home directory of your account on penguin.example.net, type the following at a shell prompt (replace username with your username):

scp shadowman username@penguin.example.net:shadowman

This will transfer the local file shadowman to /home/username/shadowman on penguin.example.net. Alternately, you can leave off the final shadowman in the scp command.

The general syntax to transfer a remote file to the local system is as follows:

scp username@tohostname:<remotefile><newlocalfile>

The <remotefile> specifies the source including path, and <newlocalfile> specifies the destination including path.

Multiple files can be specified as the source files. For example, to transfer the contents of the directorydownloads/ to an existing directory called uploads/ on the remote machine penguin.example.net, type the following at a shell prompt:

scp downloads/* username@penguin.example.net:uploads/

在使用 Linux 的 `scp` 命令进行文件传输时,如果遇到连接中断的问题,可能涉及多个方面的因素,包括网络稳定性、SSH 配置、文件系统权限、防火墙规则等。以下是一些常见的排查步骤和解决方法: ### 1. 检查网络连接 确保源主机和目标主机之间的网络连接稳定。可以使用 `ping` 或 `traceroute` 来测试网络连通性: ```bash ping <目标主机IP> traceroute <目标主机IP> ``` 如果发现网络延迟高或丢包,可能需要联系网络管理员或检查本地网络设备。 ### 2. 调整 SSH 配置 `scp` 命令依赖于 SSH 协议,因此 SSH 的配置会影响 `scp` 的行为。可以在 `/etc/ssh/sshd_config` 文件中调整以下参数以提高连接稳定性: - `ClientAliveInterval`:设置服务器在一段时间内没有收到客户端消息后发送保持活动的消息间隔(单位为秒)。例如,设置为 `60`。 - `ClientAliveCountMax`:设置服务器在断开连接之前允许的无响应保持活动消息的最大数量。例如,设置为 `3`。 修改完配置后,重启 SSH 服务: ```bash systemctl restart sshd ``` ### 3. 使用 `scp` 的 `-C` 选项压缩数据 使用 `-C` 选项可以启用压缩,减少传输的数据量,从而降低网络拥塞的可能性: ```bash scp -C <源文件路径> <用户名>@<目标主机IP>:<目标路径> ``` ### 4. 检查防火墙和安全组规则 确保源主机和目标主机之间的防火墙规则允许 SSH 端口(默认为 22)的通信。可以使用 `iptables` 或 `firewalld` 来检查和调整防火墙规则。 ### 5. 使用 `rsync` 替代 `scp` 如果 `scp` 经常出现连接中断的问题,可以考虑使用 `rsync` 工具,它支持断点续传功能,能够在连接中断后继续传输未完成的部分: ```bash rsync -avz -e ssh <源文件路径> <用户名>@<目标主机IP>:<目标路径> ``` ### 6. 检查磁盘空间和文件系统权限 确保目标主机上有足够的磁盘空间,并且目标目录的权限允许当前用户写入文件。 ### 7. 使用 `nohup` 或 `screen` 避免终端关闭导致的中断 如果用户担心终端关闭会导致 `scp` 中断,可以使用 `nohup` 或 `screen` 来运行 `scp` 命令: ```bash nohup scp <源文件路径> <用户名>@<目标主机IP>:<目标路径> & ``` ### 8. 检查日志文件 查看 `/var/log/secure` 或 `/var/log/auth.log` 文件,寻找与 SSH 连接相关的错误信息,这有助于定位问题的根本原因。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值