scp
字数 799 阅读 12 评论 6
SCP command is used to transfer files over computers in Linux.
Syntax
scp src_file username@dest_host:dest_folder
Common Options
- -v Provide detail information of scp process
1 | $ scp -v abc.txt username@192.169.1.7:. |
-p Provide modification times, access times, and modes from original files
-C Compress file when transferring.
“-C” parameter wil compress your files on the fly. The compression is only happen in the network. When the file is arrived to the destination server, it will be unzipped.
- -c Select another cipher to encrypt files
By default SCP uses “AES-128” to encrypt files. If you want to change to another cipher to encrypt it, you can use “-c” parameter.
$ scp -c 3des abc.txt username@192.168.1.7
Above command tells SCP to use 3-DES algorithm to encrypt file. Please be careful that this parameter using “-c” not “-C”.
- -l Limit bandwidth usage
The ‘-l’ parameter will limit the bandwidth usage. If you don’t want the bandwidth is drained by the SCP proces when you execute automation script to copy a lot of files, ‘-l’ option will be helpful.
$ scp -l 400 abc.txt username@192.168.1.7:.
The 400 value behind “-l” parameter means that the limitation of the bandwidth for SCP process is at maximum 50 KB/sec. Note that bandwidth is measured in Kilobits/sec (kbps) in SCP. It is mean that 8 bits equal with 1 byte.
therefore, if you want to limit your bandwidth for SCP maximum only 50 KB/s, you need to set it into 50 x 8 = 400.
- -P Specify specific port to use with SCP
Usually SCP is using port 22 as a default port. But for security reason, you may change the port into another port. For example, we are using port 2249. Then the command should be like this.
$ scp -P 2249 abc.txt username@192.168.1.7:.
- -r Copy files inside directory recursively
Sometimes we need to copy directory and all files / directories inside it. It will be better if we can do it in 1 command. SCP support that scenario using “-r” parameter.
$ scp -r documents abc.txt username@192.168.1.7:.
When the copy process is done, at the destination server you will found a directory named “documents” with all it’s files. The folder “documents” is automatically created.
Exclude some directories or files when use scp?
$ sudo -u search scp -r ~/project/[!.]* 192.168.1.7:.
or
$ sudo -u search rsync -rav -e ssh --include='~/project' --exclude='~/project/.svn' 192.168.56.177:
$ rsync -av --exclude='path1/to/exclude' --exclude='path2/to/exclude' source destination
Note that using source and source/ are different. A trailing slash means to copy the contents of the folder source into destination. Without the trailing slash, it means copy the folder source into destination.
For a svn managed project directory, all the sub-directories will have a hidden .svn directory, if we want to cp the project without these .svn directories, the best way is
us find with -prune option.
$ find . -type d -path '*/\.*' -prune -o -not -name '.svn' -print
or if we want to search some file in all sub-directories but not in .svn
directories, we can do this:
$ find . -type d -path '*/\.*' -prune -o -not -name '.svn' -type f -name '*filename*' -print
Now we can use scp
command cooperate with find
and grep
to exclude all the .svn
sub directories.
sudo -u search scp $(find ~/project -type d -path '*/\.*' -prune -o -type f -not -name '.svn'| grep -v .svn) 192.168.1.7:.
- -q Disable progress meter and warning / diagnostic message
If you choose not to see progress meter and warning / diagnostic messages from SCP, you may disable it using “-q” parameter.
Copy files using SCP through Proxy
Proxy server is usually used in office environment. Natively, SCP is not proxy configured.
Therefore, when our environment using proxy, we have to “tell” SCP to communicate with the proxy.
Take an example, suppose the proxy address is 10.0.96.6 and the proxy port is 8080. The proxy also implemented user authentication.
To use proxy we need to make sure we have installed corkscrew
tool.
$ apt-get install corkscrew
or
$ yum install corkscrew
Here is the procedure:
Create “~/.ssh/config” file.
Put the below command inside it.
ProxyCommand /usr/bin/corkscrew 10.0.96.6 8080 %h %p ~/.ssh/proxyauth
Create file “~/.ssh/proxyauth” which contain.
myusername:mypassword
After that we can execute scp
transparently as usual.
Another thing that since “~/.ssh/proxyauth” file contain your “username” and “password” in clear-text format, please make sure that the file can be accessed by you only.