Linux系统使用SCP命令远程拷贝文件

欢迎您,来自IP地址为:54.227.51.103的朋友

付款前,可以输入宝贝名称查一查有没有优惠券可以用哦!


SCP命令意指“secure cp”,是采用SSH协议进行远程文件拷贝的命令。使用这条命令,可以很方便的向远程服务器拷贝文件或目录。

scp命令的基本用法如下所示:

scp source_file_name username@destination_host:destination_folder

其中 source_file_name 表示源文件或目录, username 表示远程主机的登录用户, @destination_host 是要登录的远程主机, destination_folder 表示远程主机的目标文件夹。

如果不带任何参数,那么scp会在后台运行,在命令执行完成或中途出现错误时才会向用户显示信息。加入“-v”参数后,就可以将后台信息显示给用户,以便用户进行命令执行跟踪和问题处理。典型输出如下所示:

Executing: program /usr/bin/ssh host www.daehub.com, user root, command scp -v -t /dataroot
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips  26 Jan 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 58: Applying options for *
debug1: Connecting to www.daehub.com [123.206.60.79] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.4
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5* compat 0x0c000000
debug1: Authenticating to www.daehub.com:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: diffie-hellman-group-exchange-sha256
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: umac-64@openssh.com compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: umac-64@openssh.com compression: none
debug1: kex: diffie-hellman-group-exchange-sha256 need=16 dh_need=16
debug1: kex: diffie-hellman-group-exchange-sha256 need=16 dh_need=16
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<3072<8192) sent
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: ssh-rsa SHA256:ssa1mjZJCFthN2Eis7tt7rP7db01ESB4v7wJcu3JAKI
debug1: Host 'www.daehub.com' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:1
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)

debug1: Unspecified GSS failure.  Minor code may provide more information
No Kerberos credentials available (default cache: KEYRING:persistent:0)

debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: Next authentication method: password
root@www.daehub.com's password:

此时,正确输入登录远程主机的用户密码,就可以顺利完成拷贝。如果需要把整个目录的内容发送到远程主机,则需要“-r”参数指定本地目录。用法示例如下:

scp -r  source_folder username@destination_host:destination_folder

这样,就可以很方便的将本地目录下的所有文件传送到远程主机。

当然,scp 命令还有其它参数,下面对其进行简要说明,但不再做命令示例。

-p 估算传送时间并计时

-P 连接主机的选定端口,而不是SSH所使用的22端口

-C 采用压缩方式传送文件

-c 采用非默认的加密算法

-l 限定传送占用带宽,以kbps为单位

-q 采用静默方式传送,即不会显示相关信息

以上就是scp命令的简要用法说明,有了这个神奇的工具,可以很方便的通过命令行传送文件。当然,scp还有其它特色用法,这里就不再赘述。

发表评论

电子邮件地址不会被公开。 必填项已用*标注