Code前端首页关于Code前端联系我们

scp 初学者教程:基本语法、使用示例、配置项

terry 2年前 (2023-09-28) 阅读数 62 #未命名

简介 #

scp 是 secure copy 的缩写,相当于命令cp + SSH。它的底层是SSH协议,默认端口是22。这相当于使用命令ssh先登录远程主机,然后执行复制操作。

scp主要用于以下三种复制操作。

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

使用scp传输数据时,文件和密码都会被加密,敏感数据不会泄露。

基本语法 语法

scp类似于语法cp

$ scp source destination

在上面的命令中,source是文件的当前位置,destination是要复制文件的位置。两者都可以包含用户名和主机名。

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

上述命令将远程主机上用户主目录(user@host)中的foo.txt复制到当前目录中的bar.txt本地机器的。 。如您所见,冒号(:)用于分隔主机和文件。

scp 将首先使用 SSH 登录远程主机,然后通过加密连接复制文件。当客户端连接时,将提示用户输入与使用 SSH 一致的密码。

用户名和主机名都可以省略。 username 默认值为本地计算机的当前用户名,hostname 默认值为当前主机。请注意,scp 将使用 SSH 客户端配置文件 .ssh/config。如果配置文件中定义了主机别名,这里也可以使用链接别名。

scp支持一次复制多个文件。

$ scp source1 source2 destination

上述命令会将两个文件source1source2复制到destination

请注意,如果您要复制的文件在目标位置已包含同名文件,scp 将覆盖同名文件而不发出警告。

使用示例

(1) 将本地文件复制到远程系统

将本地文件复制到远程系统的用法如下。

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

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

下面是复制整个目录的示例。

# 将本机的 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) 将远程文件复制到本地

将文件从远程主机复制到本地的用法如下。

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

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

以下是复制整个目录的示例。

# 拷贝一个远程目录到本机目录下
$ 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) 两个远程系统之间复制

本机发出指令从远程主机 A 复制到远程主机 B,用法如下。

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

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

系统将提示您输入两个远程帐户的密码。数据将直接从一台远程主机传输到另一台远程主机。

配置项

(1) -c

-c 该参数用于指定传输文件复制数据的加密算法。

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

上面的代码指定加密算法为blowfish。参数

(2)-C

-C表示传输过程中是否应压缩文件。参数

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

(3)-F

-F用于指定ssh_config文件使用ssh。

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

(4) 参数-i

-i 用于指定密钥。

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

(5)-l

-l该参数用于限制数据传输带宽速度,单位为Kbit/s。对于多人共享的带宽,该参数可以指定一部分带宽供其他人使用。

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

在上面的代码中,命令scp所占用的带宽被限制为80K位/秒,即10K字节/秒。

(6)参数-p

-p用于保存原始文件的信息,如修改时间(modification time)、访问时间(access time)、文件状态(模式)..

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

(7) 参数-P

-P用于指定远程主机的SSH端口。如果远程主机使用默认端口22,则无需指定。否则,必须在命令中使用参数-P 指定。

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

(8) 参数-q

-q 用于关闭显示副本的进度条。

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

(9)-r

-r该参数表示是否递归复制目录。参数

(10)-v

-v用于显示详细结果。

$ scp -v ~/test.txt root@192.168.1.3:/root/help2356.txt

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

热门