centos平台scp通過密鑰遠程復制文件(免密登錄)


一,說明:兩台機器的平台和ip

1,a服務器:

centos8:ip:121.122.123.47

版本

[root@yjweb ~]# cat /etc/redhat-release
CentOS Linux release 8.0.1905 (Core) 

 

2,    j服務器:

centos6:ip:121.122.7.134

版本:

[root@os3 ~]# cat /etc/redhat-release
CentOS release 6.10 (Final)

 

我們的目示是通過scp程序把a服務器上的文件復制到j服務器上

說明:ip地址僅供演示,非真實

說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest

         對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/

 說明:作者:劉宏締 郵箱: 371125307@qq.com

 

二,在a服務器上生成密鑰:

1,生成密鑰

[root@yjweb ~]# ssh-keygen -t rsa

說明:中間一路回車即可,

不要設置密碼

 

說明:-t參數可以指定四種算法類型 

[-t dsa | ecdsa | ed25519 | rsa]

我們選擇 rsa

 

說明:查看參數可以使用通用的幫助命令:

[root@yjweb ~]# man ssh-keygen 

 

2,密鑰生成后,可以從用戶的home目錄下.ssh目錄看到

[root@yjweb ~]# ls .ssh/
authorized_keys  id_rsa  id_rsa.pub  known_hosts

 

三,從a服務器復制公鑰到j服務器

1,用ssh-copy-id命令復制公鑰到j服務器上

[root@yjweb ~]# ssh-copy-id -i .ssh/id_rsa.pub root@121.122.7.134
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: ".ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@121.122.7.134's password:
Number of key(s) added: 1
Now try logging into the machine, with:   "ssh 'root@121.122.7.134'"
and check to make sure that only the key(s) you wanted were added.

 

說明:ssh-copy-id屬於這個rpm包,如發現不存在可以通過yum安裝

[root@yjweb ~]# whereis ssh-copy-id
ssh-copy-id: /usr/bin/ssh-copy-id /usr/share/man/man1/ssh-copy-id.1.gz
[root@yjweb ~]# rpm -qf /usr/bin/ssh-copy-id
openssh-clients-8.0p1-4.el8_1.x86_64

 

2,登錄到j服務器,查看authorized_keys

 

[root@os3 ~]# more .ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDiZNKuLsJqi0M......

 

可以看到我們在a服務器的公鑰已經成功添加到了j服務器的 authorized_keys文件上

 

四,從a服務器用scp復制一個文件到j服務器

      注意看是否會提示輸入j服務器的密碼

1,檢查j服務器上的目標文件夾

[root@os3 test]# ll /data/dev/think_file/test/
total 0

 

2,在a服務器上執行scp命令

[root@yjweb orig]# scp -P 22 /data/web/think_file/cover/orig/1/10.gif root@121.122.7.134:/data/dev/think_file/test/

 

說明:

-P 22:   在此處指定目標服務器的端口
root 登錄目標服務器用的賬號
121.122.7.134 目標服務器用的ip
/data/dev/think_file/test/  在目標服務器上的路徑

 

3,回到j服務器,再次檢查測試用的目標文件夾

[root@os3 test]# ll /data/dev/think_file/test/
total 1300
-rw-r--r-- 1 root root 1330533 Mar 16 14:54 10.gif

 

五,如何在遠程服務器上創建目錄

[root@yjweb ~]# ssh -p 22 root@121.122.7.134 "mkdir -p /data/dev/think_file_test/cover/orig/1/"

說明: 

-p : 用來指定目標服務器的端口號

 

六,如何復制一整個目錄到目標機器?

1,加參數 -r即可

[root@yjweb web]# scp -P 22 -r /data/web/think_file/cover/orig/1/ root@121.122.7.134:/data/dev/think_file/test/
7.gif                                                                        100%  548KB 489.4KB/s   00:01
9.gif                                                                        100%  542KB 700.2KB/s   00:00
11.png                                                                       100%  997KB   1.4MB/s   00:00
10.gif                                                                       100% 1299KB   1.3MB/s   00:00
8.gif                                                                        100%  440KB 448.9KB/s   00:00

 

2,一點值得說明的知識:

-r      Recursively copy entire directories.  Note that scp follows symbolic links encountered in the tree traversal.

-r 用來復制整個目錄

 

需要注意的是scp會把符號鏈接下的文件也復制過來

如何避免這個問題?

可以先把包含符號鏈接的源目錄打包成tar包,
遠程復制tar包過來后再解開

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM