Linux實現(xiàn)免密登錄的配置方法
需求描述:192.168.31.10服務器的yunwei賬號,想要免密登陸到192.168.31.15服務器上。
直接ssh root@192.168.31.15這樣登陸,不用輸入密碼。
實現(xiàn):
1、在10機器上,創(chuàng)建運維賬號。
[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、復制密鑰到15服務器
[yunwei@docker01 .ssh]$ ssh-copy-id root@192.168.31.15 復制密鑰到15機器 /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、驗證免密登陸
[yunwei@docker01 .ssh]$ ssh root@192.168.31.15 Last login: Sun Mar 26 11:21:02 2023 from 192.168.31.1
補充:優(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”。
當使用 RSA CA 密鑰簽署證書時,此標志還可用于指定所需的簽名類型??捎玫?RSA 簽名變體是“ssh-rsa”(SHA1 簽名,不推薦)、“rsa-sha2-256”和“rsa-sha2-512”(默認值)
-P 密碼
提供(舊)密碼。
這里的密碼,是密鑰的密碼,不是遠程主機的密碼,隨便設置。但是,這就失去了免密登陸的意義。因為,設置了這個后,登陸遠程主機時,就必須輸入密鑰密碼。
所以,一般這個指指定為空即可。
-f 文件名
指定密鑰文件的文件名
這里的文件名,必須指定為id_rsa,不然,把密鑰推送到目標機器,依然無法實現(xiàn)免密登陸。
總結:
就三個命令
cd 進入當前賬號家目錄 ssh-keygen 連續(xù)三次回車 ssh-copy-id 192.168.31.15 復制公鑰到hadoop104服務器,這樣,就可以免密訪問hadoop104服務器
這里用戶賬號省略,則使用當前賬號進行免密登陸
比如,當前賬號是test
ssh-copy-id 192.168.31.15 等價與 ssh-copy-id test@192.168.31.15
實現(xiàn)的效果是,當前服務器的test賬號可以免密登陸15服務器的test賬號
參考資料:https://www.cnblogs.com/dirigent/p/16636545.html
到此這篇關于Linux實現(xiàn)免密登錄的配置方法的文章就介紹到這了,更多相關Linux免密登錄內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Centos7下編譯安裝配置Nginx+PHP+MySql環(huán)境
這篇文章主要介紹了Centos7下編譯安裝配置Nginx+PHP+MySql環(huán)境,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
linux環(huán)境配置nginx導致頁面不刷新的解決方法
這篇文章介紹了linux環(huán)境配置nginx導致頁面不刷新的解決方法,有需要的朋友可以參考一下2013-09-09

