ความเป็นไปได้ทางหนึ่งที่จะใช้~/.ssh/configคือใช้Matchข้อ จำกัด แทนHostข้อ จำกัด โดยเฉพาะMatch Execเรียกคำสั่งเชลล์เพื่อตัดสินใจว่าจะใช้การประกาศหรือไม่ ใน bash คุณสามารถใช้คำสั่งต่อไปนี้:
[ git@git.company.com:gitolite-admin = $(git config --get remote.origin.url)'' ]
สิ่งนี้ใช้[คำสั่งbash เพื่อตรวจสอบว่ามีสองสตริงเท่ากัน ในกรณีนี้เป็นการทดสอบว่าสตริงgit@git.company.com:gitolite-adminตรงกับเอาต์พุตที่ได้รับจาก$(git config --get remote.origin.url)''คำสั่ง
คุณสามารถใช้คำสั่งอื่นใดที่ระบุที่เก็บที่เชลล์เปิดอยู่ สำหรับการทำงานมันเป็นสิ่งสำคัญที่จะมีตัวแปรที่กำหนดให้เปลือกของคุณในกรณีของฉัน$SHELL /bin/bashตัวอย่างแบบเต็มจะเป็นดังต่อไปนี้~/.ssh/config:
Match Exec "[ git@git.company.com:gitolite-admin = $(git config --get remote.origin.url)'' ]"
IdentityFile ~/.ssh/gitolite-admin
IdentitiesOnly yes
ForwardAgent no
ForwardX11 no
ForwardX11Trusted no
Match Exec "[ git@git.company.com:some_repo = $(git config --get remote.origin.url)'' ]"
IdentityFile ~/.ssh/yourOwnPrivateKey
IdentitiesOnly yes
ForwardAgent no
ForwardX11 no
ForwardX11Trusted no
ในตัวอย่างนี้ผมสันนิษฐานว่า~/.ssh/yourOwnPrivateKeyมีคีย์ส่วนตัวของคุณเองและที่มีคีย์ส่วนตัวของผู้ใช้~/.ssh/gitolite-admin gitolite-adminฉันรวมการIdentitiesOnly yesประกาศเพื่อให้แน่ใจว่ามีเพียงคีย์เดียวเท่านั้นที่เสนอให้กับเซิร์ฟเวอร์ git ที่ระบุโดยMark LongairLongair การประกาศอื่น ๆ เป็นเพียงตัวเลือก ssh มาตรฐานสำหรับ git
คุณสามารถเพิ่มการกำหนดค่านี้หากคุณมีหลายอย่างsome_repoที่คุณต้องการใช้กับปุ่มที่แตกต่างกัน หากคุณมีที่เก็บข้อมูลหลายแห่งgit@git.company.comและส่วนใหญ่ใช้ที่เก็บที่~/.ssh/yourOwnPrivateKeyเหมาะสมเพื่อรวมคีย์นี้เป็นค่าเริ่มต้นสำหรับโฮสต์ ในกรณีนี้~/.ssh/configจะเป็น:
Match Exec "[ git@git.company.com:gitolite-admin = $(git config --get remote.origin.url)'' ]"
IdentityFile ~/.ssh/gitolite-admin
IdentitiesOnly yes
Host git.company.com
IdentityFile ~/.ssh/yourOwnPrivateKey
IdentitiesOnly yes
ForwardAgent no
ForwardX11 no
ForwardX11Trusted no
โปรดทราบว่าคำสั่งซื้อมีความสำคัญและHost git.company.comข้อ จำกัด ควรปรากฏหลังจากรายการMatch Execหนึ่งรายการ