Linux

如何同步更改並重新載入遠端服務,但只輸入一次 SSH 密碼

  • September 18, 2016

我想將對文件的本地更改同步到遠端伺服器,然後重新載入使用該文件的服務。例如:

# Sync file to remote server
rsync -avzhe ssh --rsync-path "sudo rsync" \
   /etc/haproxy/haproxy.cfg vagrant@192.168.50.31:/etc/haproxy/

# Reload remote service
ssh vagrant@192.168.50.31 "sudo /etc/init.d/haproxy reload"

這樣做會迫使我輸入兩次 SSH 使用者的密碼。有什麼辦法可以更好地做到這一點,這樣我可能只需要輸入一次密碼嗎?

執行ssh-keygen

ssh > ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): abc.key
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in abc.key.
Your public key has been saved in abc.key.pub.
The key fingerprint is:
9e:e2:46:c0:9a:ac:fe:fb:ea:ec:ee:66:00:39:91:f8 root@domain.tld
The key's randomart image is:
+--[ RSA 2048]----+
|..               |
|+                |
| + .             |
|+ E o            |
|.o o .  S        |
| .+   .. .       |
| ..  .. o        |
|. .o ...         |
|..OX+o.          |
+-----------------+

在這裡,我通過按 省略了密碼片語兩次Enter。現在我有abc.keyabc.key.pub文件。讓我們只讓我閱讀它們:

chmod 600 abc.key*

打開abc.key.pub

ssh > cat abc.key.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDppADcp53HZ4KF7EsNcfV1qM4WtuYEeHWjAXsFqJwHd7loyKYnORHtAMtd78+cSHTiAycLeNqX9xRh/lPlhifeovxJ8th6c4W7cXNWalmz6q+RkDE8ZznmcNSvrMToLm7oivdsBDiPIBbiOtunKRQ2TsNZ72RJuC3RlrmUakWp9RcrQMFAvyxkzJYmm4fTSozosA2/NaHn0qIm/5Ed9I5BQ9IzRQLgzsXCtBTQY83aBcYLk+AzhQiM45AV1mOGzYAkgXu6Uo3P9jDxi2DE1priW8SnKEXhrTEmNDAZDkDRAz1Go/lGnENslqLZ0URs5spGtHGhcWt9DmSjVGbbGvpP user@domain.tld

複製整行並將其粘貼到/home/user/.ssh/authorized_keys遠端主機上。

現在嘗試:

ssh -i /path/abc.key user@remote.host

是的,現在您完全沒有鍵盤身份驗證就登錄了。你可以在任何你用過的地方做這個把戲ssh

引用自:https://serverfault.com/questions/803840