這篇文章將為大家詳細講解有關ssh怎么用,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
本文以以下需求為背景,介紹詳細的做法:
需在同一臺服務器同時部署兩個不同的 Github 倉庫(對 Bitbucket 等 git 服務同樣適用)
root 用戶可在遠程登錄 SSH 后附上預期的 SSH Key 進行 git 命令操作
nginx 用戶進程(如 php-fpm)可在進程內附上預期的 SSH Key 進行 git 命令操作
以 root 身份登錄服務器,為 root 用戶和 nginx 用戶分別生成 SSH Key。
倉庫 1:
$ ssh-keygen -b 2048 -t rsa -f "~/.ssh/id_rsa_github_myrepo1" $ sudo -u nginx ssh-keygen -b 2048 -t rsa -C "nginx@localhost" -f "/var/lib/nginx/.ssh/id_rsa_github_myrepo1"
倉庫 2:
$ ssh-keygen -b 2048 -t rsa -f "~/.ssh/id_rsa_github_myrepo2" $ sudo -u nginx ssh-keygen -b 2048 -t rsa -C "nginx@localhost" -f "/var/lib/nginx/.ssh/id_rsa_github_myrepo2"
此處的 nginx 用戶的主目錄因操作系統不同而擁有不同的路徑,經測試 CentOS 系的操作系統可能值為:
"/var/lib/nginx" "/var/cache/nginx" "/usr/share/nginx"
之其一,請讀者執行
sudo -u nginx ssh-keygen
查看并以實際路徑為準,替換本文中的對應命令的路徑。如:
[root@localhost]# sudo -u nginx ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/var/cache/nginx/.ssh/id_rsa): 此處的 nginx 用戶的主目錄為 "/var/cache/nginx"。
將生成好的 SSH Key 的公鑰部分添加到兩個 Github 倉庫設置的 Deploy Keys(部署密鑰)中。
倉庫 1:
$ cat "~/.ssh/id_rsa_github_myrepo1.pub" $ cat "/var/lib/nginx/.ssh/id_rsa_github_myrepo1.pub"
倉庫 2:
$ cat "~/.ssh/id_rsa_github_myrepo2.pub" $ cat "/var/lib/nginx/.ssh/id_rsa_github_myrepo2.pub"
編輯用戶的 SSH 配置文件,并指定主機的詳細自定義配置。
root 用戶:
$ nano "~/.ssh/config" Host github.com-myrepo1 HostName github.com User git IdentityFile ~/.ssh/id_rsa_github_myrepo1 Host github.com-myrepo2 HostName github.com User git IdentityFile ~/.ssh/id_rsa_github_myrepo2
nginx 用戶:
$ nano "/var/lib/nginx/.ssh/config" Host github.com-myrepo1 HostName github.com User git IdentityFile /var/lib/nginx/.ssh/id_rsa_github_myrepo1 Host github.com-myrepo2 HostName github.com User git IdentityFile /var/lib/nginx/.ssh/id_rsa_github_myrepo2
完成了上述步驟,讀者現在可以在 root 或 nginx 用戶登錄或以其運行的進程中執行 git 命令,并且會自動附上指定的 SSH Key。例如:
$ git clone git@github.com-myrepo1:tommy/myrepo1.git "/var/www/myrepo1" $ git clone git@github.com-myrepo2:tommy/myrepo2.git "/var/www/myrepo2"
<? header( 'Content-type: text/text' ); exit( shell_exec( 'cd /var/www/myrepo1; git pull origin master 2>&1' ) ); exit( shell_exec( 'cd /var/www/myrepo2; git pull origin master 2>&1' ) );
git remote set-url <name> <newurl> git remote set-url origin git@github.com-myrepo1:tommy/myrepo1.git git remote set-url origin git@github.com-myrepo2:tommy/myrepo2.git
刪掉,重新生成即可。
rm -f ~/.ssh/know_hosts ssh -T git@github.com Hi Anonymous! You've successfully authenticated, but Github.com does not provide shell access.
Bad owner or permissions sudo chmod 600 ~/.ssh/config
關于“ssh怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。