scp菜鸟教程:基本语法、用法示例、配置项

2022-06-2616:25:33服务器及运维Comments1,744 views字数 2656阅读模式

简介 # 

scp是 secure copy 的缩写,相当于cp命令 + SSH。它的底层是 SSH 协议,默认端口是22,相当于先使用ssh命令登录远程主机,然后再执行拷贝操作。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

scp主要用于以下三种复制操作。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

  • 本地复制到远程。
  • 远程复制到本地。
  • 两个远程系统之间的复制。

使用scp传输数据时,文件和密码都是加密的,不会泄漏敏感信息。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

基本语法

scp的语法类似cp的语法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

$ scp source destination

上面命令中,source是文件当前的位置,destination是文件所要复制到的位置。它们都可以包含用户名和主机名。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

$ scp user@host:foo.txt bar.txt

上面命令将远程主机(user@host)用户主目录下的foo.txt,复制为本机当前目录的bar.txt。可以看到,主机与文件之间要使用冒号(:)分隔。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

scp会先用 SSH 登录到远程主机,然后在加密连接之中复制文件。客户端发起连接后,会提示用户输入密码,这部分是跟 SSH 的用法一致的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

用户名和主机名都是可以省略的。用户名的默认值是本机的当前用户名,主机名默认为当前主机。注意,scp会使用 SSH 客户端的配置文件.ssh/config,如果配置文件里面定义了主机的别名,这里也可以使用别名连接。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

scp支持一次复制多个文件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

$ scp source1 source2 destination

上面命令会将source1source2两个文件,复制到destination文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

注意,如果所要复制的文件,在目标位置已经存在同名文件,scp会在没有警告的情况下覆盖同名文件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

用法示例

(1)本地文件复制到远程文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

复制本机文件到远程系统的用法如下。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

# 语法
$ scp SourceFile user@host:directory/TargetFile

# 示例
$ scp file.txt remote_username@10.10.0.2:/remote/directory

下面是复制整个目录的例子。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

# 将本机的 documents 目录拷贝到远程主机,
# 会在远程主机创建 documents 目录
$ scp -r documents username@server_ip:/path_to_remote_directory

# 将本机整个目录拷贝到远程目录下
$ scp -r localmachine/path_to_the_directory username@server_ip:/path_to_remote_directory/

# 将本机目录下的所有内容拷贝到远程目录下
$ scp -r localmachine/path_to_the_directory/* username@server_ip:/path_to_remote_directory/

(2)远程文件复制到本地文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

从远程主机复制文件到本地的用法如下。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

# 语法
$ scp user@host:directory/SourceFile TargetFile

# 示例
$ scp remote_username@10.10.0.2:/remote/file.txt /local/directory

下面是复制整个目录的例子。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

# 拷贝一个远程目录到本机目录下
$ scp -r username@server_ip:/path_to_remote_directory local-machine/path_to_the_directory/

# 拷贝远程目录下的所有内容,到本机目录下
$ scp -r username@server_ip:/path_to_remote_directory/* local-machine/path_to_the_directory/
$ scp -r user@host:directory/SourceFolder TargetFolder

(3)两个远程系统之间的复制文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

本机发出指令,从远程主机 A 拷贝到远程主机 B 的用法如下。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

# 语法
$ scp user@host1:directory/SourceFile user@host2:directory/SourceFile

# 示例
$ scp user1@host1.com:/files/file.txt user2@host2.com:/files

系统将提示你输入两个远程帐户的密码。数据将直接从一个远程主机传输到另一个远程主机。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

配置项

(1)-c文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

-c参数用来指定文件拷贝数据传输的加密算法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

$ scp -c blowfish some_file your_username@remotehost.edu:~

上面代码指定加密算法为blowfish文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

(2)-C文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

-C参数表示是否在传输时压缩文件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

$ scp -c blowfish -C local_file your_username@remotehost.edu:~

(3)-F文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

-F参数用来指定 ssh_config 文件,供 ssh 使用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

$ scp -F /home/pungki/proxy_ssh_config Label.pdf root@172.20.10.8:/root

(4)-i文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

-i参数用来指定密钥。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

$ scp -vCq -i private_key.pem ~/test.txt root@192.168.1.3:/some/path/test.txt

(5)-l文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

-l参数用来限制传输数据的带宽速率,单位是 Kbit/sec。对于多人分享的带宽,这个参数可以留出一部分带宽供其他人使用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

$ scp -l 80 yourusername@yourserver:/home/yourusername/* .

上面代码中,scp命令占用的带宽限制为每秒 80K 比特位,即每秒 10K 字节。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

(6)-p文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

-p参数用来保留修改时间(modification time)、访问时间(access time)、文件状态(mode)等原始文件的信息。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

$ scp -p ~/test.txt root@192.168.1.3:/some/path/test.txt

(7)-P文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

-P参数用来指定远程主机的 SSH 端口。如果远程主机使用默认端口22,可以不用指定,否则需要用-P参数在命令中指定。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

$ scp -P 2222 user@host:directory/SourceFile TargetFile

(8)-q文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

-q参数用来关闭显示拷贝的进度条。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

$ scp -q Label.pdf mrarianto@202.x.x.x:.

(9)-r文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

-r参数表示是否以递归方式复制目录。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

(10)-v文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

-v参数用来显示详细的输出。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html

$ scp -v ~/test.txt root@192.168.1.3:/root/help2356.txt
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/24410.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/24410.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定