赞
踩
有时候不可避免的需要将文件复制到另外一台服务器上,那么这时就可以使用scp命令远程拷贝文件,scp命令是基于SSH协议,在复制的过程中数据都是加密过的,会比明文传输更为安全。
scp的帮助文档中查看命令的概述
- NAME
- scp — secure copy (remote file copy program)
- DESCRIPTION
- scp copies files between hosts on a network. It uses ssh(1) for data transfer, and uses the same
- authentication and provides the same security as ssh(1). scp will ask for passwords or passphrases if
- they are needed for authentication.
-
- File names may contain a user and host specification to indicate that the file is to be copied to/from
- that host. Local file names can be made explicit using absolute or relative pathnames to avoid scp
- treating file names containing ‘:’ as host specifiers. Copies between two remote hosts are also per‐
- mitted.
scp(secure copy)命令,主要功能是用来远程拷贝文件,可以在多台Linux系统之间复制文件或目录,有些类似于cp命令的功能,但复制的范围是网络上的另一台主机。
- SYNOPSIS
- 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 Forces scp to use protocol 1.
-
- -2 Forces scp to use protocol 2.
-
- -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 Forces scp to use IPv4 addresses only.
-
- -6 Forces scp to use IPv6 addresses only.
-
- -B Selects batch mode (prevents asking for passwords or passphrases).
-
- -C Compression enable. Passes the -C flag to ssh(1) to enable compression.
-
- -c cipher
- Selects the cipher to use for encrypting the data transfer. This option is directly passed to
- ssh(1).
-
- -F ssh_config
- Specifies an alternative per-user configuration file for ssh. This option is directly passed
- to ssh(1).
-
- -i identity_file
- Selects the file from which the identity (private key) for public key authentication is read.
- This option is directly passed to ssh(1).
-
- -l limit
- Limits the used bandwidth, specified in Kbit/s.
-
- -o ssh_option
- Can be used to pass options to ssh in the format used in ssh_config(5). This is useful for
- specifying options for which there is no separate scp command-line flag. For full details of
- the options listed below, and their possible values, see ssh_config(5).
-
- AddressFamily
- BatchMode
- BindAddress
- CanonicalDomains
- CanonicalizeFallbackLocal
- CanonicalizeHostname
- CanonicalizeMaxDots
- CanonicalizePermittedCNAMEs
- CertificateFile
- ChallengeResponseAuthentication
- CheckHostIP
- Cipher
- Ciphers
- Compression
- CompressionLevel
- ConnectionAttempts
- ConnectTimeout
- ControlMaster
- -2 Forces scp to use protocol 2.
-
- -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 Forces scp to use IPv4 addresses only.
-
- -6 Forces scp to use IPv6 addresses only.
-
- -B Selects batch mode (prevents asking for passwords or passphrases).
-
- -C Compression enable. Passes the -C flag to ssh(1) to enable compression.
-
- -c cipher
- Selects the cipher to use for encrypting the data transfer. This option is directly passed to
- ssh(1).
-
- -F ssh_config
- Specifies an alternative per-user configuration file for ssh. This option is directly passed
- to ssh(1).
-
- -i identity_file
- Selects the file from which the identity (private key) for public key authentication is read.
- This option is directly passed to ssh(1).
-
- -l limit
- Limits the used bandwidth, specified in Kbit/s.
-
- -o ssh_option
- Can be used to pass options to ssh in the format used in ssh_config(5). This is useful for
- specifying options for which there is no separate scp command-line flag. For full details of
- the options listed below, and their possible values, see ssh_config(5).
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
命令选项 | 含义 |
-1 | 使用ssh协议版本1 |
-2 | 使用ssh协议版本2 |
-4 | 使用ipv4 |
6 | 使用ipv6 |
-B | 以批处理模式运行 |
-C | 使用压缩 |
-F | 指定ssh配置文件 |
-l | 指定带宽限制 |
-o | 指定使用的ssh选项 |
-P | 指定远程主机的端口号 |
-p | 保留文件的修改时间,访问时间和权限模式 |
-q | 不显示复制进度 |
-r | 以递归的方式进行复制 |
第一种方式:指定用户名,将文件远程拷贝到另外一台服务器的目录下,命令执行后再输入密码。
scp local_file remote_username@remote_ip:remote_folder
第二种方式:指定用户名,将文件远程拷贝到另外一台服务器下,文件可以修改成其他名字,相当于远程拷贝重命名;命令执行后在输入密码。
scp local_file remote_username@remote_ip:remote_file
第三种方式:不指定用户名,将文件远程拷贝到另外一台服务器的目录下,命令执行后需要输入用户名和密码
scp local_file remote_ip:remote_folder
第四种方式:不指定用户名,将文件远程拷贝到另外一台服务器下,文件可以修改成其他名字,相当于远程拷贝重命名;命令执行后在输入密码。
scp local_file remote_ip:remote_file
案例演示:
- scp /usr/local/nginx/conf/nginx.conf root@192.168.45.128:/usr/local/nginx/conf
-
- scp /usr/local/nginx/conf/nginx.conf root@192.168.45.128:/usr/local/nginx/conf/nginx.conf.backup
-
- scp /usr/local/nginx/conf/nginx.conf 192.168.45.128:/usr/local/nginx/conf
-
- scp /usr/local/nginx/conf/nginx.conf 192.168.45.128:/usr/local/nginx/conf.backup
第一种方式:指定用户名,将目录远程拷贝到另外一台服务器的指定目录下,需要使用-r参数表示递归操作,命令执行后再输入密码
scp -r local_folder remote_username@remote_ip:remote_folder
第二种方式:不指定用户名,将目录远程拷贝到另外一台服务器的指定目录下,需要使用-r参数表示递归操作,命令执行后再输入密码
scp -r local_folder remote_ip:remote_folder
案例演示:
- scp -r /tmp/ root@192.168.45.128:/
- scp -r /tmp/ 192.168.45.128:/
第一种方式:指定远程用户名@指定IP:要复制的文件到./,这里./表示将远程服务器上的文件拷贝到本地服务器的当前路径。
scp remote_username@remote_ip:remote_file ./
第二种方式:指定远程用户名@指定IP:要复制的文件到本地服务器指定用户名,指定目录等
scp remote_username@remote_ip:remote_file local_username@local_ip:local_folder
案例演示:
- scp root@192.168.45.128:/usr/local/nginx/conf/nginx.conf
- /usr/local/conf/
- scp -r root@192.168.45.128:/usr/local/nginx/ root@192.168.45.166:/usr/local/
当然也可以拷贝目录,方法还是一样在前面加-r参数,这里就不再演示。
除了可以选择IP来拷贝文件,还可以使用主机名进行拷贝,前提需要先将hostname做hosts映射。
- cat /etc/hosts
- 192.168.45.166 localhost166
- 192.168.45.128 localhost128
-
- scp -r root@localhost128:/usr/local/nginx/ root@localhost166:/usr/local
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。