Linux實(shí)現(xiàn)免密登錄的配置方法
需求描述:192.168.31.10服務(wù)器的yunwei賬號,想要免密登陸到192.168.31.15服務(wù)器上。
直接ssh root@192.168.31.15這樣登陸,不用輸入密碼。
實(shí)現(xiàn):
1、在10機(jī)器上,創(chuàng)建運(yùn)維賬號。
[root@docker01 ~]# id yunwei 檢查yunwei賬號是否存在 id: yunwei: no such user [root@docker01 ~]# useradd yunwei 創(chuàng)建yunwei賬號 [root@docker01 ~]# su - yunwei 切換到y(tǒng)unwei賬號 [yunwei@docker01 ~]$ pwd /home/yunwei
2、在yunwei賬號下創(chuàng)建密鑰
[yunwei@docker01 ~]$ ssh-keygen 創(chuàng)建密鑰,一路回車 Generating public/private rsa key pair. Enter file in which to save the key (/home/yunwei/.ssh/id_rsa): Created directory '/home/yunwei/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/yunwei/.ssh/id_rsa. Your public key has been saved in /home/yunwei/.ssh/id_rsa.pub. The key fingerprint is: SHA256:kLXaRvzgGOqF62RyGWKGUekspD39l0pudQBt1MQp3NU yunwei@docker01 The key's randomart image is: +---[RSA 2048]----+ | .. +o=.o.. | | o. .+=.= E | |++ . +o=. | |oo= .o O.o | |..+.+.+ So. | | o o =o.+ . | | . Bo + . | | * + | | .. | +----[SHA256]-----+ 檢查密鑰是否創(chuàng)建成功 [yunwei@docker01 ~]$ pwd /home/yunwei [yunwei@docker01 ~]$ ll -a total 12 drwx------. 5 yunwei yunwei 103 Mar 25 23:18 . drwxr-xr-x. 16 root root 177 Mar 25 23:17 .. -rw-r--r--. 1 yunwei yunwei 18 Mar 31 2020 .bash_logout -rw-r--r--. 1 yunwei yunwei 193 Mar 31 2020 .bash_profile -rw-r--r--. 1 yunwei yunwei 231 Mar 31 2020 .bashrc drwxrwxr-x. 3 yunwei yunwei 18 Mar 25 23:17 .cache drwxrwxr-x. 3 yunwei yunwei 18 Mar 25 23:17 .config drwx------. 2 yunwei yunwei 38 Mar 25 23:18 .ssh [yunwei@docker01 ~]$ cd .ssh/ [yunwei@docker01 .ssh]$ ls id_rsa id_rsa.pub
3、復(fù)制密鑰到15服務(wù)器
[yunwei@docker01 .ssh]$ ssh-copy-id root@192.168.31.15 復(fù)制密鑰到15機(jī)器 /bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/yunwei/.ssh/id_rsa.pub" The authenticity of host '192.168.31.15 (192.168.31.15)' can't be established. ECDSA key fingerprint is SHA256:v3zhW/rvSt+T7QfAnIDIiHhbALRLNiLzl8Hg3TAZQCA. ECDSA key fingerprint is MD5:cf:b8:e1:f6:a5:61:60:f0:77:aa:f3:76:ab:d2:ce:9b. Are you sure you want to continue connecting (yes/no)? yes /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@192.168.31.15's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.31.15'" and check to make sure that only the key(s) you wanted were added.
4、驗(yàn)證免密登陸
[yunwei@docker01 .ssh]$ ssh root@192.168.31.15 Last login: Sun Mar 26 11:21:02 2023 from 192.168.31.1
補(bǔ)充:優(yōu)化密鑰創(chuàng)建方式,免交互創(chuàng)建密鑰
[yunwei@docker01 .ssh]$ ssh-keygen -P '' -f id_rsa 免交互方式,創(chuàng)建密鑰 Generating public/private rsa key pair. Your identification has been saved in id_rsa. Your public key has been saved in id_rsa.pub. The key fingerprint is: SHA256:hXuSBtV1o1D1PfIyG/+iC1IFnZh8Q3NGf5eiuQ8IExQ yunwei@docker01 The key's randomart image is: +---[RSA 2048]----+ | EoooB=+B | | .. .=o=* +o| | ... ..o+ o*| | ..+ .o + +| | oS oo + . | | .o+. . * | | ...o . . | | . .o . .| | o+ ..| +----[SHA256]-----+ [yunwei@docker01 .ssh]$ ls id_rsa id_rsa.pub known_hosts
參數(shù)說明:
-t 指定要創(chuàng)建的密鑰類型
dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | RSA
可能的值為“dsa”、“ecdsa”、“ecdsa-sk”、“ed25519”、“ed25519-sk”或“rsa”。
當(dāng)使用 RSA CA 密鑰簽署證書時(shí),此標(biāo)志還可用于指定所需的簽名類型??捎玫?RSA 簽名變體是“ssh-rsa”(SHA1 簽名,不推薦)、“rsa-sha2-256”和“rsa-sha2-512”(默認(rèn)值)
-P 密碼
提供(舊)密碼。
這里的密碼,是密鑰的密碼,不是遠(yuǎn)程主機(jī)的密碼,隨便設(shè)置。但是,這就失去了免密登陸的意義。因?yàn)?,設(shè)置了這個后,登陸遠(yuǎn)程主機(jī)時(shí),就必須輸入密鑰密碼。
所以,一般這個指指定為空即可。
-f 文件名
指定密鑰文件的文件名
這里的文件名,必須指定為id_rsa,不然,把密鑰推送到目標(biāo)機(jī)器,依然無法實(shí)現(xiàn)免密登陸。
總結(jié):
就三個命令
cd 進(jìn)入當(dāng)前賬號家目錄 ssh-keygen 連續(xù)三次回車 ssh-copy-id 192.168.31.15 復(fù)制公鑰到hadoop104服務(wù)器,這樣,就可以免密訪問hadoop104服務(wù)器
這里用戶賬號省略,則使用當(dāng)前賬號進(jìn)行免密登陸
比如,當(dāng)前賬號是test
ssh-copy-id 192.168.31.15 等價(jià)與 ssh-copy-id test@192.168.31.15
實(shí)現(xiàn)的效果是,當(dāng)前服務(wù)器的test賬號可以免密登陸15服務(wù)器的test賬號
參考資料:https://www.cnblogs.com/dirigent/p/16636545.html
到此這篇關(guān)于Linux實(shí)現(xiàn)免密登錄的配置方法的文章就介紹到這了,更多相關(guān)Linux免密登錄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Centos7下編譯安裝配置Nginx+PHP+MySql環(huán)境
這篇文章主要介紹了Centos7下編譯安裝配置Nginx+PHP+MySql環(huán)境,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
linux環(huán)境配置nginx導(dǎo)致頁面不刷新的解決方法
這篇文章介紹了linux環(huán)境配置nginx導(dǎo)致頁面不刷新的解決方法,有需要的朋友可以參考一下2013-09-09
關(guān)于安裝LNMP集成包后上傳圖片報(bào)500錯誤的解決方法
這篇文章主要給大家介紹了關(guān)于安裝LNMP集成包后上傳圖片報(bào)500錯誤的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
在Linux系統(tǒng)下上傳項(xiàng)目到碼云的方法
今天小編就為大家分享一篇關(guān)于在Linux系統(tǒng)下上傳項(xiàng)目到碼云的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10

