Site icon PlusMagi's Blog By Pitt Phunsanit

SSH: ให้ mac จำ login with passphras

เป็นขั้นตอนสุดท้ายในการย่อคำสั่ง SSH แล้วคือแทนที่จะมากรอก passphras เราก็ใช้ MacOs (และ all apple device) กรอก login passphras แทนเรา เมื่อเราเรียก ใช้คำสั่ง อย่าง ssh digitalocean_vps มันจะมีคำถามอยู่ว่า Enter passphrase for key ‘/Users/phunsanit/.ssh/digitalocean_vps‘: คือยังต้องกรอก passphras ให้อยู่นั่นละ แต่อยากจะใช้แค่ ssh digitalocean_vps ก็เข้าไปใน server ได้เลย ก็ทำได้โดยใช้ password manager ของ MacOS Keychain Access (หรือ “การเข้าถึงพวงกุญแจ”)


ทำให้ mac ใช้ ssh-agent ให้เองอัตโนมัติ (ทำครั้งเดียว) Set and Forget

  1. เปิดไฟล์ Config โดยคำสั่ง
    nano ~/.ssh/config
  2. เพิ่มไว้ บนสุด ของไฟล์
    Host *
    AddKeysToAgent yes
    UseKeychain yes
    IdentityFile ~/.ssh/id_rsa
  3. บันทึก
  4. ปิด Terminal แล้วเปิดใหม่ (New Window)

โดย
AddKeysToAgent yes: เมื่อคุณ SSH ครั้งแรกและใส่รหัสผ่าน มันจะจำกุญแจนั้นลง Agent ให้อัตโนมัติ (ไม่ต้องสั่ง ssh-add เอง)
UseKeychain yes: สำคัญมาก! มันสั่งให้ SSH ไปดึงรหัส Passphrase จาก Apple Keychain (ที่คุณเพิ่งบันทึกไป) มากรอกให้เอง


manual add Apple Keychain

  1. add โดยคำสั่งรูปแบบ
    ssh-add --apple-use-keychain ~/.ssh/{key no .pub}
    เช่น
    ssh-add --apple-use-keychain ~/.ssh/digitalocean_vps
  2. ตั้งสิทธิ์ไฟล์
    chmod 600 ~/.ssh/digitalocean_vps
  3. ทดสอบโดยการใช้
    ssh digtalocean_vps

ดู passphras ที่เก็บเอาไว้

  1. เปิดโปรแกรม Keychain Access
    1. กด Command + Spacebar (เพื่อเปิด Spotlight Search)
    2. พิมพ์ว่า Keychain Access (หรือ “การเข้าถึงพวงกุญแจ”) แล้วกด Enter
    3. ในช่องค้นหา (มุมขวาบนของโปรแกรม) ให้พิมพ์ชื่อไฟล์กุญแจของคุณ เช่น digitalocean_vps
    4. จะเห็นรายการประมาณว่า SH: /Users/phunsanit/.ssh/digitalocean_vps
  2. กดดูรหัส
    1. ดับเบิ้ลคลิก ที่รายการนั้น
    2. ติ๊ก “Show password” (แสดงรหัสผ่าน)
    3. กรอก รหัสผ่านเข้า mac ของเรา
    4. จะเห็น passphras ที่ safe อยู่

อ่านเพิ่มเติม

Exit mobile version