原文鏈接:https://blog.csdn.net/qq_43688472/article/details/109625590
場(chǎng)景需求:gitlab自動(dòng)化發(fā)布,需要遠(yuǎn)程發(fā)布到其他服務(wù)器這里使用到ssh免密登錄
1.命令介紹
ssh-copy-id命令可以把本地的ssh公鑰文件安裝到遠(yuǎn)程主機(jī)對(duì)應(yīng)的賬戶下。
達(dá)到的功能:
ssh-copy-id - 將你的公共密鑰填充到一個(gè)遠(yuǎn)程機(jī)器上的authorized_keys文件中。
使用模式:
ssh-copy-id [-i [identity_file]] [user@]machine
ssh-copy-id 是一個(gè)實(shí)用ssh去登陸到遠(yuǎn)程服務(wù)器的腳本(假設(shè)使用一個(gè)登陸密碼,
因此,密碼認(rèn)證應(yīng)該被激活直到你已經(jīng)清理了做了多個(gè)身份的使用)。
它也能夠改變遠(yuǎn)程用戶名的權(quán)限,~/.ssh和~/.ssh/authorized_keys
刪除群組寫(xiě)的權(quán)限(在其它方面,如果遠(yuǎn)程機(jī)上的sshd在它的配置
文件中是嚴(yán)格模式的話,這能夠阻止你登陸。)。
如果這個(gè) “-i”選項(xiàng)已經(jīng)給出了,然后這個(gè)認(rèn)證文件(默認(rèn)是~/.ssh
/id_rsa.pub)被使用,不管在你的ssh-agent那里是否有任何密鑰。
另外,命令 “ssh-add -L” 提供任何輸出,它使用這個(gè)輸出優(yōu)先于
身份認(rèn)證文件。如果給出了參數(shù)“-i”選項(xiàng),或者ssh-add不產(chǎn)生輸出,
然后它使用身份認(rèn)證文件的內(nèi)容。一旦它有一個(gè)或者多個(gè)指紋,它使
用ssh將這些指紋填充到遠(yuǎn)程機(jī)~/.ssh/authorized_keys文件中。
2.使用說(shuō)明
ssh-keygen 產(chǎn)生公鑰與私鑰對(duì).
ssh-copy-id 將本機(jī)的公鑰復(fù)制到遠(yuǎn)程機(jī)器的authorized_keys文件中,
ssh-copy-id也能讓你有到遠(yuǎn)程機(jī)器的home, ~./ssh , 和 ~/.ssh/authorized_keys的權(quán)利
第一步:在本地機(jī)器上使用ssh-keygen產(chǎn)生公鑰私鑰對(duì)
jsmith@local-host$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Pess enter key]
Your identification has been saved in /home/jsmith/.ssh/id_rsa.
Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub.
The key fingerprint is:
33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jsmith@local-host
第二步:用ssh-copy-id將公鑰復(fù)制到遠(yuǎn)程機(jī)器中
jsmith@local-host$ ssh-copy-id -i ~/.ssh/id_rsa.pub remote-host
jsmith@remote-host's password:
Now try logging into the machine, with "ssh 'remote-host'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
注意: ssh-copy-id 將key寫(xiě)到遠(yuǎn)程機(jī)器的 ~/ .ssh/authorized_key.文件中
第三步: 登錄到 遠(yuǎn)程機(jī)器不用輸入密碼
jsmith@local-host$ ssh remote-host
Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2
[Note: SSH did not ask for password.]
jsmith@remote-host$ [Note: You are on remote-host here]