作者介紹:在簡歷上沒有標榜自己為精通的運維工程師。請點擊上方的藍色《運維小路》關注我,下面的思維導圖展示了計劃更新的內容和當前進度(不定期更新)。
本小節內容屬于linux進階部分的日常運維內容,掌握這些日常運維技巧或方法將在我們的日常運維過程中帶來諸多便利。主要從以下幾個部分進行講解:
Linux日常運維-主機名&hosts
Linux日常運維-history
Linux日常運維-sshD(一)(本章節)
Linux日常運維-SSHD(二)
Linux日常運維-ENV(一)
Linux日常運維-ENV(二)
Linux日常運維-任務計劃
我們之前討論了如何通過ssh進行登錄,通常是使用密碼進行的。然而,在ssh登錄過程中還有幾個常見的問題,比如我們是否可以不使用ssh密碼,而采用一種無密碼的方式進行登錄(目前許多公有云平臺在創建新的云主機時,可以選擇使用密碼還是密鑰),以及ssh端口默認是22,是否可以修改,或者為了安全,是否可以禁止root登錄。
無密碼登錄時通常會涉及到兩個概念:私鑰和公鑰,它們是一對的。私鑰存放在本地服務器中,而公鑰則存放在我們希望無密碼登錄的服務器中。如果從云平臺開通主機時選擇密鑰登錄,你將獲得一個文件,這就是私鑰。如果你不想使用系統生成的密鑰,你也可以上傳自己定義的公鑰。
在實際運維中,某些軟件要求配置無密碼登錄才能更好地運行,甚至必須使用無密碼登錄。還可以設置服務器使用一個你自己都不知道或者是空的密碼,然后通過密鑰登錄來提高安全性。
配置無密碼登錄
1.生成公鑰及私鑰
# 輸入該命令后,一直按回車即可。 # 這里也可以添加許多參數,甚至可以設置密鑰的密碼。 [root@localhost ~]# ssh-keygen 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:4jzW3nqWv4wxN7j4v0LjFI+DUuYFVQ1xeiS6fUF3yGo root@localhost.localdomain The key's randomart image is: +---[RSA 2048]----+ | ...==+.o| | . . *+..| | .. ..o | | o ooE. . | | .+So.=. . | | o.oo *... | | =..+++o | | . o o*B . | | +==+*o | +----[SHA256]-----+
2.查看公鑰和私鑰
[root@localhost]# ls -l /root/.ssh/ 總用量 8 -rw-------. 1 root root 1675 6月 28 22:57 id_rsa //私鑰 -rw-r--r--. 1 root root 408 6月 28 22:57 id_rsa.pub //公鑰
3.將公鑰傳輸到其他服務器
# 需要輸入對方服務器的密碼 # 也可以手動將id_rsa.pub文件的內容放到/root/.ssh/authorized_keys中,注意權限 # 我們將公鑰傳輸到108服務器后,就可以無密碼登錄108服務器了 [root@localhost .ssh]# ssh-copy-id 192.168.179.108 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.179.108 (192.168.179.108)' can't be established. ECDSA key fingerprint is SHA256:KcCHSnldK0xz0g82jmvKQ5bFYI8Wn04rPYbmK94nwhQ. ECDSA key fingerprint is MD5:f3:3b:6d:09:54:06:57:b0:13:3a:75:e1:ca:79:70:bd. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/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@192.168.179.108's password: Number of key(s) added: 1 <p>Now try logging into the machine, with: "ssh '192.168.179.108'" and check to make sure that only the key(s) you wanted were added.
4.查看遠程服務器的公鑰
# 雖然公鑰在原始服務器的名字和內容是/root/.ssh/id_rsa.pub,但在復制到新服務器后名字會變成authorized_keys</p><h1>如果該服務器允許多個其他機器無密碼登錄,則該文件會有多行。</h1><p>[root@localhost .ssh]# cd /root/.ssh/ [root@localhost .ssh]# ls -l 總用量 4 -rw-------. 1 root root 408 6月 28 23:02 authorized_keys
5.驗證無密碼登錄
# 未輸入密碼,也可以直接登錄。</p><h1>登錄時,它會讀取本地的私鑰和服務器中的公鑰進行驗證,驗證通過則登錄成功。</h1><p>[root@localhost .ssh]# ssh 192.168.179.108 Last login: Fri Jun 28 23:09:05 2024 from 192.168.179.1 [root@localhost ~]#
總結
1.這里所有的操作都是基于root賬號進行的,實際上也可以使用普通賬號配置無密碼登錄。
2.將私鑰帶到windows上也可以成功登錄服務器,使用ssh軟件并選擇密鑰登錄即可。
3.如果不進行任何操作,它同時支持密碼和密鑰兩種登錄方式。也就是說,可以使用密鑰登錄,也可以使用密碼登錄。
4.同一臺服務器,可以配置多個公鑰,允許不同的私鑰使用無密碼登錄。在實際的authorized_keys文件中,這會體現為多行。
5.使用密鑰登錄可以避免密碼被撞庫的風險,從而提高系統的安全性。