1. 程式人生 > >遠程傳輸命令(十三)

遠程傳輸命令(十三)

服務器

遠程傳輸命令:scp,rsync


13.1.scp

功能:通貨ssh協議進行安全遠程服務器的文件復制

語法:

scp [OPTIONS] file_source file_target



幫助命令:

scp --help

man scp


常用選項:

-C 壓縮選項

-i 指定私鑰文件

-l 限制速率,單位Kb/s,1024Kb=1Mb

-P 指定遠程主機SSH端口

-p 保存修改時間、訪問時間和權限

-r 遞歸拷貝目錄

-o SSH選項,有以下常用的:

ConnectionAttempts=NUM 連接失敗後重試次數

ConnectTimeout=SEC 連接超時時間

StrictHostKeyChecking=no 自動拉去主機key文件

PasswordAuthentication=no 禁止密碼認證

-v:跟多數命令中-v一樣,用來顯示詳細信息


示例:

[[email protected] ~]# scp system.sh [email protected]:/root  #本地復制文件到遠程,並指定遠程以root登錄
The authenticity of host ‘192.168.19.51 (192.168.19.51)‘ can‘t be established.
RSA key fingerprint is 14:1a:ee:3c:8c:14:6d:4a:fd:d8:c7:c6:ea:87:0c:de.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.19.51‘ (RSA) to the list of known hosts.
[email protected]
/* */ password: system.sh 100% 7273 7.1KB/s 00:00 [[email protected] ~]# scp dir001 [email protected]:/root #復制目錄不加參數就會報錯 \[email protected] password: dir001: not a regular file [[email protected] ~]# scp -r dir001 [email protected]
:/root #所以加-r目錄才會復制過去 [email protected] password: [[email protected] ~]#

說明:舉幾個常用的例子,其他參數後續再完善


13.2.rsync

功能:一種快速的、通用的、遠程的(本地)文件復制工具,據說是scp速度的20倍

常用選項:

-v 顯示復制信息

-q 不輸出錯誤信息

-c 跳過基礎效驗,不判斷修改時間和大小

-a 歸檔模式,等效-rlptgoD,保留權限、屬組等

-r 遞歸目錄

-l 拷貝軟連接

-z 壓縮傳輸數據

-e 指定遠程shell,比如ssh、rsh

--progress 進度條,等同-P

--bwlimit=KB/s 限制速率,0為沒有限制

--delete 刪除那些DST中SRC沒有的文件

--exclude=PATTERN 排除匹配的文件或目錄

--exclude-from=FILE 從文件中讀取要排除的文件或目錄

--password-file=FILE 從文件讀取遠程主機密碼

--port=PORT 監聽端口


示例:

[[email protected] ~]# ls . /opt
.:
anaconda-ks.cfg  dir003  dir006  dir009       install.log.syslog
dir001           dir004  dir007  dir010       system.sh
dir002           dir005  dir008  install.log
/opt:      #這個目錄沒有文件
[[email protected] ~]# rsync -av * /opt   #把當前目錄所有文件同步到opt目錄下
sending incremental file list 
anaconda-ks.cfg
install.log
install.log.syslog
system.sh
dir001/
dir002/
dir003/
dir004/
dir005/
dir006/
dir007/
dir008/
dir009/
dir010/
 
sent 31002 bytes  received 128 bytes  62260.00 bytes/sec
total size is 30574  speedup is 0.98
[[email protected] ~]# ls /opt   #現在有文件了
anaconda-ks.cfg  dir003  dir006  dir009       install.log.syslog
dir001           dir004  dir007  dir010       system.sh
dir002           dir005  dir008  install.log
[[email protected] ~]# rsync -av * 192.168.19.51:/opt    #本地目錄所有文件同步到遠端
[[email protected] ~]# rsync -av --delete * 192.168.19.51:/opt  #保持遠程主機目錄與本地一樣

本文出自 “爛筆頭” 博客,請務必保留此出處http://lanbitou.blog.51cto.com/9921494/1934260

遠程傳輸命令(十三)