SSH Key เป็นตัวช่วยเพิ่มความปลอดภัยอีกระดับหนึ่งในการตรวจสอบการเข้าระบบ
SSH หรือ Secure Shell เป็นการเข้าถึงระบบหรือ Server ผ่านทาง network ซึ่งโดยปกติแล้วจะเป็นการใช้ username กับ password คู่กันในการ authentication แต่ด้วย password authentication ระบบอาจมีช่องโหว่ เช่นถูก brute force
Key authentication ก็คล้ายกับ password ตรงที่ต้องมีคู่ที่ตรงกัน แต่ Key มีการเข้ารหัส (encrypt) ระบบ Key จะประกอบด้วยกุญแจ 2 ดอก คือ
- Private key เป็นไฟล์ที่เจ้าของเก็บไว้ Key นี้มีความสำคัญมากใช้สำหรับไขเข้าระบบ เพราะฉนั้นต้องเก็บรักษาให้ปลอดภัย
- Public key เป็นไฟล์ที่เก็บไว้บนระบบหรือ server เพื่อใช้ตรวจสอบกับ Private key
สร้าง SSH Key
หากท่านใช้ Windows 10 ท่านสามารถติดตั้ง Ubuntu หรือ Bash เพิ่มเติมได้จาก Store ของ Microsoft และสามารถใช้วิธีการเดียวกันนี้ได้ โดยทำการผ่าน Ubuntu/Bash (แม้แต่ MS ยังรองรับ Linux แล้วท่านจะไม่ switch หรือ)
การสร้าง Key นั้นง่ายมาก เข้า Linux terminal แล้วใช้คำสั่ง ssh-keygen ในการสร้าง
รูปแบบคำสั่ง
ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1] [-N new_passphrase] [-C comment] [-f output_keyfile]
ดูรูปแบบคำสั่งเต็มๆ พร้อมคำอธิบายได้จาก $ man ssh-keygen
ตัวอย่างคำสั่งที่แนะนำ
$ ssh-keygen -b 4096 ตัวอย่างหน้าจอ [email protected]:~$ ssh-keygen -b 4096 Generating public/private rsa key pair. Enter file in which to save the key (/home/choo/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/choo/.ssh/id_rsa. Your public key has been saved in /home/choo/.ssh/id_rsa.pub. The key fingerprint is: SHA256:XOFdxD3Um10hqZo50qGqREj8SHhF5MlCcj0RgZoVXEE [email protected] The key randomart image is: +---[RSA 4096]----+ |..+OE= . +=+o| |o+++o. . o oo.+| |.Bo +. o o *| |=.+. . o . o.| | o o S = | | . o * | | . . . . | | . . | | .. | +----[SHA256]-----+ [email protected]:~$ ls -l .ssh total 12 -rw------- 1 choo choo 414 Aug 30 12:26 authorized_keys -rw------- 1 choo choo 3243 Aug 30 13:23 id_rsa -rw-r--r-- 1 choo choo 740 Aug 30 13:23 id_rsa.pub
การนำไปใช้
เมื่อได้ SSH Key มา เราสามารถทำการ copy public key ไปยัง server ที่เราต้องการได้ โดยใช้คำสั่ง ssh-copy-id แล้วตามด้วย [email protected] ตามตัวอย่างนี้
[email protected]:~$ ssh-copy-id [email protected] /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/choo/.ssh/id_rsa.pub" The authenticity of host '192.168.1.119 (192.168.1.119)' can't be established. ECDSA key fingerprint is SHA256:yvAnjtZGwCFhakJQDGZaHqeTG90u5Qfeq/GHn+wFsvc. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]'s password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '[email protected]'" and check to make sure that only the key(s) you wanted were added.
เมื่อทำการส่ง public key ขึ้น host เป็นที่เรียบร้อย เราก็จะสามารถ SSH เข้าได้โดยใช้แค่ key
[email protected]:~$ ssh [email protected] Linux raspberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Fri Aug 30 12:06:37 2019 from 192.168.1.102 SSH is enabled and the default password for the 'pi' user has not been changed. This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password. [email protected]:~ $
เป็นอย่างไรบ้างครับ ง่ายไหมครับกับการสร้าง และนำไปใช้งาน SSH Key