远程服务器 上传公钥,ssh-keygen教程第5章:copy公钥要服务端
ssh-keygen教程第5章:copy公鑰要服務(wù)端
上一章,我們已經(jīng)生成了公鑰和私鑰,這一章我只需要將公鑰copy到服務(wù)器(遠(yuǎn)程主機(jī))就可以實(shí)現(xiàn)免密碼登錄了。
graph LR
本地主機(jī) --ssh-copy-id--> 遠(yuǎn)程主機(jī)
ssh-copy-id 公鑰上傳到遠(yuǎn)程主機(jī)
$ ssh-copy-id root@k8s-node1
/usr/bin/ssh-copy-id: ERROR: failed to open ID file '/root/.pub': No such file or directory
(to install the contents of '/root/.pub' anyway, look at the -f option)
從提示中看,是沒(méi)有找到hellodemos.pub這個(gè)文件,所以我們可以通過(guò)-i選項(xiàng)指定pub文件所在的位置:
$ ssh-copy-id -i ~/.ssh/hellodemos.pub root@k8s-node1
root是遠(yuǎn)程服務(wù)器的用戶名
k8s-node1主機(jī)名(IP)
注意,你如果在上一章改了公鑰的名字,那么這里就需要填寫(xiě)具體的文件名字 *.pub
輸出:
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "hellodemos.pub"
/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
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0755 for '/root/.ssh/id_rsa' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/root/.ssh/id_rsa": bad permissions
上面有一段關(guān)于權(quán)限的說(shuō)明, Permissions 0755 for '/root/.ssh/id_rsa' are too open 需要0755的權(quán)限才能打開(kāi)。 Load key "/root/.ssh/id_rsa": bad permissions 這句話也代表了權(quán)限似乎錯(cuò)誤了。
這時(shí)候,我們需要設(shè)置文件的權(quán)限。
系統(tǒng)自動(dòng)在服務(wù)器機(jī)器root/.ssh目錄中生成authorized_keys文件,這個(gè)文件的內(nèi)容和本地機(jī)上的 pub文件一摸一樣,就是 ssh公鑰文件。
sh-copy-id可能遇到的權(quán)限問(wèn)題
為了讓私鑰文件和公鑰文件能夠在認(rèn)證中起作用, 需要確保權(quán)限的正確性:
對(duì)于.ssh目錄以及其內(nèi)部的公鑰、私鑰文件, 當(dāng)前用戶至少要有執(zhí)行權(quán)限, 其他用戶最多只能有執(zhí)行權(quán)限.
不要圖省事設(shè)置成777權(quán)限: 太大的權(quán)限不安全, 而且數(shù)字簽名也不支持這種權(quán)限策略.
對(duì)普通用戶, 建議設(shè)置成600權(quán)限: chmod 600 ~/.ssh/*;
對(duì)root用戶, 建議設(shè)置成644權(quán)限: chmod 644 ~/.ssh/*.
我這里是root用戶,執(zhí)行chmod 644 ~/.ssh/*修改權(quán)限就可以了。
一鍵配置2臺(tái)機(jī)器ssh免密登錄腳本
還是那句話,如果你覺(jué)得麻煩,執(zhí)行一鍵安裝ssh免密碼登錄就可以了,我們講這么多,僅僅為了你可以了解原理。:)
wget http://66-ai.com/download/script-litte-prince/app/ssh-keygen.sh -O /root/ssh-keygen.sh && sh /root/ssh-keygen.sh
總結(jié)
以上是生活随笔為你收集整理的远程服务器 上传公钥,ssh-keygen教程第5章:copy公钥要服务端的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 解决Xcode 13.2 不兼容 ios
- 下一篇: http.cookiejar.LoadE