在 linux 下 scp 命令主要用来在不同主机之间做数据(文件或者目录)的安全拷贝的。scp 命令可以将数据(文件或者目录)从本地计算机中拷贝到远程计算机中,或者从远程计算机中拷贝数据(文件或者目录)到本地计算机,scp 命令使用安全加密协议,所以在远程拷贝数据的时候会比较安全,不会被黑客截取。如有不正之处,欢迎批评指正。
一、scp 命令
语法:
scp [-12346BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]
[-l limit] [-o ssh_option] [-P port] [-S program]
[[user@]host1:]file1 ... [[user@]host2:]file2
参数项:
-1 强制scp命令使用协议ssh1。
-2 强制scp命令使用协议ssh2.
-3 Copies between two remote hosts are transferred through the
local host. Without this option the data is copied directly
between the two remote hosts. Note that this option disables
the progress meter.
-4 强制scp命令仅使用IPv4进行传输。
-6 强制scp命令仅使用IPv6进行传输。
-B 使用批处理模式(传输过程中不询问传输口令或短语)。
-C 允许压缩。(将-C标志传递给ssh,从而打开压缩功能)。
-c cipher 以cipher将数据传输进行加密,这个选项将直接传递给ssh。
-F ssh_config 指定一个替代的ssh配置文件,此参数直接传递给ssh。
-i identity_file 从指定文件中读取传输时使用的密钥文件,此参数直接传递给ssh。
-l limit 限定用户所能使用的带宽,以Kbit/s为单位。
-o ssh_option 如果习惯于使用ssh_config(5)中的参数传递方式。参数如下:
AddressFamily
BatchMode
BindAddress
CanonicalDomains
CanonicalizeFallbackLocal
CanonicalizeHostname
CanonicalizeMaxDots
CanonicalizePermittedCNAMEs
CertificateFile
ChallengeResponseAuthentication
CheckHostIP
Cipher
Ciphers
Compression
CompressionLevel
ConnectionAttempts
ConnectTimeout
ControlMaster
ControlPath
ControlPersist
GlobalKnownHostsFile
GSSAPIAuthentication
GSSAPIDelegateCredentials
HashKnownHosts
Host
HostbasedAuthentication
HostbasedKeyTypes
HostKeyAlgorithms
HostKeyAlias
HostName
IdentitiesOnly
IdentityAgent
IdentityFile
IPQoS
KbdInteractiveAuthentication
KbdInteractiveDevices
KexAlgorithms
LogLevel
MACs
NoHostAuthenticationForLocalhost
NumberOfPasswordPrompts
PasswordAuthentication
PKCS11Provider
Port
PreferredAuthentications
Protocol
ProxyCommand
ProxyJump
PubkeyAcceptedKeyTypes
PubkeyAuthentication
RekeyLimit
RhostsRSAAuthentication
RSAAuthentication
SendEnv
ServerAliveInterval
ServerAliveCountMax
StrictHostKeyChecking
TCPKeepAlive
UpdateHostKeys
UsePrivilegedPort
User
UserKnownHostsFile
VerifyHostKeyDNS
-P port 注意是大写的P, port是指定数据传输用到的端口号。
-p 保留原文件的修改时间,访问时间和访问权限。
-q quiet模式:不显示传输进度条。
-r 递归复制整个目录。
-S program 指定加密传输时所使用的程序。此程序必须能够理解ssh(1)的选项。
-v Verbose模式:详细方式显示输出。scp和ssh(1)会显示出整个过程的调试信息。这些信息用于调试连接,验证和配置问题。
二、常用命令
从本地计算机复制目录到远程计算机上
将本地计算机/home/tools
目录复制到远程计算机(IP:10.10.10.10)的 /home/tools1/
目录,复制后的目录结构为/home/tools1/tools
。
- -r 级联复制目录及子目录的内容
- -P 远程计算机上的ssh端口
-
- root 远程计算机用户名
scp -r -P 2222 /home/tools/ root@10.10.10.10:/home/tools1
如果只想复制/home/tools/
目录下的内容到/home/tools1/
目录。命令如下:
scp -r -P 2222 /home/tools/* root@10.10.10.10:/home/tools1
从远程计算机复制目录到本地计算机上
将远程计算机(IP:10.10.10.10)/home/tools
目录复制到本地计算机的 /home/tools1/
目录,复制后的目录结构为/home/tools1/tools
。
- -r 级联复制目录及子目录的内容
- -P 远程计算机上的ssh端口
- root 远程计算机用户名
scp -r -P 2222 root@10.10.10.10:/home/tools /home/tools1/
如果只想复制/home/tools/
目录下的内容到/home/tools1/
目录。命令如下:
scp -r -P 2222 root@10.10.10.10:/home/tools/* /home/tools1/