作為系統(tǒng)管理員,你計劃在 Linux 上使用 OpenSSH,完成日常工作的自動化,比如文件傳輸、備份數(shù)據(jù)庫轉(zhuǎn)儲文件到另一臺服務(wù)器等。為實現(xiàn)該目標(biāo),你需要從主機 A 能自動登錄到主機 B。自動登錄也就是說,要在 shell 腳本中使用ssh,而無需要輸入任何密碼。
本文會告訴你怎樣在 CentOS/RHEL 上設(shè)置 SSH 免密碼登錄。自動登錄配置好以后,你可以通過它使用 SSH (Secure Shell)和安全復(fù)制 (SCP)來移動文件。
SSH 是開源的,是用于遠程登錄的最為可靠的網(wǎng)絡(luò)協(xié)議。系統(tǒng)管理員用它來執(zhí)行命令,以及通過 SCP 協(xié)議在網(wǎng)絡(luò)上向另一臺電腦傳輸文件。
通過配置 SSH 免密碼登錄,你可以享受到如下的便利:
ssh-keygen 是一個用來生成、創(chuàng)建和管理 SSH 認(rèn)證用的公私鑰的工具。通過 ssh-keygen 命令,用戶可以創(chuàng)建支持SSH1 和 SSH2 兩個協(xié)議的密鑰。ssh-keygen 為 SSH1 協(xié)議創(chuàng)建 RSA 密鑰,SSH2 則可以是 RSA 或 DSA。
ssh-copy-id 是用來將本地公鑰拷貝到遠程的 authorizedkeys 文件的腳本命令,它還會將身份標(biāo)識文件追加到遠程機器的 ~/.ssh/authorizedkeys 文件中,并給遠程主機的用戶主目錄適當(dāng)?shù)牡臋?quán)限。
SSH 密鑰為登錄 Linux 服務(wù)器提供了更好且安全的機制。運行 ssh-keygen 后,將會生成公私密鑰對。你可以將公鑰放置到任意服務(wù)器,從持有私鑰的客戶端連接到服務(wù)器的時,會用它來解鎖。兩者匹配時,系統(tǒng)無需密碼就能解除鎖定。
以下步驟在 CentOS 5/6/7、RHEL 5/6/7 和 Oracle Linux 6/7 上測試通過。
節(jié)點1 : 192.168.0.9 節(jié)點2 : 192.168.l.10
測試節(jié)點1到節(jié)點2的連接和訪問:
[root@node1 ~]#ssh root@192.168.0.10
The authenticity of host '192.168.0.10 (192.168.0.10)' can't be established.
RSA key fingerprint is 6d:8f:63:9b:3b:63:e1:72:b3:06:a4:e4:f4:37:21:42.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.10' (RSA) to the list of known hosts.
root@192.168.0.10's password:
Lastlogin:ThuDec1022:04:552015from192.168.0.1
[root@node2 ~]#
使用 ssh-key-gen 命令生成公鑰和私鑰,這里要注意的是可以對私鑰進行加密保護以增強安全性。
[root@node1 ~]#ssh-keygen
Generatingpublic/private rsa key pair.
Enterfilein which to save the key (/root/.ssh/id_rsa):
Enter passphrase (emptyforno passphrase):
Enter same passphrase again:
Your identification has been saved in/root/.ssh/id_rsa.
Yourpublic key has been saved in/root/.ssh/id_rsa.pub.
The key fingerprint is:
b4:51:7e:1e:52:61:cd:fb:b2:98:4b:ad:a1:8b:31:6d root@node1.ehowstuff.local
The key's randomart image is:
+--[ RSA 2048]----+
| . ++ |
| o o o |
| o o o . |
| . o + .. |
| S . . |
| . .. .|
| o E oo.o |
| = ooo. |
| . o.o. |
+-----------------+
用 ssh-copy-id 命令將公鑰復(fù)制或上傳到遠程主機,并將身份標(biāo)識文件追加到節(jié)點2的 ~/.ssh/authorized_keys 中:
[root@node1 ~]#ssh-copy-id-i ~/.ssh/id_rsa.pub 192.168.0.10
root@192.168.0.10's password:
Now try logging into the machine, with "ssh '192.168.0.10'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
2015職稱計算機考試書PowerPoint2007中 .. 定價:¥45 優(yōu)惠價:¥42 更多書籍 | |
2015年全國職稱計算機考試教材(2007模 .. 定價:¥225 優(yōu)惠價:¥213 更多書籍 |