關註上方 浩道Linux ,回復 資料 ,即可獲取海量 L inux 、 Python 、 網路通訊、網路安全 等學習資料!
前言
大家好,這裏是 浩道Linux ,主要給大家分享 L inux 、 P ython 、 網路通訊、網路安全等 相關的IT知識平台。
今天浩道跟大家分享一下公司實習生近來的成長情況。本月在對實習生進行半年考核時,在向實習生咨詢ssh登入互信配置時,他竟然表示沒有使用過,更不懂其使用的場景。這有點讓我意外,畢竟實習生平時接觸伺服器也不少。本文就針對這個知識點給大家來一次ssh登入互信配置實操,讓大家都掌握這個技能,一起看看吧!
一、ssh登入互信使用場景
Linux主機互信配置的使用場景在集群環境中套用非常廣泛,可以在各種需要多台主機之間進行安全通訊和數據交換的場景中套用。
1、自動化運維任務: 在自動化運維任務中,經常需要透過SSH連線到多台主機並執行命令或指令碼。配置雙向互信可以使運維工程師在執行任務時免去重復輸入密碼的步驟,提高工作效率。
2、遠端登入和管理: 當需要在多台主機之間進行遠端登入和管理時,配置互信關系可以方便地實作無需輸入密碼即可登入對方主機的需求,提高了管理效率。
3、檔傳輸和同步: 透過配置互信關系,可以在多台主機之間安全地傳輸和同步檔,保證數據的安全性和完整性。
4、網路服務和套用部署: 在網路服務和套用部署中,經常需要在多台主機之間進行配置和協調。配置互信關系可以方便地實作主機之間的安全通訊和數據交換。
5、容器和虛擬化技術: 在容器和虛擬化技術的使用中,經常需要在宿主機和虛擬機器之間進行通訊和管理。配置互信關系可以提供安全可靠的通訊通道,保證數據的安全性和私密性。
二、ssh登入互信配置原理
Linux主機互信配置的原理主要是透過公鑰認證來實作的。公鑰認證是一種基於RSA演算法的安全認證方式,它使用一對金鑰來進行加密和解密。其中,公鑰用於加密數據,而私鑰用於解密數據。
在Linux主機互信配置中,兩台主機之間會交換公鑰,並保存在對方的信任儲存中。當其中一台主機向另一台主機發送數據時,它可以使用對方的公鑰對數據進行加密,然後發送給對方。對方接收到數據後,使用自己的私鑰進行解密,從而獲取原始數據。
這種認證方式的好處在於,不需要每次都在輸入密碼進行驗證,只要雙方都信任對方,就可以直接進行通訊。這種方式大大簡化了多台機器之間切換登陸的操作,提高了工作效率。
為了實作Linux主機互信配置,需要遵循以下步驟:
1、在每台需要配置互信的主機上生成一對公鑰和私鑰。
2、 將其中一台主機的公鑰復制到另一台主機的信任儲存中。
3、透過SSH登入伺服器,驗證SSH登入互信是否配置成功。
透過以上步驟,兩台Linux主機就可以建立互信關系,實作無需輸入密碼即可直接登入對方主機的目標。以下為簡單配置原理拓撲圖:
三、ssh互信配置實作及驗證
(一)配置環境
兩台主機分別是haodaolinux01的ip是192.168.20.231,haodaolinux02的ip是192.168.20.232
(二)配置步驟
1、在伺服器1上生成公鑰及私鑰對
執行如下命令完成操作即可, 提示輸入資訊時直接回車,如下所示:
[root@haodaolinux01 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:qhWqG7kkY2/YrMUxetlxBktvx+B8N42Sh1j3usV8lco root@haodaolinux01
The key's randomart image is:
+---[RSA 2048]----+
| |
| |
| o . . . |
| . * = + + .|
| o o.OSB = o ..|
| o.=.=oo + * . .|
|ooB=..o . E . |
|.=+B o o . |
| .*o. . |
+----[SHA256]-----+
[root@haodaolinux01 ~]#
完成以上操作後,即在伺服器1上生成公鑰及私鑰檔了。
生成ssh的公鑰及秘鑰將預設儲存在家目錄下的.ssh/目錄下。如下所示:
[root@haodaolinux01 ~]# cd .ssh/
[root@haodaolinux01 .ssh]# ll
總用量 8
-rw-------. 1 root root 16752月 200:44 id_rsa
-rw-r--r--. 1 root root 4002月 200:44 id_rsa.pub
其中公鑰檔 id_rsa.pub 的許可權為644,私鑰檔 id_rsa 的許可權為600,同時 .ssh 目錄許可權是700。
2 、在伺服器2上生成公鑰及私鑰對
執行如下命令完成操作即可, 提示輸入資訊時直接回車,如下所示:
[root@haodaolinux02 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:7oteuYB6biFIbcwW3LqrHgRlcMJQ7UxoluB3Fn9IGN4 root@haodaolinux02
The key's randomart image is:
+---[RSA 2048]----+
|B+*+.oo. |
|.*=o+o= . |
|.++=o+ E . |
|...B= . |
|.oo . S |
|o .... . . |
| . .o.. + |
| .oo = . |
|.oo+..o +. |
+----[SHA256]-----+
[root@haodaolinux02 ~]#
完成以上操作後,即在伺服器2上生成公鑰及私鑰檔了。
生成ssh的公鑰及秘鑰將預設儲存在家目錄下的.ssh/目錄下。如下所示:
[root@haodaolinux02 ~]# cd .ssh/
[root@haodaolinux02 .ssh]# ll
總用量 8
-rw-------. 1 root root 16752月 200:45 id_rsa
-rw-r--r--. 1 root root 4002月 200:45 id_rsa.pub
[root@haodaolinux02 .ssh]#
其中公鑰檔 id_rsa.pub 的許可權為644,私鑰檔 id_rsa 的許可權為600,同時 .ssh 目錄許可權是700。
3、在伺服器1上將自己的公鑰檔發送給伺服器2
以下透過 ssh-copy-id命令完成。ssh-copy-id命令會將原生的公鑰檔復制到遠端主機的使用者主目錄下的.ssh資料夾中,並重新命名成authorized_keys檔。同時,ssh-copy-id命令還會設定適當的檔許可權,以確保遠端主機只能由具有對應私鑰的使用者存取。
[root@haodaolinux01 .ssh]# ssh-copy-id 192.168.20.232
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.20.232 (192.168.20.232)' can't be established.
ECDSA key fingerprint is SHA256:4iwLqnweCyu1vfr/NnjXzqWc6bq/NELno+uOmdiITlQ.
ECDSA key fingerprint is MD5:89:10:36:9b:4e:1b:28:99:a2:84:3f:65:a7:c6:17:94.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the newkey(s), to filter outany that are already installed
/usr/bin/ssh-copy-id: INFO: 1key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.20.232's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.20.232'"
and check to make sure that only the key(s) you wanted were added.
[root@haodaolinux01 .ssh]#
4 、在伺服器2上將自己的公鑰檔發送給伺服器1
同上步驟, 以下透過 ssh-copy-id命令完成。
[root@haodaolinux02 .ssh]# ssh-copy-id 192.168.20.231
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.20.231 (192.168.20.231)' can't be established.
ECDSA key fingerprint is SHA256:4iwLqnweCyu1vfr/NnjXzqWc6bq/NELno+uOmdiITlQ.
ECDSA key fingerprint is MD5:89:10:36:9b:4e:1b:28:99:a2:84:3f:65:a7:c6:17:94.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the newkey(s), to filter outany that are already installed
/usr/bin/ssh-copy-id: INFO: 1key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.20.231's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.20.231'"
and check to make sure that only the key(s) you wanted were added.
[root@haodaolinux02 .ssh]#
5、在伺服器1及伺服器2上檢視公鑰檔復制情況,如上述一樣。
伺服器1上如下所示:
[root@haodaolinux01 .ssh]# pwd
/root/.ssh
[root@haodaolinux01 .ssh]# ll
總用量 16
-rw-------. 1 root root 4002月 201:08 authorized_keys
-rw-------. 1 root root 16752月 200:44 id_rsa
-rw-r--r--. 1 root root 4002月 200:44 id_rsa.pub
-rw-r--r--. 1 root root 1762月 201:05 known_hosts
[root@haodaolinux01 .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDFP0aUD/ki8Q2dTarE7txT00VVVPawxyojM76XlSVpoaJuhBRhfIcIs2DUc2SYChnnC89WqtI5ygqY5ig2M1TUMwe7N4GHB401+U2cDn6Op48TqJHEmbk5BYKT0+dj39c5My+l6F6mgtcOANzddqqXre6/I8kAbFWTEDAtAn6ynw+BWH1FhTusTnX6vjU6ErQj9VCYXBCT+4tm9gWhvMtY0ywV1Af0HToVVUtZLmV8m1nbhPl9/WZVj1LML12WRUQxoTRn9SVjPJj5FBai7k42MhSwMauiceGCQF9BDuQ+gWeEaOpVXCdG+9O86st0sg6vg5P1dGIyEmhGtyCe0tPN root@haodaolinux02
[root@haodaolinux01 .ssh]#
伺服器2上如下所示:
[root@haodaolinux02 .ssh]# pwd
/root/.ssh
[root@haodaolinux02 .ssh]# ll
總用量 16
-rw-------. 1 root root 4002月 201:06 authorized_keys
-rw-------. 1 root root 16752月 200:45 id_rsa
-rw-r--r--. 1 root root 4002月 200:45 id_rsa.pub
-rw-r--r--. 1 root root 1762月 201:07 known_hosts
[root@haodaolinux02 .ssh]# cat authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDNq5mGx/Z0pfgZk0HOxbO6iqBJEW4ZinZHM2nxJ1xQHiU/ldufW2NCpgd3HXfAKOa28uz3y5NLE4m4/lhY4XC5oSWJ4eiqxpJUdacu65u9i+RafyAXFprsCh5ortzGwPRn7DSEBMk7XQNg5+JHcMG53xloWgZOFPVYPJjlK1SexwhSmmGKZL/vCvMfyNDn8d8idyM4z5woRU8Qk2WL1F3JCNoNjnAttpntI7g2dGUnJwkjMS7GYvHSb9fyCMg8fZyIKYFetS5uKyGNVQ3Z5esP95hukSJBi5rt3acwh1ADexMryhuWKKCFbrQmptQem+WSH6KpT3afI/WRtKwaQ18F root@haodaolinux01
[root@haodaolinux02 .ssh]#
(三)驗證ssh登入互信配置成功與否
完成上述配置後,即可分別在伺服器1和伺服器2上透過ssh協定登入對方。
伺服器1上登入伺服器2,如下所示,無需密碼即可登入。
[root@haodaolinux01 .ssh]# ssh 192.168.20.232
Lastlogin: FriFeb 2 00:14:10 2024 from 192.168.20.122
[root@haodaolinux02 ~]#
伺服器2上登入伺服器1 ,如下所示,無需密碼即可登入。
[root@haodaolinux02 .ssh]# ssh 192.168.20.231
Lastlogin: FriFeb 2 00:14:09 2024 from 192.168.20.122
[root@haodaolinux01 ~]#
由此已成功完成ssh登入配置驗證。
更多精彩
關註公眾號 「 浩道Linux 」
浩道Linux ,專註於 Linux系統 的相關知識、 網路通訊 、 網路安全 、 Python相關 知識以及涵蓋IT行業相關技能的學習, 理論與實戰結合,真正讓你在學習工作中真正去用到所學。同時也會分享一些面試經驗,助你找到高薪offer,讓我們一起去學習,一起去進步,一起去漲薪!期待您的加入~~~ 關註回復「資料」可 免費獲取學習資料 (含有電子書籍、視訊等)。
喜歡的話,記得 點「贊」 和 「在看」 哦