記一次ssh免密登錄踩坑and Debug之路

突然覺(jué)得服務(wù)器ssh密碼登錄總是浪費(fèi)一定量的時(shí)間,就想試試用sshKey進(jìn)行登錄普筹。

  1. 生成服務(wù)器sshkey和本地sshkey
$ ssh-keygen
  1. 在服務(wù)器上生成一個(gè)authorized_keys文件,然后將本地public Key 存到生成的文件中呻待。
  2. 設(shè)置 /etc/ssh/sshd.config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile  .ssh/authorized_keys
  1. 重啟ssh服務(wù)
service sshd restart
  1. 將~/.ssh 文件權(quán)限設(shè)置為其他用戶不可讀寫(xiě)薇组。也就是700、600郑兴、400權(quán)限

以上這樣全部設(shè)置完之后犀斋,發(fā)現(xiàn)還是無(wú)法ssh免密登錄,name是為什么呢情连,我打開(kāi)了openssh的官網(wǎng)開(kāi)始尋找答案叽粹。

  1. 我開(kāi)始尋找ssh日志,查找問(wèn)題所在
    我們先看ssh客戶端的日志
ssh root@xxxx -v

執(zhí)行之后輸出以下日志

OpenSSH_7.5p1, LibreSSL 2.5.4
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 52: Applying options for *
debug1: Connecting to 121.42.42.155 [121.42.42.155] port 22.
debug1: Connection established.
debug1: identity file /Users/yard/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yard/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.5
debug1: Remote protocol version 1.99, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH_5* compat 0x0c000000
debug1: Authenticating to 121.42.42.155:22 as 'root'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: diffie-hellman-group-exchange-sha256
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: umac-64@openssh.com compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: umac-64@openssh.com compression: none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(2048<3072<8192) sent
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: ssh-rsa SHA256:tp4/4hwf0Q39NLrFmXNC438rpEPBEqy4C+CXCDy91xE
debug1: Host '121.42.42.155' is known and matches the RSA host key.
debug1: Found key in /Users/yard/.ssh/known_hosts:1
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /Users/yard/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /Users/yard/.ssh/id_dsa
debug1: Trying private key: /Users/yard/.ssh/id_ecdsa
debug1: Trying private key: /Users/yard/.ssh/id_ed25519
debug1: Next authentication method: password

我發(fā)現(xiàn)本機(jī)電腦提供了id_rsa文件去服務(wù)器認(rèn)證却舀,但是認(rèn)證沒(méi)有通過(guò)虫几。

  1. 我們?cè)賮?lái)看服務(wù)端的日志
    我們重新開(kāi)一個(gè)ssh的服務(wù)器端口
/usr/sbin/sshd -d -p 2222

然后再通過(guò)本機(jī)ssh去連接服務(wù)器

ssh root@xxxx -p 2222 -v

以下是服務(wù)器sshd日志

debug1: Server will not fork when running in debugging mode.
debug1: rexec start in 4 out 4 newsock 4 pipe -1 sock 7
debug1: inetd sockets after dupping: 3, 3
Connection from 122.234.57.180 port 50213
debug1: Client protocol version 2.0; client software version OpenSSH_7.5
debug1: match: OpenSSH_7.5 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-1.99-OpenSSH_5.3
debug1: permanently_set_uid: 74/74
debug1: list_hostkey_types: ssh-rsa,ssh-dss
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: client->server aes128-ctr umac-64@openssh.com none
debug1: kex: server->client aes128-ctr umac-64@openssh.com none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST received
debug1: SSH2_MSG_KEX_DH_GEX_GROUP sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_INIT
debug1: SSH2_MSG_KEX_DH_GEX_REPLY sent
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: KEX done
debug1: userauth-request for user root service ssh-connection method none
debug1: attempt 0 failures 0
debug1: PAM: initializing for "root"
debug1: PAM: setting PAM_RHOST to "122.234.57.180"
debug1: PAM: setting PAM_TTY to "ssh"
debug1: userauth-request for user root service ssh-connection method publickey
debug1: attempt 1 failures 0
debug1: test whether pkalg/pkblob are acceptable
debug1: temporarily_use_uid: 0/0 (e=0/0)
debug1: trying public key file /root/.ssh/authorized_keys
debug1: fd 4 clearing O_NONBLOCK
Authentication refused: bad ownership or modes for directory /root
debug1: restore_uid: 0/0
debug1: temporarily_use_uid: 0/0 (e=0/0)
debug1: trying public key file /root/.ssh/authorized_keys
debug1: fd 4 clearing O_NONBLOCK
Authentication refused: bad ownership or modes for directory /root
debug1: restore_uid: 0/0
Failed publickey for root from 122.234.57.180 port 50213 ssh2

我發(fā)現(xiàn)在認(rèn)證的時(shí)候出現(xiàn)了這句話

 Authentication refused: bad ownership or modes for directory /root

