คุณสามารถล็อกอินเข้าสู่เซิร์ฟเวอร์ Linux ระยะไกลโดยไม่ต้องใส่รหัสผ่านได้ใน 3 ขั้นตอนง่ายๆ โดยใช้ ssh-keygen และ ssh-copy-id ตามที่อธิบายในบทความนี้
ssh-keygen สร้าง public และ private keys ssh-copy-id คัดลอก public key ของ local-host ไปยัง authorized_keys file ของ remote-host ssh-copy-id ยังกำหนดสิทธิ์ที่เหมาะสมให้กับ home, ~/.ssh, และ ~/.ssh/authorized_keys ของ remote-host
บทความนี้ยังอธิบาย 3 ข้อรบกวนเล็กน้อยของการใช้ ssh-copy-id และวิธีใช้ ssh-copy-id ร่วมกับ ssh-agent
ขั้นตอนที่ 1: สร้าง public และ private keys ด้วย ssh-keygen บน local-host
jsmith@local-host$ [หมายเหตุ: คุณอยู่ที่ local-host] 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: [Press 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
ขั้นตอนที่ 2: คัดลอก public key ไปยัง remote-host ด้วย ssh-copy-id
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 ต่อท้าย keys ไปยัง .ssh/authorized_key ของ remote-host
ขั้นตอนที่ 3: ล็อกอิน remote-host โดยไม่ต้องใส่รหัสผ่าน
jsmith@local-host$ ssh remote-host Last login: Sun Nov 16 17:22:33 2008 from 192.168.1.2 [หมายเหตุ: SSH ไม่ได้ถามรหัสผ่าน.] jsmith@remote-host$ [หมายเหตุ: คุณอยู่ที่ remote-host ตอนนี้]
3 ขั้นตอนง่ายๆ ข้างต้นควรทำงานได้ในส่วนใหญ่ของกรณี
เราเคยอภิปรายรายละเอียดเกี่ยวกับการทำ SSH และ SCP จาก openSSH ไป openSSH โดยไม่ใส่รหัสผ่าน
หากคุณใช้ SSH2 เราเคยอภิปรายเกี่ยวกับการทำ SSH และ SCP โดยไม่ต้องใส่รหัสผ่าน จาก SSH2 ไป SSH2, จาก OpenSSH ไป SSH2 และจาก SSH2 ไป OpenSSH
การใช้ ssh-copy-id ร่วมกับ ssh-add/ssh-agent
เมื่อไม่มีค่าสำหรับ ตัวเลือก -i และหาก ~/.ssh/identity.pub ไม่พร้อมใช้งาน ssh-copy-id จะแสดงข้อความผิดพลาดดังนี้
jsmith@local-host$ ssh-copy-id -i remote-host /usr/bin/ssh-copy-id: ERROR: No identities found
หากคุณโหลด keys ไปที่ ssh-agent โดยใช้ ssh-add แล้ว ssh-copy-id จะดึง keys จาก ssh-agent เพื่อคัดลอกไป remote-host นั่นคือ มันคัดลอก keys ที่ให้โดยคำสั่ง ssh-add -L ไปยัง remote-host เมื่อคุณไม่ส่ง ตัวเลือก -i ให้ ssh-copy-id
jsmith@local-host$ ssh-agent $SHELL jsmith@local-host$ ssh-add -L The agent has no identities. jsmith@local-host$ ssh-add Identity added: /home/jsmith/.ssh/id_rsa (/home/jsmith/.ssh/id_rsa) jsmith@local-host$ ssh-add -L ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAsJIEILxftj8aSxMa3d8t6JvM79DyBV aHrtPhTYpq7kIEMUNzApnyxsHpH1tQ/Ow== /home/jsmith/.ssh/id_rsa jsmith@local-host$ ssh-copy-id -i 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. [หมายเหตุ: นี้ได้เพิ่ม key ที่แสดงโดย ssh-add -L]
สามข้อรบกวนเล็กน้อยของ ssh-copy-id
ต่อไปนี้คือข้อรบกวนเล็กน้อยของ ssh-copy-id
- Default public key: ssh-copy-id ใช้ ~/.ssh/identity.pub เป็นไฟล์ public key เริ่มต้น (เช่น เมื่อไม่ส่งค่าให้ ตัวเลือก -i) แทนที่จะเป็นนั้น ผมหวังว่ามันจะใช้ id_dsa.pub, หรือ id_rsa.pub, หรือ identity.pub เป็น default keys นั่นคือ หากมีอยู่ใดๆ หนึ่ง ควรคัดลอกไป remote-host หากมีสองหรือสามอัน ควรคัดลอก identity.pub เป็น default
- The agent has no identities: เมื่อ ssh-agent กำลังรัน และ ssh-add -L คืนค่า "The agent has no identities" (เช่น ไม่มี keys เพิ่มใน ssh-agent) ssh-copy-id จะยังคงคัดลอกข้อความ "The agent has no identities" ไปยัง authorized_keys ของ remote-host
- Duplicate entry in authorized_keys: ผมหวังว่า ssh-copy-id จะตรวจสอบ duplicate entry บน authorized_keys ของ remote-host หากรัน ssh-copy-id หลายครั้งบน local-host จะต่อท้าย key เดิมซ้ำๆ บน authorized_keys ของ remote-host โดยไม่ตรวจสอบ duplicates แม้กับ duplicate entries ทุกอย่างทำงานได้ตามปกติ แต่ผมอยากให้ authorized_keys file ของผมสะอาด
หากคุณชอบบทความนี้ โปรดบุ๊กมาร์กบน Delicious และ Stumble it.