這個(gè)錯(cuò)誤提示的意識(shí)是/root文件夾的文件權(quán)限有問(wèn)題
在google上查找這個(gè)錯(cuò)誤提示后知道是因?yàn)閪/.ssh文件需要只供root讀寫(xiě),其他用戶都不可以用寫(xiě)的權(quán)限挽拔,然后導(dǎo)致/root/.ssh/authorized_keys無(wú)法讀取辆脸,也就導(dǎo)致了認(rèn)證不通過(guò)。
解決方法也很簡(jiǎn)單螃诅,給/root文件加上700權(quán)限就好了啡氢。

雖然寫(xiě)的很簡(jiǎn)單,但整個(gè)debug時(shí)間超過(guò)2小時(shí)术裸,主要是因?yàn)閛penssh軟件比較古老倘是,資料相對(duì)難找。袭艺。搀崭。開(kāi)始又懶得去看文檔,導(dǎo)致沒(méi)有找到正確的debug方式匹表,一直在查關(guān)于sshconfig的問(wèn)題门坷。 找到正確debug方式之后,解決起來(lái)就非撑鄱疲快默蚌。

PS:無(wú)論什么工具,debug一定要好好看文檔苇羡。绸吸。。。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末锦茁,一起剝皮案震驚了整個(gè)濱河市攘轩,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌码俩,老刑警劉巖度帮,帶你破解...
    沈念sama閱讀 218,682評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異稿存,居然都是意外死亡笨篷,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)瓣履,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)率翅,“玉大人,你說(shuō)我怎么就攤上這事袖迎∶岢簦” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,083評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵燕锥,是天一觀的道長(zhǎng)辜贵。 經(jīng)常有香客問(wèn)我,道長(zhǎng)脯宿,這世上最難降的妖魔是什么念颈? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,763評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮连霉,結(jié)果婚禮上榴芳,老公的妹妹穿的比我還像新娘。我一直安慰自己跺撼,他們只是感情好窟感,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著歉井,像睡著了一般柿祈。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上哩至,一...
    開(kāi)封第一講書(shū)人閱讀 51,624評(píng)論 1 305
  • 那天躏嚎,我揣著相機(jī)與錄音,去河邊找鬼菩貌。 笑死卢佣,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的箭阶。 我是一名探鬼主播虚茶,決...
    沈念sama閱讀 40,358評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼戈鲁,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了嘹叫?” 一聲冷哼從身側(cè)響起婆殿,我...
    開(kāi)封第一講書(shū)人閱讀 39,261評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎罩扇,沒(méi)想到半個(gè)月后婆芦,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,722評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡喂饥,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年寞缝,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片仰泻。...
    茶點(diǎn)故事閱讀 40,030評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖滩届,靈堂內(nèi)的尸體忽然破棺而出集侯,到底是詐尸還是另有隱情,我是刑警寧澤帜消,帶...
    沈念sama閱讀 35,737評(píng)論 5 346
  • 正文 年R本政府宣布棠枉,位于F島的核電站,受9級(jí)特大地震影響泡挺,放射性物質(zhì)發(fā)生泄漏辈讶。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評(píng)論 3 330
  • 文/蒙蒙 一娄猫、第九天 我趴在偏房一處隱蔽的房頂上張望贱除。 院中可真熱鬧,春花似錦媳溺、人聲如沸月幌。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,941評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)扯躺。三九已至,卻和暖如春蝎困,著一層夾襖步出監(jiān)牢的瞬間录语,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,057評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工禾乘, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留澎埠,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,237評(píng)論 3 371
  • 正文 我出身青樓盖袭,卻偏偏與公主長(zhǎng)得像失暂,于是被迫代替她去往敵國(guó)和親彼宠。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • SSH全稱(chēng)Secure SHell弟塞,顧名思義就是非常安全的shell的意思凭峡,SSH協(xié)議是IETF(Internet...
    StarShift閱讀 2,528評(píng)論 0 7
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn)决记,斷路器摧冀,智...
    卡卡羅2017閱讀 134,659評(píng)論 18 139
  • SSH 為 Secure Shell 的縮寫(xiě),由 IETF 的網(wǎng)絡(luò)小組(Network Working Group...
    shuff1e閱讀 1,770評(píng)論 1 11
  • SSH(Secure Shell)協(xié)議為遠(yuǎn)程登錄或其它網(wǎng)絡(luò)服務(wù)(如:sftp、scp)提供安全保障的一種協(xié)議扩借。它設(shè)...
    CHUANHAI閱讀 5,520評(píng)論 0 0
  • 有些人忘不掉潮罪,就放在心里康谆。 我行過(guò)許多地方的橋,看過(guò)許多次數(shù)的云嫉到,喝過(guò)許多種類(lèi)的酒沃暗,卻只愛(ài)過(guò)一個(gè)正當(dāng)最好年齡的人。...
    糊涂孩子閱讀 229評(píng)論 0 0